-support for 'length' parameter in EntitiesData (for convenience)

This commit is contained in:
Mergul 2018-10-01 15:08:14 +02:00
parent 0e13fafefd
commit 125c9e7d40
3 changed files with 24 additions and 5 deletions

View file

@ -78,11 +78,17 @@ class EntityManager
{ {
static if (isFunction!(__traits(getMember, Sys.EntitiesData, member))) static if (isFunction!(__traits(getMember, Sys.EntitiesData, member)))
static assert(0, "EntitiesData can't have any function!"); static assert(0, "EntitiesData can't have any function!");
else static if(member == "length")
{
static assert(isIntegral!(typeof(__traits(getMember,Sys.EntitiesData, member))),"EntitiesData 'length' member must be integral type.");
static assert(typeof(__traits(getMember,Sys.EntitiesData, member)).sizeof > 1,"EntitiesData 'length' member can't be byte or ubyte.");
}
else static if (!(isArray!(typeof(__traits(getMember, else static if (!(isArray!(typeof(__traits(getMember,
Sys.EntitiesData, member))))) Sys.EntitiesData, member)))))
static assert(0, "EntitiesData members should be arrays of elements!"); static assert(0, "EntitiesData members should be arrays of elements!");
else static if (is(typeof(__traits(getMember, Sys.EntitiesData, else static if (is(typeof(__traits(getMember, Sys.EntitiesData,
member)) == Entity[])) member)) == Entity[]) || is(typeof(__traits(getMember,
Sys.EntitiesData, member)) == const(Entity)[]))
{ {
//ret ~= "const string entities_name = \"" ~ member ~ "\";"; //ret ~= "const string entities_name = \"" ~ member ~ "\";";
} }
@ -124,6 +130,10 @@ class EntityManager
Sys.EntitiesData, member)) == const(Entity)[])) Sys.EntitiesData, member)) == const(Entity)[]))
{ {
//ret ~= "const string entities_name = \"" ~ member ~ "\";"; //ret ~= "const string entities_name = \"" ~ member ~ "\";";
}
else static if(member == "length")
{
} }
else else
{ {
@ -198,6 +208,11 @@ class EntityManager
ret ~= "input_data." ~ member ret ~= "input_data." ~ member
~ " = (cast(Entity*) block.dataBegin())[0 .. block.entities_count];"; ~ " = (cast(Entity*) block.dataBegin())[0 .. block.entities_count];";
} }
else if(member == "length")
{
ret ~= "input_data." ~ member
~ " = block.entities_count;";
}
else else
{ {
{ {
@ -306,8 +321,8 @@ class EntityManager
} }
else else
{ {
string name = Mallocator.instance.makeArray(Sys.stringof); system.name = Mallocator.instance.makeArray(Sys.stringof);
systems_map.add(name, cast(ushort) systems.length); systems_map.add(system.name, cast(ushort) systems.length);
systems.add(system); systems.add(system);
@ -1240,7 +1255,7 @@ class EntityManager
Vector!ubyte change_entities_list; Vector!ubyte change_entities_list;
HashMap!(ushort[], EntityInfo*) entities_infos; HashMap!(ushort[], EntityInfo*) entities_infos;
HashMap!(string, ushort) systems_map; HashMap!(const (char)[], ushort) systems_map;
HashMap!(string, ushort) components_map; HashMap!(string, ushort) components_map;
HashMap!(string, ushort) events_map; HashMap!(string, ushort) events_map;
Vector!System systems; Vector!System systems;

View file

@ -38,6 +38,8 @@ package:
///pointer to system implementation ///pointer to system implementation
void* m_system_pointer; void* m_system_pointer;
const (char)[] name;
ushort[] m_components; ushort[] m_components;
ushort[] m_absen_components; ushort[] m_absen_components;
ushort[] m_optional_components; ushort[] m_optional_components;

View file

@ -127,6 +127,7 @@ int main()
static struct EntitiesData static struct EntitiesData
{ {
size_t length;
TestComp[] test; TestComp[] test;
TestComp2[] test2; TestComp2[] test2;
@optional TestComp3[] test3; @optional TestComp3[] test3;
@ -147,7 +148,7 @@ int main()
void update(ref EntitiesData data) void update(ref EntitiesData data)
{ {
foreach(i;0..data.test.length) foreach(i;0..data.length)
{ {
data.test[i].a += 1000; data.test[i].a += 1000;
data.test[i].b += 2000; data.test[i].b += 2000;
@ -193,6 +194,7 @@ int main()
static struct EntitiesData static struct EntitiesData
{ {
short length;
Entity[] entity; Entity[] entity;
TestComp3[] test; TestComp3[] test;
} }