1 module tests.bugs; 2 3 import tests.basic; 4 5 import bubel.ecs.core; 6 import bubel.ecs.manager; 7 import bubel.ecs.system; 8 import bubel.ecs.attributes; 9 10 version(GNU) 11 { 12 pragma(inline, true) T[n] staticArray(T, size_t n)(auto ref T[n] a) 13 { 14 return a; 15 } 16 } 17 else import std.array : staticArray; 18 19 @("Bug0001") 20 unittest 21 { 22 struct Event1 23 { 24 // mixin ECS.Event; 25 26 EntityID id; 27 } 28 29 struct Event2 30 { 31 // mixin ECS.Event; 32 } 33 34 struct System1 35 { 36 mixin ECS.System; 37 38 struct EntitiesData 39 { 40 CInt[] int_; 41 } 42 43 EntityTemplate* tmpl; 44 EntityID id; 45 46 void onCreate() 47 { 48 tmpl = gEntityManager.allocateTemplate([becsID!CInt, becsID!CLong].staticArray); 49 } 50 51 void onDestroy() 52 { 53 gEntityManager.freeTemplate(tmpl); 54 } 55 56 void handleEvent(Entity* entity, Event1 event) 57 { 58 gEntityManager.removeEntity(event.id); 59 gEntityManager.sendEvent(entity.id,Event2()); 60 } 61 62 void handleEvent(Entity* entity, Event2 event) 63 { 64 id = gEntityManager.addEntity(tmpl).id; 65 } 66 } 67 68 struct System2 69 { 70 mixin ECS.System; 71 72 struct EntitiesData 73 { 74 Entity[] entity; 75 } 76 77 ///check if every entity was removed correctly 78 void onUpdate(EntitiesData data) 79 { 80 assert(0); 81 } 82 } 83 84 struct System3 85 { 86 mixin ECS.System; 87 88 struct EntitiesData 89 { 90 uint length; 91 Entity[] entity; 92 } 93 94 ///remove every entity 95 void onUpdate(EntitiesData data) 96 { 97 foreach(i;0..data.length)gEntityManager.removeEntity(data.entity[i].id); 98 } 99 } 100 101 gEntityManager.initialize(0); 102 103 gEntityManager.beginRegister(); 104 105 gEntityManager.registerComponent!CInt; 106 gEntityManager.registerComponent!CFloat; 107 gEntityManager.registerComponent!CDouble; 108 gEntityManager.registerComponent!CLong; 109 gEntityManager.registerComponent!CShort; 110 gEntityManager.registerComponent!CFlag; 111 112 gEntityManager.registerEvent!Event1; 113 gEntityManager.registerEvent!Event2; 114 115 gEntityManager.registerSystem!System1(0); 116 gEntityManager.registerSystem!System2(-200); 117 gEntityManager.registerSystem!System3(-200); 118 119 gEntityManager.endRegister(); 120 121 EntityTemplate* tmpl = gEntityManager.allocateTemplate([becsID!CInt, becsID!CLong].staticArray); 122 123 CLong clong = CLong(10); 124 CInt cint = CInt(6); 125 CInt cint2 = CInt(4); 126 EntityID id = gEntityManager.addEntity(tmpl,[ComponentRef(&clong, becsID(clong)), ComponentRef(&cint, becsID(cint))].staticArray).id; 127 EntityID id2 = gEntityManager.addEntity(tmpl,[ComponentRef(&cint2, becsID(cint2))].staticArray).id; 128 gEntityManager.freeTemplate(tmpl); 129 gEntityManager.commit(); 130 131 gEntityManager.sendEvent(id2, Event1(id)); 132 133 gEntityManager.getSystem(becsID!System2).disable(); 134 135 gEntityManager.begin(); 136 gEntityManager.update(); 137 gEntityManager.end(); 138 139 gEntityManager.getSystem(becsID!System2).enable(); 140 141 gEntityManager.begin(); 142 gEntityManager.update(); 143 gEntityManager.end(); 144 145 gEntityManager.destroy(); 146 } 147 148 @("2-empty-entity-crash") 149 unittest 150 { 151 152 gEntityManager.initialize(0); 153 154 gEntityManager.beginRegister(); 155 156 gEntityManager.registerComponent!CInt; 157 gEntityManager.registerComponent!CFloat; 158 159 gEntityManager.endRegister(); 160 161 162 EntityTemplate* tmpl = gEntityManager.allocateTemplate([becsID!CInt, becsID!CFloat].staticArray); 163 scope(exit) gEntityManager.freeTemplate(tmpl); 164 165 EntityID id = gEntityManager.addEntity(tmpl).id; 166 // EntityID id2 = gEntityManager.addEntity().id; 167 168 gEntityManager.commit(); 169 170 gEntityManager.removeComponents(id, [becsID!CInt, becsID!CFloat].staticArray); 171 172 gEntityManager.commit(); 173 174 gEntityManager.destroy(); 175 }