From 19687b9f88fca07efa70568941feba96a99ed13f Mon Sep 17 00:00:00 2001 From: Mergul Date: Sat, 4 Apr 2020 13:58:31 +0200 Subject: [PATCH] -fixed critical bug with adding components -some fixes -minor optimization -added debug assert test --- source/ecs/core.d | 6 +++--- source/ecs/manager.d | 42 ++++++++++++++++++++++++++---------------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/source/ecs/core.d b/source/ecs/core.d index dde39a5..91c9065 100644 --- a/source/ecs/core.d +++ b/source/ecs/core.d @@ -45,7 +45,7 @@ static struct ECS */ mixin template System(uint jobs_count = 32) { - __gshared ushort system_id; + __gshared ushort system_id = ushort.max; uint __ecs_jobs_count = jobs_count; } @@ -54,7 +54,7 @@ static struct ECS */ mixin template Component() { - __gshared ushort component_id; + __gshared ushort component_id = ushort.max; } /************************************************************************************************************************ @@ -62,7 +62,7 @@ static struct ECS */ mixin template Event() { - __gshared ushort event_id; + __gshared ushort event_id = ushort.max; EntityID entity_id; } diff --git a/source/ecs/manager.d b/source/ecs/manager.d index ae94cf7..0e91095 100644 --- a/source/ecs/manager.d +++ b/source/ecs/manager.d @@ -770,13 +770,15 @@ export struct EntityManager else enum OnUpdateOverloadNum = -1; //enum HasOnUpdate = (hasMember!(Sys, "onUpdate") && checkOnUpdateParams()); + enum IsEmpty = components_info.req.length == 0 && components_info.optional.length == 0 + && components_info.excluded.length == 0 && components_info.entites_array.length == 0; - static if (components_info.req.length == 0 && components_info.optional.length == 0) + static if (IsEmpty) system.m_empty = true; static if (OnUpdateOverloadNum != -1) { - static if (components_info.req.length != 0 || components_info.optional.length != 0) + static if (!IsEmpty) { static void callUpdate(ref CallData data) { @@ -972,6 +974,8 @@ export struct EntityManager */ export System* getSystem(ushort id) nothrow @nogc { + if (id >= systems.length) + return null; return &systems[id]; } @@ -980,6 +984,8 @@ export struct EntityManager */ Sys* getSystem(Sys)() nothrow @nogc { + if (Sys.system_id >= systems.length) + return null; return cast(Sys*) systems[Sys.system_id].m_system_pointer; } @@ -1404,6 +1410,7 @@ export struct EntityManager uint j = 1; foreach (i; 1 .. ids.length) { + assert(ids[i] != ushort.max); if (ids[i] != ids[j - 1]) { ids[j] = ids[i]; @@ -1500,9 +1507,10 @@ export struct EntityManager } info.comp_add_info = Mallocator.makeArray!(EntityInfo*)(instance.components.length); - info.comp_rem_info = Mallocator.makeArray!(EntityInfo*)(instance.components.length, info); + info.comp_rem_info = Mallocator.makeArray!(EntityInfo*)(instance.components.length, + info); - foreach(comp; info.components) + foreach (comp; info.components) { info.comp_add_info[comp] = info; info.comp_rem_info[comp] = null; @@ -1772,17 +1780,18 @@ export struct EntityManager EntityInfo* info = block.type_info; //remove non-existing components - uint num = cast(uint)del_ids.length; - foreach_reverse(i; 0 .. num) + uint num = cast(uint) del_ids.length; + foreach_reverse (i; 0 .. num) { - if(info.deltas.length <= del_ids[i] || info.deltas[del_ids[i]] == 0) + if (info.deltas.length <= del_ids[i] || info.deltas[del_ids[i]] == 0) { num--; del_ids[i] = del_ids[num]; } } - - if(num == 0)return; + + if (num == 0) + return; del_ids = del_ids[0 .. num]; //sort components @@ -1883,16 +1892,17 @@ export struct EntityManager EntitiesBlock* block = getMetaData(entity); EntityInfo* info = block.type_info; - foreach_reverse(i; 0 .. num) + foreach_reverse (i; 0 .. num) { - if(info.deltas.length > new_ids[i] && info.deltas[new_ids[i]] != 0) + if (info.deltas.length > new_ids[i] && info.deltas[new_ids[i]] != 0) { num--; new_ids[i] = new_ids[num]; } } - - if(num == 0)return; + + if (num == 0) + return; new_ids = new_ids[0 .. num]; foreach (int i; 0 .. num) @@ -1924,7 +1934,7 @@ export struct EntityManager { new_info = new_info.getNewInfoAdd(id); } - + assert(new_info != info); /*if (new_info == info) return;*/ @@ -2825,7 +2835,7 @@ export struct EntityManager foreach (comp2; components[len - 1 .. $]) { ids[len++] = comp2; - } + } break; } } @@ -2834,7 +2844,7 @@ export struct EntityManager assert(len == components.length + 1); - EntityInfo* new_info = instance.getEntityInfo(ids);//[0 .. len]); + EntityInfo* new_info = instance.getEntityInfo(ids); comp_add_info[id] = new_info; return new_info;