Skip to content

Commit

Permalink
Implement removeTemplateFrom().
Browse files Browse the repository at this point in the history
  • Loading branch information
player-03 committed Nov 11, 2024
1 parent bfb04ed commit a3a36e1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
46 changes: 44 additions & 2 deletions src/echoes/macro/EntityTemplateBuilder.hx
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,51 @@ class EntityTemplateBuilder {

//Add `applyTemplateToSelf()`; it must not be static because
//`knownComponents` may refer to instance properties or functions.
addFunctions((macro class ApplyToSelf {
addFunctions(macro class ApplyToSelf {
@:noCompletion private function applyTemplateToSelf():Void $b{ applyToSelfExprs }
}));
});

//"Remove" functions
//------------------

final removeFromSelfExprs:Array<Expr> = [];
for(component in knownComponents) {
final type:ComplexType = component.type;
removeFromSelfExprs.push(macro this.remove((_:$type)));
}
if(parents.length > 1) {
removeFromSelfExprs.push(macro if(recursive) @:privateAccess this.removeTemplateFromSelf(true));
}

fields = (macro class RemoveTemplate {
@:noCompletion public static inline function removeTemplateFrom(entity:$templateType, ?recursive:Bool = false):echoes.Entity {
entity.removeTemplateFromSelf(${ parents.length > 1 ? macro recursive : macro false });
return cast entity;
}

@:noCompletion private function removeTemplateFromSelf(recursive:Bool):Void $b{ removeFromSelfExprs }
}).fields.concat(fields);

if(parents.length <= 1) {
switch(fields[0].kind) {
case FFun(f):
f.args.pop();
default:
}
}

final parentNames:Array<String> = [for(i in 0...(parents.length - 1))
"`" + parents[i].abstractType.name + "`"
];
if(parentNames.length > 2) {
parentNames.push("and " + parentNames.pop());
}
fields[0].doc =
(knownComponents.length == 1
? 'Removes `${ new Printer().printComplexType(knownComponents[0].type) }`'
: 'Removes `${ type.name }`\'s ${ knownComponents.length } components')
+ " from the given entity.\n\nCaution: this feature is experimental, and may be subject to change."
+ (parents.length > 1 ? '\n@param recursive Also removes components inherited from ${ parentNames.join(", ") }.' : "");

return fields;
}
Expand Down
8 changes: 7 additions & 1 deletion test/AdvancedFunctionalityTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,16 @@ class AdvancedFunctionalityTest extends Test {
assertTimesCalled(3, "NameSystem.nameAdded");
assertTimesCalled(1, "NameSystem.nameRemoved");

Assert.equals(NameStringEntity.DEFAULT_NAME, new NameStringEntity().name);
NameStringEntity.applyTemplateTo(visualEntity);
Assert.equals(NameStringEntity.DEFAULT_NAME, namedEntity.name);
assertTimesCalled(4, "NameSystem.nameAdded");
assertTimesCalled(1, "NameSystem.nameRemoved");

NamedEntity.removeTemplateFrom(namedEntity);
Assert.isNull(namedEntity.name);
Assert.notNull(namedEntity.get(String));
assertTimesCalled(2, "NameSystem.nameRemoved");

final nullEntity:Null<NamedEntity> = null;
Assert.isNull(nullEntity);
#if cpp
Expand Down

0 comments on commit a3a36e1

Please sign in to comment.