diff --git a/source/ecs/manager.d b/source/ecs/manager.d index cd3c00f..d04a516 100644 --- a/source/ecs/manager.d +++ b/source/ecs/manager.d @@ -84,9 +84,10 @@ class EntityManager assert(register_state, "beginRegister() should be called before endRegister();"); register_state = false; - foreach(ref info; &entities_infos.byValue) + foreach (ref info; &entities_infos.byValue) { - if(info.systems)Mallocator.instance.dispose(info.systems); + if (info.systems) + Mallocator.instance.dispose(info.systems); info.systems = Mallocator.instance.makeArray!bool(systems.length); } @@ -94,11 +95,11 @@ class EntityManager { if (system.m_update is null) { - if(system.m_entity_added || system.m_entity_removed) + if (system.m_entity_added || system.m_entity_removed) { foreach (info; &entities_infos.byValue) { - connectListenerToEntityInfo(*info,system.id); + connectListenerToEntityInfo(*info, system.id); } } continue; @@ -134,32 +135,35 @@ class EntityManager event_manager.allocateData(cast(uint) threads.length); - foreach(ref info;events) + foreach (ref info; events) { Mallocator.instance.dispose(info.callers); } - ushort[] event_callers = (cast(ushort*)alloca(ushort.sizeof * events.length))[0..events.length]; - foreach(ref caller; event_callers)caller = 0; + ushort[] event_callers = (cast(ushort*) alloca(ushort.sizeof * events.length))[0 + .. events.length]; + foreach (ref caller; event_callers) + caller = 0; - foreach(ref system;systems) + foreach (ref system; systems) { - foreach(caller;system.m_event_callers) + foreach (caller; system.m_event_callers) { event_callers[caller.id]++; } } - foreach(i,ref info; events) + foreach (i, ref info; events) { info.callers = Mallocator.instance.makeArray!(EventCaller)(event_callers[i]); } - foreach(ref caller; event_callers)caller = 0; + foreach (ref caller; event_callers) + caller = 0; - foreach(ref system;systems) + foreach (ref system; systems) { - foreach(caller;system.m_event_callers) + foreach (caller; system.m_event_callers) { events[caller.id].callers[event_callers[caller.id]].callback = caller.callback; events[caller.id].callers[event_callers[caller.id]].system = &system; @@ -167,7 +171,7 @@ class EntityManager } } - foreach(info; &entities_infos.byValue) + foreach (info; &entities_infos.byValue) { generateListeners(info); } @@ -554,8 +558,8 @@ class EntityManager ret ~= "system.m_read_only_components[" ~ (read_only++) .to!string ~ "] = comp;"; else - ret ~= "system.m_modified_components[" ~ (modified++) - .to!string ~ "] = comp;"; + ret ~= "system.m_modified_components[" ~ (modified++) + .to!string ~ "] = comp;"; ret ~= "}"; } } @@ -640,8 +644,8 @@ class EntityManager //pragma(msg,genFillInputData); - static void fillInputData(ref Sys.EntitiesData input_data, EntityInfo* info, EntitiesBlock* block, - uint offset, uint entities_count, System* system) + static void fillInputData(ref Sys.EntitiesData input_data, EntityInfo* info, + EntitiesBlock* block, uint offset, uint entities_count, System* system) { mixin(genFillInputData()); } @@ -755,8 +759,8 @@ class EntityManager mixin(catchFunc!(bool)("m_begin", "onBegin")); mixin(catchFunc("m_end", "onEnd")); - mixin(catchEntityFunc("m_entity_added","onAdd")); - mixin(catchEntityFunc("m_entity_removed","onRemove")); + mixin(catchEntityFunc("m_entity_added", "onAdd")); + mixin(catchEntityFunc("m_entity_removed", "onRemove")); system.m_system_pointer = cast(void*) Mallocator.instance.make!Sys; system.m_priority = priority; @@ -845,8 +849,7 @@ class EntityManager } static if (hasMember!(Comp, "onCreate") && isFunction!(Comp.onCreate) - && is(ReturnType!(Comp.onCreate) == void) - && Parameters!(Comp.onCreate).length == 0) + && is(ReturnType!(Comp.onCreate) == void) && Parameters!(Comp.onCreate).length == 0) { static void callCreate(void* pointer) nothrow @nogc { @@ -1077,7 +1080,7 @@ class EntityManager nextJob(); caller.job_group.jobs = sys.jobs[0 .. job_id]; - (cast(void delegate(JobGroup) nothrow @nogc)m_dispatch_jobs)(caller.job_group); //sys.jobs[0 .. job_id]); + (cast(void delegate(JobGroup) nothrow @nogc) m_dispatch_jobs)(caller.job_group); //sys.jobs[0 .. job_id]); } } } @@ -1210,10 +1213,11 @@ class EntityManager //if(system.m_entity_added || system.m_entity_removed)info.systems[system.id] = true; if (system.m_update is null) { - if(system.m_entity_added || system.m_entity_removed)connectListenerToEntityInfo(*info,cast(uint)i); + if (system.m_entity_added || system.m_entity_removed) + connectListenerToEntityInfo(*info, cast(uint) i); continue; } - addSystemCaller(*info, cast(uint)i); + addSystemCaller(*info, cast(uint) i); } entities_infos.add(info.components, info); @@ -1225,76 +1229,80 @@ class EntityManager void generateListeners(EntityInfo* info) { - if(info.add_listeners) + if (info.add_listeners) { Mallocator.instance.dispose(info.add_listeners); info.add_listeners = null; } - if(info.remove_listeners) + if (info.remove_listeners) { Mallocator.instance.dispose(info.remove_listeners); info.remove_listeners = null; } //allocate local data - ushort[] tmp_add = (cast(ushort*)alloca(systems.length*ushort.sizeof))[0..systems.length]; - ushort[] tmp_rem = (cast(ushort*)alloca(systems.length*ushort.sizeof))[0..systems.length]; + ushort[] tmp_add = (cast(ushort*) alloca(systems.length * ushort.sizeof))[0 + .. systems.length]; + ushort[] tmp_rem = (cast(ushort*) alloca(systems.length * ushort.sizeof))[0 + .. systems.length]; int add_len = 0; int rem_len = 0; //assign listeners to lists - foreach(i;0..systems.length) + foreach (i; 0 .. systems.length) { - if(info.systems[i]) + if (info.systems[i]) { System* system = &systems[i]; //onAddEntity listener - if(system.m_entity_added) + if (system.m_entity_added) { //find listener position by priority int j; - for(j=0;j systems[tmp_add[j]].priority)break; + if (systems[i].priority > systems[tmp_add[j]].priority) + break; } add_len++; //move elements after new listener - for(int k=add_len;k>j;k--) + for (int k = add_len; k > j; k--) { - tmp_add[k] = tmp_add[k-1]; + tmp_add[k] = tmp_add[k - 1]; } //assign listener - tmp_add[j] = cast(ushort)i; + tmp_add[j] = cast(ushort) i; } //onRemoveEntity listener - if(system.m_entity_removed) + if (system.m_entity_removed) { //find listener position by priority int j; - for(j=0;j systems[tmp_rem[j]].priority)break; + if (systems[i].priority > systems[tmp_rem[j]].priority) + break; } rem_len++; //move elements after new listener - for(int k=rem_len;k>j;k--) + for (int k = rem_len; k > j; k--) { - tmp_rem[k] = tmp_rem[k-1]; + tmp_rem[k] = tmp_rem[k - 1]; } //assign listener - tmp_rem[j] = cast(ushort)i; + tmp_rem[j] = cast(ushort) i; } } } - - if(add_len) + + if (add_len) { info.add_listeners = Mallocator.instance.makeArray!ushort(add_len); - memcpy(info.add_listeners.ptr,tmp_add.ptr,add_len*ushort.sizeof); + memcpy(info.add_listeners.ptr, tmp_add.ptr, add_len * ushort.sizeof); } - if(rem_len) + if (rem_len) { info.remove_listeners = Mallocator.instance.makeArray!ushort(rem_len); - memcpy(info.remove_listeners.ptr,tmp_rem.ptr,rem_len*ushort.sizeof); + memcpy(info.remove_listeners.ptr, tmp_rem.ptr, rem_len * ushort.sizeof); } } @@ -1368,7 +1376,7 @@ class EntityManager { passes[system.m_pass].system_callers[index].infos.add(&info); } - + } /************************************************************************************************************************ @@ -1448,13 +1456,13 @@ class EntityManager else uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) / EntityID.sizeof()); - if(info.remove_listeners) + if (info.remove_listeners) { - foreach(listener;info.remove_listeners) + foreach (listener; info.remove_listeners) { - if(!new_info.systems[listener]) + if (!new_info.systems[listener]) { - callRemoveEntityListener(&systems[listener],info,block,ind,ind+1); + callRemoveEntityListener(&systems[listener], info, block, ind, ind + 1); } } } @@ -1466,13 +1474,14 @@ class EntityManager cast(void*) block + info.deltas[comp] + ind * comp_size, comp_size); } - if(new_info.add_listeners) + if (new_info.add_listeners) { - foreach(listener;new_info.add_listeners) + foreach (listener; new_info.add_listeners) { - if(!info.systems[listener]) + if (!info.systems[listener]) { - callAddEntityListener(&systems[listener],new_info,new_block,new_block.entities_count,new_block.entities_count+1); + callAddEntityListener(&systems[listener], new_info, new_block, + new_block.entities_count, new_block.entities_count + 1); } } } @@ -1601,13 +1610,13 @@ class EntityManager else uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) / EntityID.sizeof()); - if(info.remove_listeners) + if (info.remove_listeners) { - foreach(listener;info.remove_listeners) + foreach (listener; info.remove_listeners) { - if(!new_info.systems[listener]) + if (!new_info.systems[listener]) { - callRemoveEntityListener(&systems[listener],info,block,ind,ind+1); + callRemoveEntityListener(&systems[listener], info, block, ind, ind + 1); } } } @@ -1639,13 +1648,14 @@ class EntityManager } } - if(new_info.add_listeners) + if (new_info.add_listeners) { - foreach(listener;new_info.add_listeners) + foreach (listener; new_info.add_listeners) { - if(!info.systems[listener]) + if (!info.systems[listener]) { - callAddEntityListener(&systems[listener],new_info,new_block,new_block.entities_count,new_block.entities_count+1); + callAddEntityListener(&systems[listener], new_info, new_block, + new_block.entities_count, new_block.entities_count + 1); } } } @@ -1738,8 +1748,8 @@ class EntityManager if (components[comp].create_callback) { - components[comp].create_callback(cast( - void*) block + info.deltas[comp] + id * components[comp].size); + components[comp].create_callback( + cast(void*) block + info.deltas[comp] + id * components[comp].size); } } @@ -1846,9 +1856,9 @@ class EntityManager if (entity is null) return; //return if entity doesn't exist EntitiesBlock* block = getMetaData(entity); - + EntityInfo* info = block.type_info; - if(info.remove_listeners) + if (info.remove_listeners) { void* data_begin = block.dataBegin(); static if (EntityID.sizeof == 8) @@ -1856,7 +1866,7 @@ class EntityManager else uint pos = cast(uint)((cast(void*) entity - data_begin) / EntityID.sizeof()); - callRemoveEntityListeners(info,block,pos,pos+1); + callRemoveEntityListeners(info, block, pos, pos + 1); } id_manager.releaseID(id); //release id from manager @@ -1977,40 +1987,43 @@ class EntityManager private void callAddEntityListeners(EntityInfo* info, EntitiesBlock* block, int begin, int end) @nogc nothrow { - foreach(listener;info.add_listeners) + foreach (listener; info.add_listeners) { System* system = &systems[listener]; - callAddEntityListener(system,info,block,begin,end); + callAddEntityListener(system, info, block, begin, end); } } - private void callAddEntityListener(System* system, EntityInfo* info, EntitiesBlock* block, int begin, int end) @nogc nothrow + private void callAddEntityListener(System* system, EntityInfo* info, + EntitiesBlock* block, int begin, int end) @nogc nothrow { ListenerCallData data; data.system = system; data.block = block; data.begin = begin; data.end = end; - (cast(void function (ref ListenerCallData) nothrow @nogc) system.m_entity_added)(data); + (cast(void function(ref ListenerCallData) nothrow @nogc) system.m_entity_added)(data); } - private void callRemoveEntityListeners(EntityInfo* info, EntitiesBlock* block, int begin, int end) @nogc nothrow + private void callRemoveEntityListeners(EntityInfo* info, EntitiesBlock* block, int begin, + int end) @nogc nothrow { - foreach(listener;info.remove_listeners) + foreach (listener; info.remove_listeners) { System* system = &systems[listener]; - callRemoveEntityListener(system,info,block,begin,end); + callRemoveEntityListener(system, info, block, begin, end); } } - private void callRemoveEntityListener(System* system, EntityInfo* info, EntitiesBlock* block, int begin, int end) @nogc nothrow + private void callRemoveEntityListener(System* system, EntityInfo* info, + EntitiesBlock* block, int begin, int end) @nogc nothrow { ListenerCallData data; data.system = system; data.block = block; data.begin = begin; data.end = end; - (cast(void function (ref ListenerCallData) nothrow @nogc) system.m_entity_removed)(data); + (cast(void function(ref ListenerCallData) nothrow @nogc) system.m_entity_removed)(data); } private void updateBlocks() @@ -2028,11 +2041,11 @@ class EntityManager } block.added_count.atomicStore(cast(ushort) 0); - if(info.add_listeners) + if (info.add_listeners) { - callAddEntityListeners(info,block,entities_count,block.entities_count); + callAddEntityListeners(info, block, entities_count, block.entities_count); } - + } thread.blocks_to_update.clear(); } @@ -2053,38 +2066,46 @@ class EntityManager void updateEvents() nothrow @nogc { bool empty = true; - while(1) + while (1) { event_manager.swapCurrent(); uint current_index; - if(event_manager.current_index == 0)current_index = cast(uint)threads.length; - else current_index = 0; - foreach(i,event;event_manager.events) + if (event_manager.current_index == 0) + current_index = cast(uint) threads.length; + else + current_index = 0; + foreach (i, event; event_manager.events) { - foreach(first_block;event.first_blocks[current_index .. current_index + threads.length]) + foreach (first_block; event.first_blocks[current_index + .. current_index + threads.length]) { EventManager.EventBlock* block = first_block; - if(block)empty = false; - while(block) + if (block) + empty = false; + while (block) { EventCallData call_data; - void* event_pointer = cast(void*)block + event.data_offset; + void* event_pointer = cast(void*) block + event.data_offset; call_data.event = event_pointer; - foreach(j;0..block.count) + foreach (j; 0 .. block.count) { - EntityID entity_id = *cast(EntityID*)event_pointer; + EntityID entity_id = *cast(EntityID*) event_pointer; Entity* entity = id_manager.getEntityPointer(entity_id); call_data.block = getMetaData(entity); static if (EntityID.sizeof == 8) - call_data.id = cast(ushort)((cast(void*)entity - call_data.block.dataBegin()) >> 3); + call_data.id = cast(ushort)( + (cast(void*) entity - call_data.block.dataBegin()) >> 3); else - call_data.id = cast(ushort)((cast(void*)entity - call_data.block.dataBegin()) / EntityID.sizeof); - - foreach(caller; events[i].callers) + call_data.id = cast(ushort)( + (cast(void*) entity - call_data.block.dataBegin()) / EntityID + .sizeof); + + foreach (caller; events[i].callers) { call_data.system_pointer = caller.system.m_system_pointer; - (cast(void function(ref EventCallData) nothrow @nogc)caller.callback)(call_data); + (cast(void function(ref EventCallData) nothrow @nogc) caller + .callback)(call_data); } event_pointer += events[i].size; } @@ -2092,14 +2113,15 @@ class EntityManager } } } - if(empty)break; + if (empty) + break; empty = true; } } export void commit() { - updateEvents(); + updateEvents(); id_manager.optimize(); updateBlocks(); removeEntities(); @@ -2142,7 +2164,7 @@ class EntityManager private void getThreadID() nothrow @nogc { if (m_thread_id_func) - thread_id = (cast(uint delegate() nothrow @nogc)m_thread_id_func)(); + thread_id = (cast(uint delegate() nothrow @nogc) m_thread_id_func)(); else thread_id = 0; } @@ -2243,7 +2265,7 @@ class EntityManager (SystemCaller*).sizeof, &compareSystems); foreach (i, caller; pass.system_callers) - caller.job_group.id = cast(uint)i; + caller.job_group.id = cast(uint) i; int priority = int.min; uint beg = 0; diff --git a/source/ecs/system.d b/source/ecs/system.d index 747f85c..9b377cf 100644 --- a/source/ecs/system.d +++ b/source/ecs/system.d @@ -36,7 +36,7 @@ struct System export void enable() nothrow @nogc { if (!m_enabled && m_enable) - (cast(void function(void*) nothrow @nogc)m_enable)(m_system_pointer); + (cast(void function(void*) nothrow @nogc) m_enable)(m_system_pointer); m_enabled = true; } @@ -46,7 +46,7 @@ struct System export void disable() nothrow @nogc { if (m_enabled && m_disable) - (cast(void function(void*) nothrow @nogc)m_disable)(m_system_pointer); + (cast(void function(void*) nothrow @nogc) m_disable)(m_system_pointer); m_enabled = false; } @@ -97,7 +97,7 @@ package: int m_pass; ///system name - const (char)[] name; + const(char)[] name; ///required components ushort[] m_components; @@ -140,6 +140,6 @@ package: //void function(ref EntityManager.CallData data) m_initialize; //void function(ref EntityManager.CallData data) m_deinitilize; - void* m_initialize; - void* m_deinitilize; + void* m_initialize; + void* m_deinitilize; }