diff --git a/source/ecs/manager.d b/source/ecs/manager.d index 19d7cd1..3318d9e 100644 --- a/source/ecs/manager.d +++ b/source/ecs/manager.d @@ -69,7 +69,7 @@ export class EntityManager if(system.m_components)Mallocator.instance.dispose(system.m_components); if(system.m_excluded_components)Mallocator.instance.dispose(system.m_excluded_components); if(system.m_optional_components)Mallocator.instance.dispose(system.m_optional_components); - if(system.name)Mallocator.instance.dispose(system.name); + if(system.m_name)Mallocator.instance.dispose(system.m_name); if(system.m_event_callers)Mallocator.instance.dispose(system.m_event_callers); if(system.m_system_pointer)Mallocator.instance.dispose(system.m_system_pointer); @@ -344,20 +344,31 @@ export class EntityManager data_system.handleEvent(input, *cast(Type*) data.event); } - static void setEventCallers(Sys)(ref System system) + void setEventCallers(Sys)(ref System system) { enum event_handlers_num = __traits(getOverloads, Sys, "handleEvent").length; - system.m_event_callers = Mallocator.instance.makeArray!( - System.EventCaller)(event_handlers_num); + System.EventCaller[] callers = (cast(System.EventCaller*)alloca(event_handlers_num * System.EventCaller.sizeof))[0..event_handlers_num]; + int i = 0; foreach (j, func; __traits(getOverloads, Sys, "handleEvent")) { - alias EventParamType = Parameters!(__traits(getOverloads, - Sys, "handleEvent")[j])[1]; - system.m_event_callers[j].callback = cast( - void*)&callEventHandler!(EventParamType); - system.m_event_callers[j].id = EventParamType.event_id; + alias Params = Parameters!(__traits(getOverloads, + Sys, "handleEvent")[j]); + static if(Params.length == 2 && is(Params[0] == __traits(getMember, Sys, "EventInput"))) + { + alias EventParamType = Params[1]; + enum EventName = Unqual!(EventParamType).stringof; + ushort evt = events_map.get(cast(char[]) EventName, ushort.max); + assert(evt != ushort.max, "Can't register system \""~Sys.stringof~"\" due to non existing event \""~EventName~"\"."); + + callers[i].callback = cast( + void*)&callEventHandler!(EventParamType); + callers[i].id = EventParamType.event_id; + i++; + } } + + system.m_event_callers = Mallocator.instance.makeArray(callers[0..i]); } static if (__traits(hasMember, Sys, "handleEvent")) @@ -736,8 +747,8 @@ export class EntityManager } else { - system.name = Mallocator.instance.makeArray(Sys.stringof); - systems_map.add(system.name, cast(ushort) systems.length); + system.m_name = Mallocator.instance.makeArray(Sys.stringof); + systems_map.add(system.m_name, cast(ushort) systems.length); system.m_id = cast(ushort)(systems.length); @@ -2302,9 +2313,9 @@ export class EntityManager caller.exclusion = null; /*import std.stdio; - write("Exclusive systems for system ", caller.system.name, ": "); + write("Exclusive systems for system ", caller.system.m_name, ": "); foreach (ex; exclusion[0 .. index]) - write(ex.system.name, " "); + write(ex.system.m_name, " "); writeln();*/ } @@ -2362,9 +2373,9 @@ export class EntityManager caller.dependencies = null; /*import std.stdio; - write("Dependencies for system ", caller.system.name, ": "); + write("Dependencies for system ", caller.system.m_name, ": "); foreach (ex; caller.dependencies) - write(ex.system.name, " "); + write(ex.system.m_name, " "); writeln();*/ } } diff --git a/source/ecs/system.d b/source/ecs/system.d index e033147..b4ad780 100644 --- a/source/ecs/system.d +++ b/source/ecs/system.d @@ -74,6 +74,14 @@ struct System return m_id; } + /************************************************************************************************************************ + *Get system name. + */ + export const (char)[] name() nothrow @nogc + { + return cast(const (char)[])m_name; + } + struct EventCaller { ushort id; @@ -97,7 +105,7 @@ package: int m_pass; ///system name - char[] name; + char[] m_name; ///required components ushort[] m_components; diff --git a/tests/tests.d b/tests/tests.d index edd8b16..3cf3697 100644 --- a/tests/tests.d +++ b/tests/tests.d @@ -376,6 +376,11 @@ struct TestSystem2 //TestComp* tt; } + void handleEvent(EventInput input) + { + + } + void handleEvent(EventInput input, ref TestEvent event) { input.test.bg = event.a; @@ -571,9 +576,11 @@ int main() //foreach(j; 0..1_000)gEM.addEntity(tmpl); gEM.beginRegister(); - //gEM.registerSystem!TestSystem2(0); + gEM.registerSystem!TestSystem2(0); gEM.endRegister(); + System* sys = EntityManager.instance.getSystem(TestSystem2.system_id); + //gEM.generateDependencies(); //assert(*(cast(EntityID*)(cast(void*)tmpl.info.first_block+24)) == EntityID(1,1));