diff --git a/source/ecs/id_manager.d b/source/ecs/id_manager.d index c5b521b..60f9d5c 100644 --- a/source/ecs/id_manager.d +++ b/source/ecs/id_manager.d @@ -11,7 +11,7 @@ struct IDManager m_ids_array.add(Data()); EntityID id; id.id = m_next_id; - id.counter = ++m_ids_array[m_next_id].counter; + id.counter = /*++*/m_ids_array[m_next_id].counter; m_next_id = m_ids_array[m_next_id].next_id; if (m_next_id == uint.max) m_next_id = cast(uint) m_ids_array.length; @@ -23,6 +23,7 @@ struct IDManager Data* data = &m_ids_array[id.id]; if (data.counter != id.counter) return; + data.counter++; data.next_id = m_next_id; data.entity = null; m_next_id = id.id; diff --git a/source/ecs/manager.d b/source/ecs/manager.d index 8477254..4cdb8e7 100644 --- a/source/ecs/manager.d +++ b/source/ecs/manager.d @@ -118,10 +118,10 @@ class EntityManager foreach (i; 1 .. (Parameters!(Sys.update)).length) { ret ~= " - static if(isPointer!(types[" ~ i.to!string - ~ "])) + static if(isPointer!(types[" ~ i.to!string ~ "])) { - comp = components_map.get(PointerTarget!(types[" ~ i.to!string ~ "]).stringof, ushort.max);\n + comp = components_map.get(PointerTarget!(types[" + ~ i.to!string ~ "]).stringof, ushort.max);\n if(comp == ushort.max)assert(0,\"Can't register system \\\"" ~ Sys.stringof ~ "\\\" due to non existing component \\\"\"~types[" ~ i.to!string ~ "].stringof~\"\\\".\"); system.m_optional_components[opt++] = comp; @@ -220,7 +220,7 @@ class EntityManager { mixin(genCompList()); } - + ushort sys_id = systems_map.get(Sys.stringof, ushort.max); if (sys_id < systems.length) { @@ -257,7 +257,7 @@ class EntityManager Sys* getSystem(Sys)() { - return cast(Sys*)systems[Sys.system_id].system_pointer; + return cast(Sys*) systems[Sys.system_id].system_pointer; } void registerComponent(Comp)() @@ -513,6 +513,7 @@ class EntityManager private void __removeComponents(EntityID entity_id, ushort[] del_ids) { Entity* entity = id_manager.getEntityPointer(entity_id); + if(!entity)return; EntitiesBlock* block = getMetaData(entity); EntityInfo* info = block.type_data; @@ -583,6 +584,7 @@ class EntityManager { uint num = cast(uint) new_ids.length; Entity* entity = id_manager.getEntityPointer(entity_id); + if(!entity)return; EntitiesBlock* block = getMetaData(entity); EntityInfo* info = block.type_data; ushort[] ids = (cast(ushort*) alloca(ushort.sizeof * (info.components.length + num)))[0 @@ -774,6 +776,7 @@ class EntityManager else { previous_block.next_block = block; + block.prev_block = previous_block; block.id = previous_block.id + 1; } info.first_with_free_space = block; @@ -837,6 +840,28 @@ class EntityManager } } + if (block.entities_count == 0) + { + if (block.type_data.first_block is block) + { + block.type_data.first_block = block.next_block; + } + if (block.type_data.first_with_free_space is block) + { + block.type_data.first_with_free_space = block.next_block;//block.type_data.first_block; + } + if (block.prev_block) + { + block.prev_block.next_block = block.next_block; + } + if(block.next_block) + { + block.next_block.prev_block = block.prev_block; + } + allocator.freeBlock(block); + return; + } + if (pos == block.entities_count) return; @@ -922,8 +947,8 @@ class EntityManager export void begin() { updateBlocks(); - changeEntites(); removeEntities(); + changeEntites(); foreach (ref system; instance.systems) { if (system.m_begin) @@ -939,8 +964,8 @@ class EntityManager system.m_end(system.m_system_pointer); } updateBlocks(); - changeEntites(); removeEntities(); + changeEntites(); clearEvents(); } @@ -1003,15 +1028,17 @@ class EntityManager } ///pointer to Entity type info - EntityInfo* type_data; + EntityInfo* type_data = null; ///number of entities in block - ushort entities_count; + ushort entities_count = 0; ///number of new entities in block - ushort added_count; + ushort added_count = 0; ///block id - uint id; + uint id = 0; ///pointer to next block/page - EntitiesBlock* next_block; + EntitiesBlock* next_block = null; + ///pointer to next block/page + EntitiesBlock* prev_block = null; //there is a loooot of data (4kB, pure magic) } diff --git a/tests/tests.d b/tests/tests.d index 8c778d7..c4840ba 100644 --- a/tests/tests.d +++ b/tests/tests.d @@ -93,6 +93,7 @@ int main() struct TestSystem { + __gshared ushort system_id; void onCreate() { @@ -152,6 +153,7 @@ int main() struct TestSystemWithHighPriority { + __gshared ushort system_id; void initialize(ref Entity entity, ref TestComp comp) { @@ -173,6 +175,7 @@ int main() struct TestSystem2 { + __gshared ushort system_id; void onEnable() {