-fixed events bug with calls for unsupported systems

-now empty systems can't handle events and listeners
This commit is contained in:
Mergul 2020-03-07 13:21:29 +01:00
parent d6b53425dd
commit 546b73c567
3 changed files with 71 additions and 14 deletions

View file

@ -1,6 +1,7 @@
project('DECS', 'd') project('DECS', 'd')
src = [ src = [
'source/ecs/atomic.d',
'source/ecs/attributes.d', 'source/ecs/attributes.d',
'source/ecs/block_allocator.d', 'source/ecs/block_allocator.d',
'source/ecs/core.d', 'source/ecs/core.d',
@ -9,14 +10,12 @@ src = [
'source/ecs/hash_map.d', 'source/ecs/hash_map.d',
'source/ecs/id_manager.d', 'source/ecs/id_manager.d',
'source/ecs/manager.d', 'source/ecs/manager.d',
'source/ecs/simple_vector.d', 'source/ecs/package.d',
'source/ecs/simple_vector.d', 'source/ecs/simple_vector.d',
'source/ecs/std.d', 'source/ecs/std.d',
'source/ecs/atomic.d',
'source/ecs/system.d', 'source/ecs/system.d',
'source/ecs/traits.d', 'source/ecs/traits.d',
'source/ecs/vector.d', 'source/ecs/vector.d'
'source/ecs/package.d'
] ]
tests_src = [ tests_src = [

View file

@ -164,14 +164,16 @@ export struct EntityManager
foreach (ref system; systems) foreach (ref system; systems)
{ {
if (system.m_empty == true) if (system.m_empty)
{ {
addSystemCaller(system.id); if (system.m_update)
addSystemCaller(system.id);
continue; continue;
} }
if (system.m_update is null) if (system.m_update is null)
{ {
if (system.m_add_entity || system.m_remove_entity || system.m_change_entity) if (system.m_add_entity || system.m_remove_entity
|| system.m_change_entity || system.m_event_callers.length)
{ {
foreach (info; &entities_infos.byValue) foreach (info; &entities_infos.byValue)
{ {
@ -181,7 +183,7 @@ export struct EntityManager
continue; continue;
} }
bool added = false; /*bool added = false;
foreach (i, caller; passes[system.m_pass].system_callers) foreach (i, caller; passes[system.m_pass].system_callers)
{ {
if (systems[caller.system_id].priority > system.priority) if (systems[caller.system_id].priority > system.priority)
@ -202,7 +204,8 @@ export struct EntityManager
sys_caller.job_group.caller = sys_caller; sys_caller.job_group.caller = sys_caller;
system.m_any_system_caller = sys_caller; system.m_any_system_caller = sys_caller;
passes[system.m_pass].system_callers.add(sys_caller); passes[system.m_pass].system_callers.add(sys_caller);
} }*/
addSystemCaller(system.id);
foreach (info; &entities_infos.byValue) foreach (info; &entities_infos.byValue)
{ {
@ -739,6 +742,9 @@ export struct EntityManager
enum OnUpdateOverloadNum = -1; enum OnUpdateOverloadNum = -1;
//enum HasOnUpdate = (hasMember!(Sys, "onUpdate") && checkOnUpdateParams()); //enum HasOnUpdate = (hasMember!(Sys, "onUpdate") && checkOnUpdateParams());
static if (components_info.req.length == 0 && components_info.optional.length == 0)
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 (components_info.req.length != 0 || components_info.optional.length != 0)
@ -819,8 +825,6 @@ export struct EntityManager
(cast(typeof(&__traits(getOverloads, s, (cast(typeof(&__traits(getOverloads, s,
"onUpdate")[OnUpdateOverloadNum])) data.update_delegate)(input_data); "onUpdate")[OnUpdateOverloadNum])) data.update_delegate)(input_data);
} }
system.m_empty = true;
} }
system.m_update = &callUpdate; system.m_update = &callUpdate;
@ -1454,9 +1458,12 @@ export struct EntityManager
foreach (i, ref system; systems) foreach (i, ref system; systems)
{ {
if (system.m_empty)
continue;
if (system.m_update is null) if (system.m_update is null)
{ {
if (system.m_add_entity || system.m_remove_entity || system.m_change_entity) if (system.m_add_entity || system.m_remove_entity
|| system.m_change_entity || system.m_event_callers.length)
connectListenerToEntityInfo(*info, cast(uint) i); connectListenerToEntityInfo(*info, cast(uint) i);
continue; continue;
} }

View file

@ -412,6 +412,55 @@ struct Sys3
} }
} }
struct EmptyEventSystem
{
mixin ECS.System;
bool handled = false;
struct EntitiesData
{
uint thread_id;
}
void handleEvent(Entity* entity, TestEvent event)
{
if(!handled)
{
printf("EmptyEventSystem.handleEvent() called!\n");
handled = true;
}
assert(0,"this shouldn't be called!");
}
}
struct EventSystem
{
mixin ECS.System;
bool handled = false;
struct EntitiesData
{
uint thread_id;
TestComp[] comp;
}
void handleEvent(Entity* entity, TestEvent event)
{
if(!handled)
{
printf("EventSystem.handleEvent() called!\n");
handled = true;
}
}
/*void onUpdate(EntitiesData)
{
}*/
}
struct EmptySystem struct EmptySystem
{ {
mixin ECS.System; mixin ECS.System;
@ -458,7 +507,7 @@ struct TestSystem2
//TestComp* tt; //TestComp* tt;
} }
void handleEvent(Entity* entity, ref TestEvent event) void handleEvent(Entity* entity, TestEvent event)
{ {
TestComp3* test = entity.getComponent!TestComp3; TestComp3* test = entity.getComponent!TestComp3;
test.bg = event.a; test.bg = event.a;
@ -467,7 +516,7 @@ struct TestSystem2
gEM.sendEvent(entity.id, event2); gEM.sendEvent(entity.id, event2);
} }
void handleEvent(Entity* entity, ref TestEvent2 event) void handleEvent(Entity* entity, TestEvent2 event)
{ {
TestComp3* test = entity.getComponent!TestComp3; TestComp3* test = entity.getComponent!TestComp3;
test.gg = cast(uint) event.a; test.gg = cast(uint) event.a;
@ -618,6 +667,8 @@ else:
gEM.registerSystem!Sys2(-100); gEM.registerSystem!Sys2(-100);
gEM.registerSystem!Sys3(-2); gEM.registerSystem!Sys3(-2);
gEM.registerSystem!EmptySystem(2); gEM.registerSystem!EmptySystem(2);
gEM.registerSystem!EmptyEventSystem(2);
gEM.registerSystem!EventSystem(2);
//gEM.registerSystem!TestSystemWithHighPriority(100); //gEM.registerSystem!TestSystemWithHighPriority(100);
//gEM.registerSystem!TestSystem2(0); //gEM.registerSystem!TestSystem2(0);
gEM.endRegister(); gEM.endRegister();