module tests.tests; import ecs.entity; import ecs.events; import ecs.manager; import ecs.system; import ecs.attributes; import ecs.core; import core.time; import std.stdio; int main() { struct TestEvent { mixin ECS.Event;//__gshared ushort event_id; int a; } struct TestEvent2 { mixin ECS.Event;//__gshared ushort event_id; float a; } static struct TestComp { mixin ECS.Component;//__gshared ushort component_id; int a = 1; ulong b = 2; static void serializeComponent(SerializeVector output) { } static void deserializeComponent(ubyte[] data) { } } static struct TestComp2 { mixin ECS.Component;//__gshared ushort component_id; int b = 3; int a = 4; static void serializeComponent(ref TestComp comp, SerializeVector output) { } static void deserializeComponent(ref TestComp comp, ubyte[] data) { } } static struct TestComp3 { mixin ECS.Component;//__gshared ushort component_id; uint gg = 5; //good game uint bg = 6; //bad game void serializeComponent(SerializeVector output) { } void deserializeComponent(ubyte[] data) { } } static struct TestComp4 { mixin ECS.Component;//__gshared ushort component_id; uint gg = 7; //good game uint bg = 8; //bad game ulong a = 9; ulong b = 10; ulong c = 11; ulong g = 12; static void serializeComponent(ref TestComp comp, SerializeVector output) { } static void deserializeComponent(ref TestComp comp, ubyte[] data) { } } struct TestSystem { mixin ECS.System!16;//__gshared ushort system_id; void onCreate() { writeln("On Test System create."); } void onDestroy() { writeln("On Test System destroy."); } void onBegin() { //writeln("On Test System begin."); } void onEnd() { //writeln("On Test System end."); } void initialize(ref Entity entity, ref TestComp comp) { } static struct EntitiesData { size_t length; TestComp[] test; TestComp2[] test2; @readonly @optional const(TestComp3)[] test3; //@absent TestComp4[] test4; } 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); import std.stdio; test.a += 1000; test.b += 2000; test2.b += 2; test2.a = 8; } void update(EntitiesData data) { foreach(i;0..data.length) { data.test[i].a += 1000; data.test[i].b += 2000; data.test2[i].b += 2; data.test2[i].a = 8; } } void handleEvent(TestEvent event, ref TestComp test, ref TestComp2 test2, TestComp3* test3) { } } struct TestSystemWithHighPriority { mixin ECS.System!16;//__gshared ushort system_id; static struct EntitiesData { TestComp[] test; } void initialize(ref Entity entity, ref TestComp comp) { } void update(EntitiesData data) { } /*void handleEvent(Event event, ref TestComp comp) { }*/ } import std.meta; struct TestSystem2 { mixin ECS.System!16;//__gshared ushort system_id; enum AbsentComponents0 { TestComp, TestComp4 } alias AbsentComponents = AliasSeq!("TestComp", "TestComp4"); string AbsentComponents2; static struct EntitiesData { short length; const (Entity)[] entity; TestComp3[] test; //@absent TestComp[] testt; } void onEnable() { import std.stdio; writeln("TestSystem2 enabled"); } void onDisable() { import std.stdio; writeln("TestSystem2 disabled"); } void initialize(ref Entity entity, ref TestComp comp) { } void update(ref EntitiesData data) { foreach(i;0..data.test.length) { data.test[i].gg += 14; 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 dispatch(EntityManager.JobGroup jobs) { foreach(job;jobs.jobs) { //writeln(job); job.execute(); } } 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(1); gEM.setJobDispachFunc(&dispatch); assert(gEM !is null); MonoTime time = MonoTime.currTime; gEM.registerComponent!TestComp2; gEM.registerComponent!TestComp4; gEM.registerComponent!TestComp; gEM.registerComponent!TestComp3; gEM.registerEvent!TestEvent; gEM.registerEvent!TestEvent2; ulong dur = (MonoTime.currTime - time).total!"usecs"; writeln("Components register: ", dur, " usecs"); time = MonoTime.currTime; gEM.registerSystem!TestSystemWithHighPriority(100); gEM.registerSystem!TestSystem(0); //gEM.registerSystem!TestSystemWithHighPriority(100); //gEM.registerSystem!TestSystem2(0); dur = (MonoTime.currTime - time).total!"usecs"; writeln("Systems register: ", dur, " usecs"); time = MonoTime.currTime; //ushort[3] ids = [TestComp2.component_id, TestComp.component_id, TestComp4.component_id]; ushort[2] ids = [TestComp2.component_id, TestComp.component_id]; EntityTemplate* tmpl = gEM.allocateTemplate(ids); //ushort[3] ids2 = [TestComp3.component_id, TestComp.component_id, TestComp4.component_id]; ushort[2] ids2 = [TestComp3.component_id, TestComp.component_id]; EntityTemplate* tmpl2 = gEM.allocateTemplate(ids2); //writeln(tmpl.info.components[]); //*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); //foreach(i; 0..1_000_000)gEM.removeEntity(gEM.addEntity(tmpl).id); EntityID[5000] idss; foreach (i; 0 .. 200) { gEM.begin(); foreach (j; 0 .. 5_000) idss[j] = gEM.addEntity(tmpl).id; foreach (j; 0 .. 5_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) { EntityManager.EntitiesBlock* block = info.first_block; while (block !is null) { block = block.next_block; blocks++; } } writeln("Entities blocks: ", blocks); //foreach(j; 0..1_000)gEM.addEntity(tmpl); gEM.registerSystem!TestSystem2(0); gEM.generateDependencies(); //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)); Entity entity2; foreach (i; 0 .. 500_000) { entity2 = gEM.addEntity(tmpl); gEM.addEntity(tmpl2); } time = MonoTime.currTime; gEM.begin(); //gEM.updateMT(); gEM.update(); gEM.end(); dur = (MonoTime.currTime - time).total!"usecs"; writeln("Update: ", dur, " usecs"); writeEntityComponents(gEM.getEntity(entity2.id)); time = MonoTime.currTime; gEM.begin(); gEM.updateMT(); //gEM.update(); gEM.end(); dur = (MonoTime.currTime - time).total!"usecs"; writeln("Update: ", dur, " usecs"); writeEntityComponents(gEM.getEntity(entity2.id)); time = MonoTime.currTime; gEM.begin(); gEM.updateMT(); //gEM.update(); gEM.end(); dur = (MonoTime.currTime - time).total!"usecs"; writeln("Update: ", dur, " usecs"); writeEntityComponents(gEM.getEntity(entity2.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(); writeEntityComponents(gEM.getEntity(entity.id)); gEM.removeComponents!(TestComp)(entity.id); gEM.addComponents(entity.id, TestComp()); gEM.begin(); gEM.update(); gEM.end(); writeEntityComponents(gEM.getEntity(entity.id)); //import std.stdio; //writeln((cast(uint*)tmpl.info.first_block)[0..48]); gEM.freeTemplate(tmpl); EntityManager.destroy(); return 0; }