-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,9 +1284,12 @@ class EntityManager
}
if (index < passes[system.m_pass].system_callers.length)
{
passes[system.m_pass].system_callers[index].infos.add(&entity);
entity.systems[system_id] = true;
entity.systems[system_id] = true;
}
}
/************************************************************************************************************************
@ -1365,6 +1368,18 @@ class EntityManager
uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) >> 3);
else
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)
{
uint comp_size = components[comp].size;
@ -1372,6 +1387,17 @@ class EntityManager
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++;
removeEntityNoID(entity, block);
@ -1495,6 +1521,18 @@ class EntityManager
uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) >> 3);
else
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])
{
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++;
removeEntityNoID(entity, block);
}
@ -1852,29 +1901,39 @@ class EntityManager
foreach(listener;info.add_listeners)
{
System* system = &systems[listener];
ListenerCallData data;
data.system = system;
data.block = block;
data.begin = begin;
data.end = end;
(cast(void function (ref ListenerCallData) nothrow @nogc) system.m_entity_added)(data);
callAddEntityListener(system,info,block,begin,end);
}
}
private void callAddEntityListener(System* system, EntityInfo* info, EntitiesBlock* block, int begin, int end) @nogc nothrow
{
ListenerCallData data;
data.system = system;
data.block = block;
data.begin = begin;
data.end = end;
(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
{
foreach(listener;info.add_listeners)
{
System* system = &systems[listener];
ListenerCallData data;
data.system = system;
data.block = block;
data.begin = begin;
data.end = end;
(cast(void function (ref ListenerCallData) nothrow @nogc) system.m_entity_removed)(data);
callRemoveEntityListener(system,info,block,begin,end);
}
}
private void callRemoveEntityListener(System* system, EntityInfo* info, EntitiesBlock* block, int begin, int end) @nogc nothrow
{
ListenerCallData data;
data.system = system;
data.block = block;
data.begin = begin;
data.end = end;
(cast(void function (ref ListenerCallData) nothrow @nogc) system.m_entity_removed)(data);
}
private void updateBlocks()
{
foreach (ref thread; threads)

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
{
mixin ECS.System!16;//__gshared ushort system_id;
@ -344,6 +401,7 @@ int main()
gEM.registerSystem!TestSystemWithHighPriority(100,"fixed");
gEM.registerSystem!TestSystem(0);
gEM.registerSystem!ChangeTestSystem(0);
//gEM.registerSystem!TestSystemWithHighPriority(100);
//gEM.registerSystem!TestSystem2(0);
gEM.endRegister();
@ -491,6 +549,7 @@ int main()
gEM.removeComponents!(TestComp)(entity.id);
gEM.addComponents(entity.id, TestComp());
gEM.removeComponents!(TestComp4)(entity.id);
gEM.begin();
gEM.update();
@ -498,7 +557,6 @@ int main()
gEM.end();
writeEntityComponents(gEM.getEntity(entity.id));
//import std.stdio;
//writeln((cast(uint*)tmpl.info.first_block)[0..48]);
gEM.freeTemplate(tmpl);