-fixed critical bug with adding components

-some fixes
-minor optimization
-added debug assert test
This commit is contained in:
Mergul 2020-04-04 13:58:31 +02:00
parent 913cf1aef8
commit 19687b9f88
2 changed files with 29 additions and 19 deletions

View file

@ -45,7 +45,7 @@ static struct ECS
*/ */
mixin template System(uint jobs_count = 32) mixin template System(uint jobs_count = 32)
{ {
__gshared ushort system_id; __gshared ushort system_id = ushort.max;
uint __ecs_jobs_count = jobs_count; uint __ecs_jobs_count = jobs_count;
} }
@ -54,7 +54,7 @@ static struct ECS
*/ */
mixin template Component() mixin template Component()
{ {
__gshared ushort component_id; __gshared ushort component_id = ushort.max;
} }
/************************************************************************************************************************ /************************************************************************************************************************
@ -62,7 +62,7 @@ static struct ECS
*/ */
mixin template Event() mixin template Event()
{ {
__gshared ushort event_id; __gshared ushort event_id = ushort.max;
EntityID entity_id; EntityID entity_id;
} }

View file

@ -770,13 +770,15 @@ export struct EntityManager
else else
enum OnUpdateOverloadNum = -1; enum OnUpdateOverloadNum = -1;
//enum HasOnUpdate = (hasMember!(Sys, "onUpdate") && checkOnUpdateParams()); //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; system.m_empty = true;
static if (OnUpdateOverloadNum != -1) 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) static void callUpdate(ref CallData data)
{ {
@ -972,6 +974,8 @@ export struct EntityManager
*/ */
export System* getSystem(ushort id) nothrow @nogc export System* getSystem(ushort id) nothrow @nogc
{ {
if (id >= systems.length)
return null;
return &systems[id]; return &systems[id];
} }
@ -980,6 +984,8 @@ export struct EntityManager
*/ */
Sys* getSystem(Sys)() nothrow @nogc Sys* getSystem(Sys)() nothrow @nogc
{ {
if (Sys.system_id >= systems.length)
return null;
return cast(Sys*) systems[Sys.system_id].m_system_pointer; return cast(Sys*) systems[Sys.system_id].m_system_pointer;
} }
@ -1404,6 +1410,7 @@ export struct EntityManager
uint j = 1; uint j = 1;
foreach (i; 1 .. ids.length) foreach (i; 1 .. ids.length)
{ {
assert(ids[i] != ushort.max);
if (ids[i] != ids[j - 1]) if (ids[i] != ids[j - 1])
{ {
ids[j] = ids[i]; ids[j] = ids[i];
@ -1500,7 +1507,8 @@ export struct EntityManager
} }
info.comp_add_info = Mallocator.makeArray!(EntityInfo*)(instance.components.length); 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)
{ {
@ -1782,7 +1790,8 @@ export struct EntityManager
} }
} }
if(num == 0)return; if (num == 0)
return;
del_ids = del_ids[0 .. num]; del_ids = del_ids[0 .. num];
//sort components //sort components
@ -1892,7 +1901,8 @@ export struct EntityManager
} }
} }
if(num == 0)return; if (num == 0)
return;
new_ids = new_ids[0 .. num]; new_ids = new_ids[0 .. num];
foreach (int i; 0 .. num) foreach (int i; 0 .. num)
@ -2834,7 +2844,7 @@ export struct EntityManager
assert(len == components.length + 1); 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; comp_add_info[id] = new_info;
return new_info; return new_info;