-onAdd/onRemove called when components are changed

This commit is contained in:
Mergul 2019-03-24 14:42:21 +00:00
parent e4be23ee96
commit 4ac80d7025
2 changed files with 131 additions and 14 deletions

View file

@ -1284,11 +1284,14 @@ class EntityManager
} }
if (index < passes[system.m_pass].system_callers.length) if (index < passes[system.m_pass].system_callers.length)
{
passes[system.m_pass].system_callers[index].infos.add(&entity); passes[system.m_pass].system_callers[index].infos.add(&entity);
entity.systems[system_id] = true; entity.systems[system_id] = true;
} }
}
/************************************************************************************************************************ /************************************************************************************************************************
*Returns pointer to entity. *Returns pointer to entity.
* *
@ -1365,6 +1368,18 @@ class EntityManager
uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) >> 3); uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) >> 3);
else else
uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) / EntityID.sizeof()); uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) / EntityID.sizeof());
if(info.remove_listeners)
{
foreach(listener;info.remove_listeners)
{
if(!new_info.systems[listener])
{
callRemoveEntityListener(&systems[listener],info,block,ind,ind+1);
}
}
}
foreach (comp; new_info.components) foreach (comp; new_info.components)
{ {
uint comp_size = components[comp].size; uint comp_size = components[comp].size;
@ -1372,6 +1387,17 @@ class EntityManager
cast(void*) block + info.deltas[comp] + ind * comp_size, comp_size); cast(void*) block + info.deltas[comp] + ind * comp_size, comp_size);
} }
if(new_info.add_listeners)
{
foreach(listener;new_info.add_listeners)
{
if(!info.systems[listener])
{
callAddEntityListener(&systems[listener],new_info,new_block,new_block.entities_count,new_block.entities_count+1);
}
}
}
new_block.entities_count++; new_block.entities_count++;
removeEntityNoID(entity, block); removeEntityNoID(entity, block);
@ -1495,6 +1521,18 @@ class EntityManager
uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) >> 3); uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) >> 3);
else else
uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) / EntityID.sizeof()); uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) / EntityID.sizeof());
if(info.remove_listeners)
{
foreach(listener;info.remove_listeners)
{
if(!new_info.systems[listener])
{
callRemoveEntityListener(&systems[listener],info,block,ind,ind+1);
}
}
}
foreach (ref id; ids[0 .. len]) foreach (ref id; ids[0 .. len])
{ {
void* dst = cast(void*) new_block + new_info.deltas[id] + ( void* dst = cast(void*) new_block + new_info.deltas[id] + (
@ -1522,6 +1560,17 @@ class EntityManager
} }
} }
if(new_info.add_listeners)
{
foreach(listener;new_info.add_listeners)
{
if(!info.systems[listener])
{
callAddEntityListener(&systems[listener],new_info,new_block,new_block.entities_count,new_block.entities_count+1);
}
}
}
new_block.entities_count++; new_block.entities_count++;
removeEntityNoID(entity, block); removeEntityNoID(entity, block);
} }
@ -1852,6 +1901,12 @@ class EntityManager
foreach(listener;info.add_listeners) foreach(listener;info.add_listeners)
{ {
System* system = &systems[listener]; System* system = &systems[listener];
callAddEntityListener(system,info,block,begin,end);
}
}
private void callAddEntityListener(System* system, EntityInfo* info, EntitiesBlock* block, int begin, int end) @nogc nothrow
{
ListenerCallData data; ListenerCallData data;
data.system = system; data.system = system;
data.block = block; data.block = block;
@ -1859,13 +1914,18 @@ class EntityManager
data.end = end; 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.add_listeners) foreach(listener;info.add_listeners)
{ {
System* system = &systems[listener]; System* system = &systems[listener];
callRemoveEntityListener(system,info,block,begin,end);
}
}
private void callRemoveEntityListener(System* system, EntityInfo* info, EntitiesBlock* block, int begin, int end) @nogc nothrow
{
ListenerCallData data; ListenerCallData data;
data.system = system; data.system = system;
data.block = block; data.block = block;
@ -1873,7 +1933,6 @@ class EntityManager
data.end = end; 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() private void updateBlocks()
{ {

View file

@ -94,6 +94,63 @@ static struct TestComp4
} }
} }
struct ChangeTestSystem
{
mixin ECS.System!16;//__gshared ushort system_id;
void onCreate()
{
writeln("On Change Test System create.");
}
void onDestroy()
{
writeln("On Change Test System destroy.");
}
void onAdd(EntitiesData data)
{
foreach(i;0..data.length)
writeln("Entity added ID: ",data.entites[i].id.id);
}
void onRemove(EntitiesData data)
{
writeln("Entity removed ID: ",data.entites[0].id);
}
bool onBegin()
{
//writeln("On Test System begin.");
return true;
}
void onEnd()
{
//writeln("On Test System end.");
}
void initialize(ref Entity entity, ref TestComp comp)
{
}
static struct EntitiesData
{
size_t length;
const(Entity)[] entites;
TestComp4[] test4;
}
void update(EntitiesData data)
{
foreach(i;0..data.length)
{
}
}
}
struct TestSystem struct TestSystem
{ {
mixin ECS.System!16;//__gshared ushort system_id; mixin ECS.System!16;//__gshared ushort system_id;
@ -344,6 +401,7 @@ int main()
gEM.registerSystem!TestSystemWithHighPriority(100,"fixed"); gEM.registerSystem!TestSystemWithHighPriority(100,"fixed");
gEM.registerSystem!TestSystem(0); gEM.registerSystem!TestSystem(0);
gEM.registerSystem!ChangeTestSystem(0);
//gEM.registerSystem!TestSystemWithHighPriority(100); //gEM.registerSystem!TestSystemWithHighPriority(100);
//gEM.registerSystem!TestSystem2(0); //gEM.registerSystem!TestSystem2(0);
gEM.endRegister(); gEM.endRegister();
@ -491,6 +549,7 @@ int main()
gEM.removeComponents!(TestComp)(entity.id); gEM.removeComponents!(TestComp)(entity.id);
gEM.addComponents(entity.id, TestComp()); gEM.addComponents(entity.id, TestComp());
gEM.removeComponents!(TestComp4)(entity.id);
gEM.begin(); gEM.begin();
gEM.update(); gEM.update();
@ -498,7 +557,6 @@ int main()
gEM.end(); gEM.end();
writeEntityComponents(gEM.getEntity(entity.id)); writeEntityComponents(gEM.getEntity(entity.id));
//import std.stdio; //import std.stdio;
//writeln((cast(uint*)tmpl.info.first_block)[0..48]); //writeln((cast(uint*)tmpl.info.first_block)[0..48]);
gEM.freeTemplate(tmpl); gEM.freeTemplate(tmpl);