-better compile time code generation
-support for AbsenComponents. Absen components can't exist in entity which are used by system. There are three possibilities to add absen component. Add '@absen' property to EntitiesData mebmer, create AbsenComponents enum with fields names corresponding to components names, or create AbsenComponents AliasSeq with strings expressions corresponding to components names.
This commit is contained in:
parent
125c9e7d40
commit
e4573f1ec7
2 changed files with 93 additions and 29 deletions
|
|
@ -73,7 +73,7 @@ class EntityManager
|
||||||
|
|
||||||
static string genCompList()()
|
static string genCompList()()
|
||||||
{
|
{
|
||||||
string ret = "ushort comp;uint req;uint opt;uint absen;";
|
|
||||||
static foreach (member; __traits(allMembers, Sys.EntitiesData))
|
static foreach (member; __traits(allMembers, Sys.EntitiesData))
|
||||||
{
|
{
|
||||||
static if (isFunction!(__traits(getMember, Sys.EntitiesData, member)))
|
static if (isFunction!(__traits(getMember, Sys.EntitiesData, member)))
|
||||||
|
|
@ -86,11 +86,20 @@ class EntityManager
|
||||||
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,
|
}
|
||||||
|
|
||||||
|
string ret;// = "ushort comp;uint req;uint opt;uint absen;";
|
||||||
|
|
||||||
|
uint req;
|
||||||
|
uint opt;
|
||||||
|
uint absen;
|
||||||
|
foreach (member; __traits(allMembers, Sys.EntitiesData))
|
||||||
|
{
|
||||||
|
if (member == "length" || is(typeof(__traits(getMember, Sys.EntitiesData,
|
||||||
member)) == Entity[]) || is(typeof(__traits(getMember,
|
member)) == Entity[]) || is(typeof(__traits(getMember,
|
||||||
Sys.EntitiesData, member)) == const(Entity)[]))
|
Sys.EntitiesData, member)) == const(Entity)[]))
|
||||||
{
|
{
|
||||||
//ret ~= "const string entities_name = \"" ~ member ~ "\";";
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -101,44 +110,72 @@ class EntityManager
|
||||||
{
|
{
|
||||||
if (att == "optional")
|
if (att == "optional")
|
||||||
{
|
{
|
||||||
ret ~= "opt++;";
|
//ret ~= "opt++;";
|
||||||
|
opt++;
|
||||||
has_att = true;
|
has_att = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (att == "absen")
|
else if (att == "absen")
|
||||||
{
|
{
|
||||||
ret ~= "absen++;";
|
absen++;
|
||||||
|
//ret ~= "absen++;";
|
||||||
has_att = true;
|
has_att = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!has_att)
|
if (!has_att)req++;
|
||||||
ret ~= "req++;";
|
//ret ~= "req++;";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ret ~= "system.m_components = Mallocator.instance.makeArray!ushort(req);";
|
static if(__traits(hasMember, Sys, "AbsenComponents"))
|
||||||
ret ~= "system.m_optional_components = Mallocator.instance.makeArray!ushort(opt);";
|
|
||||||
ret ~= "system.m_absen_components = Mallocator.instance.makeArray!ushort(absen);";
|
|
||||||
ret ~= "opt = 0;req = 0;absen = 0;";
|
|
||||||
|
|
||||||
static foreach (member; __traits(allMembers, Sys.EntitiesData))
|
|
||||||
{
|
{
|
||||||
static if (is(typeof(__traits(getMember, Sys.EntitiesData,
|
static if(is(Sys.AbsenComponents == enum))
|
||||||
|
{
|
||||||
|
absen += (Fields!(Sys.AbsenComponents)).length;//static assert(0,"Enum AbsenComponents are not implemented yet.");
|
||||||
|
}
|
||||||
|
else static if(__traits(compiles,allSameType!(string,typeof(Sys.AbsenComponents))) && allSameType!(string,typeof(Sys.AbsenComponents)) &&
|
||||||
|
isExpressions!(Sys.AbsenComponents))
|
||||||
|
{
|
||||||
|
absen += Sys.AbsenComponents.length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(req > 0)ret ~= "system.m_components = Mallocator.instance.makeArray!ushort("~req.to!string~");";
|
||||||
|
if(opt > 0)ret ~= "system.m_optional_components = Mallocator.instance.makeArray!ushort("~opt.to!string~");";
|
||||||
|
if(absen > 0)ret ~= "system.m_absen_components = Mallocator.instance.makeArray!ushort("~absen.to!string~");";
|
||||||
|
ret ~= "ushort comp;";//uint opt = 0;uint req = 0;uint absen = 0;";
|
||||||
|
|
||||||
|
opt = 0;
|
||||||
|
req = 0;
|
||||||
|
absen = 0;
|
||||||
|
|
||||||
|
static if(__traits(hasMember, Sys, "AbsenComponents"))
|
||||||
|
{
|
||||||
|
static if(is(Sys.AbsenComponents == enum))
|
||||||
|
{
|
||||||
|
//static assert(0,"Enum AbsenComponents are not implemented yet.");
|
||||||
|
foreach(str;Fields!(Sys.AbsenComponents))ret ~= "system.m_absen_components["~(absen++).to!string~"] = components_map.get(\""~str.stringof~"\", ushort.max);";
|
||||||
|
}
|
||||||
|
else static if(__traits(compiles,allSameType!(string,typeof(Sys.AbsenComponents))) && allSameType!(string,typeof(Sys.AbsenComponents)) &&
|
||||||
|
isExpressions!(Sys.AbsenComponents))
|
||||||
|
{
|
||||||
|
foreach(str;Sys.AbsenComponents)ret ~= "system.m_absen_components["~(absen++).to!string~"] = components_map.get(\""~str~"\", ushort.max);";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (member; __traits(allMembers, Sys.EntitiesData))
|
||||||
|
{
|
||||||
|
if (member == "length" || is(typeof(__traits(getMember, Sys.EntitiesData,
|
||||||
member)) == Entity[]) || is(typeof(__traits(getMember,
|
member)) == Entity[]) || is(typeof(__traits(getMember,
|
||||||
Sys.EntitiesData, member)) == const(Entity)[]))
|
Sys.EntitiesData, member)) == const(Entity)[]))
|
||||||
{
|
|
||||||
//ret ~= "const string entities_name = \"" ~ member ~ "\";";
|
|
||||||
}
|
|
||||||
else static if(member == "length")
|
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
|
|
||||||
ret ~= "{comp = components_map.get(Unqual!(ForeachType!(typeof(
|
ret ~= "{comp = components_map.get(Unqual!(ForeachType!(typeof(
|
||||||
Sys.EntitiesData." ~ member ~ ")))
|
Sys.EntitiesData." ~ member ~ ")))
|
||||||
.stringof, ushort.max);\n
|
.stringof, ushort.max);\n
|
||||||
|
|
@ -156,20 +193,20 @@ class EntityManager
|
||||||
{
|
{
|
||||||
if (att == "optional")
|
if (att == "optional")
|
||||||
{
|
{
|
||||||
ret ~= "system.m_optional_components[opt++] = comp;}";
|
ret ~= "system.m_optional_components["~(opt++).to!string~"] = comp;}";
|
||||||
has_att = true;
|
has_att = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (att == "absen")
|
else if (att == "absen")
|
||||||
{
|
{
|
||||||
ret ~= "system.m_absen_components[absen++] = comp;}";
|
ret ~= "system.m_absen_components["~(absen++).to!string~"] = comp;}";
|
||||||
has_att = true;
|
has_att = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!has_att)
|
if (!has_att)
|
||||||
{
|
{
|
||||||
ret ~= "system.m_components[req++] = comp;}";
|
ret ~= "system.m_components["~(req++).to!string~"] = comp;}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -586,6 +623,17 @@ class EntityManager
|
||||||
ushort[] deltas = (cast(ushort*) alloca(num * ushort.sizeof))[0 .. num];
|
ushort[] deltas = (cast(ushort*) alloca(num * ushort.sizeof))[0 .. num];
|
||||||
uint delta_id = 0;
|
uint delta_id = 0;
|
||||||
|
|
||||||
|
if(system.m_absen_components)
|
||||||
|
{
|
||||||
|
foreach (id; system.m_absen_components)
|
||||||
|
{
|
||||||
|
foreach (id2; entity.components)
|
||||||
|
{
|
||||||
|
if(id == id2)return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
foreach (id; system.m_components)
|
foreach (id; system.m_components)
|
||||||
{
|
{
|
||||||
deltas[delta_id] = ushort.max;
|
deltas[delta_id] = ushort.max;
|
||||||
|
|
@ -618,11 +666,6 @@ class EntityManager
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*if (deltas[delta_id] == ushort.max)
|
|
||||||
{
|
|
||||||
deltas = null;
|
|
||||||
break;
|
|
||||||
}*/
|
|
||||||
delta_id++;
|
delta_id++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -131,7 +131,7 @@ int main()
|
||||||
TestComp[] test;
|
TestComp[] test;
|
||||||
TestComp2[] test2;
|
TestComp2[] test2;
|
||||||
@optional TestComp3[] test3;
|
@optional TestComp3[] test3;
|
||||||
@absen TestComp4[] test4;
|
//@absen TestComp4[] test4;
|
||||||
}
|
}
|
||||||
|
|
||||||
void update(ref Entity entity, ref TestComp test, ref TestComp2 test2)//, TestComp3* test3) //ref TestComp comp)
|
void update(ref Entity entity, ref TestComp test, ref TestComp2 test2)//, TestComp3* test3) //ref TestComp comp)
|
||||||
|
|
@ -188,15 +188,28 @@ int main()
|
||||||
}*/
|
}*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
import std.meta;
|
||||||
|
|
||||||
struct TestSystem2
|
struct TestSystem2
|
||||||
{
|
{
|
||||||
__gshared ushort system_id;
|
__gshared ushort system_id;
|
||||||
|
|
||||||
|
enum AbsenComponents0
|
||||||
|
{
|
||||||
|
TestComp,
|
||||||
|
TestComp4
|
||||||
|
}
|
||||||
|
|
||||||
|
alias AbsenComponents = AliasSeq!("TestComp", "TestComp4");
|
||||||
|
|
||||||
|
string AbsenComponents2;
|
||||||
|
|
||||||
static struct EntitiesData
|
static struct EntitiesData
|
||||||
{
|
{
|
||||||
short length;
|
short length;
|
||||||
Entity[] entity;
|
Entity[] entity;
|
||||||
TestComp3[] test;
|
TestComp3[] test;
|
||||||
|
//@absen TestComp[] testt;
|
||||||
}
|
}
|
||||||
|
|
||||||
void onEnable()
|
void onEnable()
|
||||||
|
|
@ -225,7 +238,15 @@ int main()
|
||||||
data.test[i].gg += 14;
|
data.test[i].gg += 14;
|
||||||
gEM.sendSelfEvent!(TestEvent)(data.entity[i].id, TestEvent());
|
gEM.sendSelfEvent!(TestEvent)(data.entity[i].id, TestEvent());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void lateUpdate(ref EntitiesData data)
|
||||||
|
{
|
||||||
|
foreach(i;0..data.test.length)
|
||||||
|
{
|
||||||
|
data.test[i].gg -= 1;
|
||||||
|
gEM.sendSelfEvent!(TestEvent)(data.entity[i].id, TestEvent());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*void handleEvent(Event event, ref TestComp comp)
|
/*void handleEvent(Event event, ref TestComp comp)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue