-LinearLayout implemntation:

*Entity.getComponent works
 *adding/removing objects work
 *update work
This commit is contained in:
Mergul 2018-09-26 09:02:16 +02:00
parent 535071ebb9
commit cb9bac5dde
5 changed files with 411 additions and 120 deletions

View file

@ -23,11 +23,11 @@ int main()
float a;
}
struct TestComp
static struct TestComp
{
__gshared ushort component_id;
int a;
ulong b;
int a = 1;
ulong b = 2;
static void serializeComponent(SerializeVector output)
{
@ -40,11 +40,11 @@ int main()
}
}
struct TestComp2
static struct TestComp2
{
__gshared ushort component_id;
int b;
int a;
int b = 3;
int a = 4;
static void serializeComponent(ref TestComp comp, SerializeVector output)
{
@ -57,11 +57,11 @@ int main()
}
}
struct TestComp3
static struct TestComp3
{
__gshared ushort component_id;
uint gg; //good game
uint bg; //bad game
uint gg = 5; //good game
uint bg = 6; //bad game
void serializeComponent(SerializeVector output)
{
@ -74,11 +74,15 @@ int main()
}
}
struct TestComp4
static struct TestComp4
{
__gshared ushort component_id;
uint gg; //good game
uint bg; //bad game
uint gg = 7; //good game
uint bg = 8; //bad game
ulong a;
ulong b;
ulong c;
ulong g;
static void serializeComponent(ref TestComp comp, SerializeVector output)
{
@ -107,12 +111,12 @@ int main()
void onBegin()
{
writeln("On Test System begin.");
//writeln("On Test System begin.");
}
void onEnd()
{
writeln("On Test System end.");
//writeln("On Test System end.");
}
void initialize(ref Entity entity, ref TestComp comp)
@ -127,7 +131,7 @@ int main()
@("optional") TestComp3* test3;
}
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)
{
assert(cast(size_t)&test % TestComp.alignof == 0);
assert(cast(size_t)&test2 % TestComp2.alignof == 0);
@ -141,8 +145,12 @@ int main()
test2.a = 8;
//writeln("Jakis tekst! ",test2.b);
//writeln("Low priority tekst! ");
if (test3)
/*if (test3)
{
test3.gg = 200;
test3.bg += 1;
}*/
}
void handleEvent(TestEvent event, ref TestComp test, ref TestComp2 test2, TestComp3* test3)
@ -209,6 +217,21 @@ int main()
}*/
}
void writeEntityComponents(Entity* entity)
{
write(entity.id);
TestComp* test_comp = entity.getComponent!TestComp;
if(test_comp)write(*test_comp);
TestComp2* test_comp2 = entity.getComponent!TestComp2;
if(test_comp2)write(*test_comp2);
TestComp3* test_comp3 = entity.getComponent!TestComp3;
if(test_comp3)write(*test_comp3);
TestComp4* test_comp4 = entity.getComponent!TestComp4;
if(test_comp4)write(*test_comp4);
writeln();
//writeln((cast(uint*) pp)[0 .. 14], " ", pp);
}
EntityManager.initialize();
assert(gEM !is null);
@ -237,14 +260,24 @@ int main()
time = MonoTime.currTime;
ushort[2] ids = [TestComp2.component_id, TestComp.component_id];
ushort[3] ids = [TestComp2.component_id, TestComp.component_id, TestComp4.component_id];
EntityTemplate* tmpl = gEM.allocateTemplate(ids);
ushort[3] ids2 = [TestComp3.component_id, TestComp.component_id, TestComp4.component_id];
EntityTemplate* tmpl2 = gEM.allocateTemplate(ids2);
//writeln(tmpl.info.components[]);
*cast(EntityID*) tmpl.entity_data.ptr = EntityID(1, 1);
//*cast(EntityID*) tmpl.entity_data.ptr = EntityID(1, 1);
dur = (MonoTime.currTime - time).total!"usecs";
writeln("Template allocating: ", dur, " usecs");
Entity entity;
{
entity = gEM.addEntity(tmpl);
writeEntityComponents(gEM.getEntity(entity.id));
}
time = MonoTime.currTime;
//foreach(i; 0..1_000_000)gEM.addEntity(tmpl);
@ -253,13 +286,18 @@ int main()
EntityID[1000] idss;
foreach (i; 0 .. 1_000)
/*foreach (i; 0 .. 1_000)
{
gEM.begin();
foreach (j; 0 .. 1_000)
idss[j] = gEM.addEntity(tmpl).id;
foreach (j; 0 .. 1_000)
gEM.removeEntity(idss[j]);
}
gEM.end();
}*/
dur = (MonoTime.currTime - time).total!"usecs";
writeln("Entities adding: ", dur, " usecs");
uint blocks = 0;
foreach (info; &gEM.entities_infos.byValue)
@ -273,13 +311,6 @@ int main()
}
writeln("Entities blocks: ", blocks);
/*Entity entity = gEM.addEntity(tmpl);
gEM.removeEntity(entity.id);
gEM.addEntity(tmpl);*/
dur = (MonoTime.currTime - time).total!"usecs";
writeln("Entities adding: ", dur, " usecs");
//foreach(j; 0..1_000)gEM.addEntity(tmpl);
gEM.registerSystem!TestSystem2(0);
@ -287,43 +318,69 @@ int main()
//assert(*(cast(EntityID*)(cast(void*)tmpl.info.first_block+24)) == EntityID(1,1));
//assert(*(cast(EntityID*)(cast(void*)tmpl.info.first_block+48)) == EntityID(1,1));
foreach (i; 0 .. 500_000)
{
gEM.addEntity(tmpl);
gEM.addEntity(tmpl2);
}
time = MonoTime.currTime;
//gEM.update();
dur = (MonoTime.currTime - time).total!"usecs";
writeln("Update: ", dur, " usecs");
Entity entity = gEM.addEntity(tmpl);
gEM.begin();
gEM.update();
gEM.end();
Entity* pp = gEM.getEntity(entity.id);
writeln((cast(uint*) pp)[0 .. 14], " ", pp);
dur = (MonoTime.currTime - time).total!"usecs";
writeln("Update: ", dur, " usecs");
time = MonoTime.currTime;
gEM.begin();
gEM.update();
gEM.end();
dur = (MonoTime.currTime - time).total!"usecs";
writeln("Update: ", dur, " usecs");
time = MonoTime.currTime;
gEM.begin();
gEM.update();
gEM.end();
dur = (MonoTime.currTime - time).total!"usecs";
writeln("Update: ", dur, " usecs");
writeEntityComponents(gEM.getEntity(entity.id));
entity = gEM.addEntity(tmpl);
gEM.begin();
gEM.update();
gEM.end();
//Entity* pp;// = gEM.getEntity(entity.id);
//writeln((cast(uint*) pp)[0 .. 14], " ", pp);
writeEntityComponents(gEM.getEntity(entity.id));
gEM.addEntity(tmpl);
gEM.addComponents(entity.id, TestComp4());
gEM.addComponents(entity.id, TestComp3());
gEM.begin();
gEM.update();
gEM.end();
pp = gEM.getEntity(entity.id);
writeEntityComponents(gEM.getEntity(entity.id));
writeln((cast(uint*) pp)[0 .. 14], " ", pp);
//gEM.removeComponents!(TestComp)(entity.id);
gEM.removeComponents!(TestComp)(entity.id);
gEM.begin();
gEM.update();
gEM.end();
pp = gEM.getEntity(entity.id);
writeln((cast(uint*) pp)[0 .. 14], " ", pp);
writeEntityComponents(gEM.getEntity(entity.id));
//import std.stdio;
//writeln((cast(uint*)tmpl.info.first_block)[0..48]);
@ -331,4 +388,4 @@ int main()
EntityManager.destroy();
return 0;
}
}