diff --git a/source/ecs/entity.d b/source/ecs/entity.d index 11f7354..f267853 100644 --- a/source/ecs/entity.d +++ b/source/ecs/entity.d @@ -45,6 +45,7 @@ export struct EntityTemplate T* getComponent(T)() { - return cast(T*)(entity_data.ptr + info.deltas[T.component_id]); + version(LinearLayout)return cast(T*)(entity_data.ptr + info.tmpl_deltas[T.component_id]); + else return cast(T*)(entity_data.ptr + info.deltas[T.component_id]); } } diff --git a/source/ecs/id_manager.d b/source/ecs/id_manager.d index 60f9d5c..86542e3 100644 --- a/source/ecs/id_manager.d +++ b/source/ecs/id_manager.d @@ -31,7 +31,7 @@ struct IDManager void update(ref Entity entity) { - m_ids_array[entity.id.id].entity = &entity; + if(entity.id.counter == m_ids_array[entity.id.id].counter)m_ids_array[entity.id.id].entity = &entity; } export Entity* getEntityPointer(EntityID id) diff --git a/source/ecs/manager.d b/source/ecs/manager.d index 3b89943..c637768 100644 --- a/source/ecs/manager.d +++ b/source/ecs/manager.d @@ -156,7 +156,9 @@ class EntityManager } else { - ret ~= "types[" ~ i.to!string ~ "][] array" ~ req.to!string ~ " = (cast(types[" ~ i.to!string ~ "]*)(cast(void*)block + info.deltas[types[" ~ i.to!string ~ "].component_id]))[0..block.entities_count];"; + ret ~= "types[" ~ i.to!string ~ "][] array" ~ req.to!string ~ " = (cast(types[" + ~ i.to!string ~ "]*)(cast(void*)block + info.deltas[types[" + ~ i.to!string ~ "].component_id]))[0..block.entities_count];"; req++; } } @@ -235,7 +237,8 @@ class EntityManager pointer = null; }*/ EntityInfo* info = block.type_info; - Entity[] id_array = (cast(Entity*)block.dataBegin())[0..block.entities_count]; + Entity[] id_array = (cast(Entity*) block.dataBegin())[0 + .. block.entities_count]; mixin(genArrays()); foreach (i; 0 .. block.entities_count) { @@ -710,10 +713,26 @@ class EntityManager new_entity.id = entity.id; new_entity.updateID(); - foreach (comp; new_info.components) + version (LinearLayout) { - memcpy(cast(void*) new_entity + new_info.deltas[comp], - cast(void*) entity + info.deltas[comp], components[comp].size); + static if (EntityID.sizeof == 8) + uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) >> 3); + else + uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) / EntityID.sizeof()); + foreach (comp; new_info.components) + { + uint comp_size = components[comp].size; + memcpy(cast(void*) new_block + new_info.deltas[comp] + new_block.entities_count * comp_size, + cast(void*) block + info.deltas[comp] + ind * comp_size, comp_size); + } + } + else + { + foreach (comp; new_info.components) + { + memcpy(cast(void*) new_entity + new_info.deltas[comp], + cast(void*) entity + info.deltas[comp], components[comp].size); + } } new_block.entities_count++; @@ -817,42 +836,74 @@ class EntityManager Entity* new_entity = cast(Entity*) start; new_entity.id = entity.id; new_entity.updateID(); - new_block.entities_count++; //removeEntityNoID(entity, block); j = 0; k = 0; - foreach (ref id; ids) + version (LinearLayout) { - if (k >= new_ids.length) - { - memcpy(cast(void*) new_entity + new_info.deltas[id], - cast(void*) entity + info.deltas[info.components[j]], - components[info.components[j]].size); //id = info.components[j++]; - j++; - } - else if (j >= info.components.length) - { - memcpy(cast(void*) new_entity + new_info.deltas[id], - data_pointers[k], components[new_ids[k]].size); //id = new_ids[k++]; - k++; - } - else if (id == new_ids[k]) - { - memcpy(cast(void*) new_entity + new_info.deltas[id], - data_pointers[k], components[new_ids[k]].size); //id = new_ids[k++]; - k++; - } + static if (EntityID.sizeof == 8) + uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) >> 3); else + uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) / EntityID.sizeof()); + foreach (ref id; ids) { - memcpy(cast(void*) new_entity + new_info.deltas[id], - cast(void*) entity + info.deltas[info.components[j]], - components[info.components[j]].size); //id = info.components[j++]; - j++; + void* dst = cast(void*) new_block + new_info.deltas[id] + ( + new_block.entities_count + new_block.added_count) * components[id].size; + uint size = components[id].size; + if (k >= new_ids.length) + { + memcpy(dst, cast(void*) block + info.deltas[id] + ind * size, size); + j++; + } + else if (j >= info.components.length) + { + memcpy(dst, data_pointers[k], size); + k++; + } + else if (id == new_ids[k]) + { + memcpy(dst, data_pointers[k], size); + k++; + } + else + { + memcpy(dst, cast(void*) block + info.deltas[id] + ind * size, size); + j++; + } + } + } + else + { + foreach (ref id; ids) + { + void* dst = cast(void*) new_entity + new_info.deltas[id]; + uint size = components[id].size; + if (k >= new_ids.length) + { + memcpy(dst, cast(void*) entity + info.deltas[id], size); + j++; + } + else if (j >= info.components.length) + { + memcpy(dst, data_pointers[k], size); + k++; + } + else if (id == new_ids[k]) + { + memcpy(dst, data_pointers[k], size); + k++; + } + else + { + memcpy(dst, cast(void*) entity + info.deltas[id], size); + j++; + } } } + new_block.entities_count++; removeEntityNoID(entity, block); } diff --git a/tests/tests.d b/tests/tests.d index 8a21d9d..5fc09f9 100644 --- a/tests/tests.d +++ b/tests/tests.d @@ -79,10 +79,10 @@ int main() __gshared ushort component_id; uint gg = 7; //good game uint bg = 8; //bad game - ulong a; - ulong b; - ulong c; - ulong g; + ulong a = 9; + ulong b = 10; + ulong c = 11; + ulong g = 12; static void serializeComponent(ref TestComp comp, SerializeVector output) { @@ -260,10 +260,12 @@ int main() time = MonoTime.currTime; - ushort[3] ids = [TestComp2.component_id, TestComp.component_id, TestComp4.component_id]; + //ushort[3] ids = [TestComp2.component_id, TestComp.component_id, TestComp4.component_id]; + ushort[2] ids = [TestComp2.component_id, TestComp.component_id]; EntityTemplate* tmpl = gEM.allocateTemplate(ids); - ushort[3] ids2 = [TestComp3.component_id, TestComp.component_id, TestComp4.component_id]; + //ushort[3] ids2 = [TestComp3.component_id, TestComp.component_id, TestComp4.component_id]; + ushort[2] ids2 = [TestComp3.component_id, TestComp.component_id]; EntityTemplate* tmpl2 = gEM.allocateTemplate(ids2); //writeln(tmpl.info.components[]); //*cast(EntityID*) tmpl.entity_data.ptr = EntityID(1, 1); @@ -286,7 +288,7 @@ int main() EntityID[1000] idss; - /*foreach (i; 0 .. 1_000) + foreach (i; 0 .. 1_000) { gEM.begin(); foreach (j; 0 .. 1_000) @@ -294,7 +296,7 @@ int main() foreach (j; 0 .. 1_000) gEM.removeEntity(idss[j]); gEM.end(); - }*/ + } dur = (MonoTime.currTime - time).total!"usecs"; writeln("Entities adding: ", dur, " usecs"); @@ -375,6 +377,7 @@ int main() writeEntityComponents(gEM.getEntity(entity.id)); gEM.removeComponents!(TestComp)(entity.id); + gEM.addComponents(entity.id, TestComp()); gEM.begin(); gEM.update();