diff --git a/source/ecs/manager.d b/source/ecs/manager.d index ffea30a..cee24f2 100644 --- a/source/ecs/manager.d +++ b/source/ecs/manager.d @@ -61,7 +61,7 @@ class EntityManager id_manager.initialize(); - allocator = BlockAllocator(page_size,pages_in_block); + allocator = BlockAllocator(page_size, pages_in_block); //add_mutex = Mallocator.instance.make!Mutex; entity_block_alloc_mutex = Mallocator.instance.make!Mutex; @@ -72,13 +72,13 @@ class EntityManager pass.name = Mallocator.instance.makeArray("update"); passes.add(pass); - passes_map.add(cast(string)pass.name, cast(ushort)(passes.length - 1)); + passes_map.add(cast(string) pass.name, cast(ushort)(passes.length - 1)); } /************************************************************************************************************************ *Same as "void registerSystem(Sys)(int priority, int pass = 0)" but use pass name instead of id. */ - void registerSystem(Sys)(int priority, const (char)[] pass_name) + void registerSystem(Sys)(int priority, const(char)[] pass_name) { ushort pass = passes_map.get(pass_name, ushort.max); assert(pass != ushort.max); @@ -441,29 +441,35 @@ class EntityManager system.m_update = &callUpdate; } - static string catchFunc()(string member, string func) + static string catchFunc(RetType = void)(string member, string func) { - string ret = "static if (hasMember!(Sys, \"" ~ func ~ "\")) + //dfmt off + static if(is(RetType == void))string ret_str = ""; + else string ret_str = "return "; + string ret = "static if (hasMember!(Sys, \"" ~ func ~ "\") && + Parameters!(Sys."~func~").length == 0 && + is(ReturnType!(Sys."~func~") == "~RetType.stringof~")) { - static void call" ~ func + static "~RetType.stringof~" call" ~ func ~ "(void* system_pointer) { Sys* s = cast(Sys*) system_pointer; - s." ~ func ~ "(); + "~ret_str~"s." ~ func ~ "(); } system." ~ member ~ " = &call" ~ func ~ "; }"; return ret; + //dfmt on } mixin(catchFunc("m_enable", "onEnable")); mixin(catchFunc("m_disable", "onDisable")); mixin(catchFunc("m_create", "onCreate")); mixin(catchFunc("m_destroy", "onDestroy")); - mixin(catchFunc("m_begin", "onBegin")); + mixin(catchFunc!(bool)("m_begin", "onBegin")); mixin(catchFunc("m_end", "onEnd")); system.m_system_pointer = cast(void*) Mallocator.instance.make!Sys; @@ -544,12 +550,12 @@ class EntityManager return cast(Sys*) systems[Sys.system_id].m_system_pointer; } - ushort registerPass(const (char)[] name) + ushort registerPass(const(char)[] name) { UpdatePass* pass = Mallocator.instance.make!UpdatePass; pass.name = Mallocator.instance.makeArray(name); passes.add(pass); - passes_map.add(name,cast(ushort)(passes.length - 1)); + passes_map.add(name, cast(ushort)(passes.length - 1)); return cast(ushort)(passes.length - 1); } @@ -592,7 +598,7 @@ class EntityManager { components.add(info); Comp.component_id = cast(ushort)(components.length - 1); - const (char)[] name = Mallocator.instance.makeArray(Comp.stringof); + const(char)[] name = Mallocator.instance.makeArray(Comp.stringof); components_map.add(name, cast(ushort)(components.length - 1)); } } @@ -636,7 +642,7 @@ class EntityManager /************************************************************************************************************************ *Same as "void update(int pass = 0)" but use pass name instead of id. */ - void update(const (char)[] pass_name) + void update(const(char)[] pass_name) { ushort pass = passes_map.get(pass_name, ushort.max); assert(pass != ushort.max); @@ -654,7 +660,7 @@ class EntityManager foreach (caller; passes[pass].system_callers) { System* sys = &systems[caller.system_id]; - if (sys.enabled) + if (sys.enabled && sys.execute) { //if (sys.m_begin) // sys.m_begin(sys.m_system_pointer); @@ -686,7 +692,7 @@ class EntityManager /************************************************************************************************************************ *Same as "void updateMT(int pass = 0)" but use pass name instead of id. */ - void updateMT(const (char)[] pass_name) + void updateMT(const(char)[] pass_name) { ushort pass = passes_map.get(pass_name, ushort.max); assert(pass != ushort.max); @@ -704,7 +710,7 @@ class EntityManager foreach (caller; passes[pass].system_callers) { System* sys = &systems[caller.system_id]; - if (sys.enabled) + if (sys.enabled && sys.execute) { uint entities_count = 0; foreach (info; caller.infos) @@ -1668,10 +1674,10 @@ class EntityManager commit(); m_call_data_allocator.clear(); - foreach (ref system; instance.systems) + foreach (ref system; systems) { - if (system.m_begin) - (cast(void function(void*)) system.m_begin)(system.m_system_pointer); + if (system.enabled && system.m_begin) + system.execute = (cast(bool function(void*)) system.m_begin)(system.m_system_pointer); } } @@ -1680,10 +1686,10 @@ class EntityManager */ export void end() { - - foreach (ref system; instance.systems) + + foreach (ref system; systems) { - if (system.m_end) + if (system.enabled && system.m_end) (cast(void function(void*)) system.m_end)(system.m_system_pointer); } @@ -1703,7 +1709,7 @@ class EntityManager /*private */ void generateDependencies() { - foreach(pass_id,pass;passes) + foreach (pass_id, pass; passes) { foreach (caller; pass.system_callers) { @@ -1715,8 +1721,8 @@ class EntityManager } uint index = 0; SystemCaller*[] exclusion; - exclusion = (cast(SystemCaller**) alloca((SystemCaller*).sizeof * pass.system_callers.length))[0 - .. pass.system_callers.length]; + exclusion = (cast(SystemCaller**) alloca((SystemCaller*) + .sizeof * pass.system_callers.length))[0 .. pass.system_callers.length]; foreach (caller; pass.system_callers) { index = 0; @@ -1787,8 +1793,8 @@ class EntityManager return 1; } - qsort(pass.system_callers.array.ptr, pass.system_callers.length, (SystemCaller*) - .sizeof, &compareSystems); + qsort(pass.system_callers.array.ptr, pass.system_callers.length, + (SystemCaller*).sizeof, &compareSystems); /*static struct CallerData { @@ -1830,7 +1836,8 @@ class EntityManager if (index > 0) { caller.dependencies = Mallocator.instance.makeArray(exclusion[0 .. index]); - caller.job_group.dependencies = Mallocator.instance.makeArray!(JobGroup*)(index); + caller.job_group.dependencies = Mallocator.instance.makeArray!( + JobGroup*)(index); foreach (j, dep; caller.dependencies) { @@ -2015,8 +2022,10 @@ class EntityManager ~this() { assert(name); - if(name)Mallocator.instance.dispose(name); + if (name) + Mallocator.instance.dispose(name); } + char[] name; Vector!(SystemCaller*) system_callers; } @@ -2041,7 +2050,7 @@ class EntityManager enum pages_in_block = 128; IDManager id_manager; - BlockAllocator/*!(page_size, pages_in_block)*/ allocator; + BlockAllocator /*!(page_size, pages_in_block)*/ allocator; //EventManager event_manager; mixin EventManagerCode; diff --git a/source/ecs/system.d b/source/ecs/system.d index 84d9862..2787c68 100644 --- a/source/ecs/system.d +++ b/source/ecs/system.d @@ -47,6 +47,9 @@ struct System package: + ///should system be executed in current update? + bool execute = true; + ///should system update and catch events? bool m_enabled = false; ///system priority diff --git a/tests/tests.d b/tests/tests.d index 22753eb..a217b91 100644 --- a/tests/tests.d +++ b/tests/tests.d @@ -111,9 +111,10 @@ int main() writeln("On Test System destroy."); } - void onBegin() + bool onBegin() { //writeln("On Test System begin."); + return true; } void onEnd()