Skip to content

Instantly share code, notes, and snippets.

@Jarrio
Last active February 12, 2023 02:23
Show Gist options
  • Save Jarrio/dae896d876e5a5a62125f0723fee66b2 to your computer and use it in GitHub Desktop.
Save Jarrio/dae896d876e5a5a62125f0723fee66b2 to your computer and use it in GitHub Desktop.
AdianECS EcsTools
#if !macro
import ecs.System;
import ecs.Entity;
import ecs.Universe;
#else
import haxe.macro.Expr;
import haxe.macro.Context;
using haxe.macro.TypeTools;
#end
class EcsTools {
#if !macro
public static var universe:Universe;
#end
public static macro function delete(self:ExprOf<System>, entity:ExprOf<Entity>):ExprOf<Entity> {
return macro @:pos(self.pos) $self.universe.deleteEntity(entity);
}
public static macro function addComponents(components:Expr):ExprOf<Entity> {
var block = [macro var entity = universe.createEntity()];
block.push(macro universe.setComponents(entity, $components));
block.push(macro entity);
return macro @:pos(components.pos) $b{block};
}
public static macro function addWithBaseType(components:Array<Expr>):ExprOf<Entity> {
var block = [macro var entity = universe.createEntity()];
var arr = [macro entity];
for (key => comp in components) {
var type = Context.typeof(comp);
var is_class = type.getName() == 'TInst';
if (!is_class) {
arr.push(macro @:pos(comp.pos) $comp);
continue;
}
try {
var type_b = TypeTools.toComplexType(TInst(type.getClass().superClass.t, type.getClass().superClass.params));
switch (comp.expr) {
case ENew(t, params):
block.push(macro @:pos(comp.pos) var __tmp_remap = $comp);
arr.push(macro @:pos(comp.pos) __tmp_remap);
arr.push(macro @:pos(comp.pos) (__tmp_remap : $type_b));
default:
arr.push(macro @:pos(comp.pos) $comp);
arr.push(macro @:pos(comp.pos) ($comp : $type_b));
}
} catch (e) {
Context.error(e.message, comp.pos);
}
}
block.push(macro @:pos(components[0].pos) universe.setComponents($a{arr}));
block.push(macro entity);
return macro @:pos(components[0].pos) $b{block};
}
}
import tools.EcsTools.addComponents;
import tools.EcsTools.addWithBaseType;
using tools.EcsTools;
function foo() {
EcsTools.universe = universe;
addComponents(new ComponentA(), new ComponentB()); //will return a new entity
}
class Foo extends ecs.System {
public function new(_) {
super(_);
this.delete(entity); //shortcut on systems to universe.deleteEntity
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment