-system name getter

-added error checking for event handling in registerSystem
-now only valid handleEvent() functions are taken by system during register
This commit is contained in:
Mergul 2019-08-10 15:35:36 +00:00
parent f27e4c30ad
commit dfdb56d501
3 changed files with 43 additions and 17 deletions

View file

@ -69,7 +69,7 @@ export class EntityManager
if(system.m_components)Mallocator.instance.dispose(system.m_components);
if(system.m_excluded_components)Mallocator.instance.dispose(system.m_excluded_components);
if(system.m_optional_components)Mallocator.instance.dispose(system.m_optional_components);
if(system.name)Mallocator.instance.dispose(system.name);
if(system.m_name)Mallocator.instance.dispose(system.m_name);
if(system.m_event_callers)Mallocator.instance.dispose(system.m_event_callers);
if(system.m_system_pointer)Mallocator.instance.dispose(system.m_system_pointer);
@ -344,20 +344,31 @@ export class EntityManager
data_system.handleEvent(input, *cast(Type*) data.event);
}
static void setEventCallers(Sys)(ref System system)
void setEventCallers(Sys)(ref System system)
{
enum event_handlers_num = __traits(getOverloads, Sys, "handleEvent").length;
system.m_event_callers = Mallocator.instance.makeArray!(
System.EventCaller)(event_handlers_num);
System.EventCaller[] callers = (cast(System.EventCaller*)alloca(event_handlers_num * System.EventCaller.sizeof))[0..event_handlers_num];
int i = 0;
foreach (j, func; __traits(getOverloads, Sys, "handleEvent"))
{
alias EventParamType = Parameters!(__traits(getOverloads,
Sys, "handleEvent")[j])[1];
system.m_event_callers[j].callback = cast(
void*)&callEventHandler!(EventParamType);
system.m_event_callers[j].id = EventParamType.event_id;
alias Params = Parameters!(__traits(getOverloads,
Sys, "handleEvent")[j]);
static if(Params.length == 2 && is(Params[0] == __traits(getMember, Sys, "EventInput")))
{
alias EventParamType = Params[1];
enum EventName = Unqual!(EventParamType).stringof;
ushort evt = events_map.get(cast(char[]) EventName, ushort.max);
assert(evt != ushort.max, "Can't register system \""~Sys.stringof~"\" due to non existing event \""~EventName~"\".");
callers[i].callback = cast(
void*)&callEventHandler!(EventParamType);
callers[i].id = EventParamType.event_id;
i++;
}
}
system.m_event_callers = Mallocator.instance.makeArray(callers[0..i]);
}
static if (__traits(hasMember, Sys, "handleEvent"))
@ -736,8 +747,8 @@ export class EntityManager
}
else
{
system.name = Mallocator.instance.makeArray(Sys.stringof);
systems_map.add(system.name, cast(ushort) systems.length);
system.m_name = Mallocator.instance.makeArray(Sys.stringof);
systems_map.add(system.m_name, cast(ushort) systems.length);
system.m_id = cast(ushort)(systems.length);
@ -2302,9 +2313,9 @@ export class EntityManager
caller.exclusion = null;
/*import std.stdio;
write("Exclusive systems for system ", caller.system.name, ": ");
write("Exclusive systems for system ", caller.system.m_name, ": ");
foreach (ex; exclusion[0 .. index])
write(ex.system.name, " ");
write(ex.system.m_name, " ");
writeln();*/
}
@ -2362,9 +2373,9 @@ export class EntityManager
caller.dependencies = null;
/*import std.stdio;
write("Dependencies for system ", caller.system.name, ": ");
write("Dependencies for system ", caller.system.m_name, ": ");
foreach (ex; caller.dependencies)
write(ex.system.name, " ");
write(ex.system.m_name, " ");
writeln();*/
}
}

View file

@ -74,6 +74,14 @@ struct System
return m_id;
}
/************************************************************************************************************************
*Get system name.
*/
export const (char)[] name() nothrow @nogc
{
return cast(const (char)[])m_name;
}
struct EventCaller
{
ushort id;
@ -97,7 +105,7 @@ package:
int m_pass;
///system name
char[] name;
char[] m_name;
///required components
ushort[] m_components;