diff --git a/README.md b/README.md index ab5da9f..86a2161 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,28 @@ query.Foreach( (Entity entity, ref int value ) => // you can access the Console.WriteLine($"{entity} value is {value}"); }); +foreach ((Entity entity, int value) in query.Foreach()) // foreach makes so you can use continue and break +{ + if (value > 0) + { + continue; + } + + if (value < 0) + { + break; + } + + Console.WriteLine($"{entity} value is {value}"); +} + +// having more then 5 components makes so you have to put double parenthesis. +// the reason is that a tuple only can hold in 8 values, so you create a second tuple in the tuple +foreach ((Entity entity, A aa, B bb, C cc, D dd, E ee, (F ff, G gg, H hh, I ii, J jj)) in query.Foreach()) +{ + Console.WriteLine($"value x is {aa.x} value y is {aa.y}"); +} + var all_entities = world.CreateQuery(); // a simple way to match against all entities is to make a query with no filters query.GetEntities(); // returns a copy of all entities currently matching the query diff --git a/SimpleECS/QueryForeachFunctions.cs b/SimpleECS/QueryForeachFunctions.cs index 7aacb7f..f090c75 100644 --- a/SimpleECS/QueryForeachFunctions.cs +++ b/SimpleECS/QueryForeachFunctions.cs @@ -3278,5 +3278,294 @@ public void Foreach + /// iterates over the entities that match the query and returns a tuple of the entity and the components + /// possible to use continue and break + /// NOTE: it is only possible to modity a refernce type in the tuple, like a class. You can not modify a value type, like int, float, bool, etc. + /// + public IEnumerable> Foreach() + { + if (Update(out var world_info)) + { + world_info.StructureEvents.EnqueueEvents++; + for (int archetype_index = 0; archetype_index < archetype_count; ++archetype_index) + { + var archetype = world_info.archetypes[matching_archetypes[archetype_index]].data; + int count = archetype.entity_count; + var entities = archetype.entities; + if (count > 0 && archetype.TryGetArray(out C1[] c1)) + { + for (int e = 0; e < count; ++e) + yield return Tuple.Create(entities[e], c1[e]); + } + } + world_info.StructureEvents.EnqueueEvents--; + } + } + /// + /// iterates over the entities that match the query and returns a tuple of the entity and the components + /// possible to use continue and break + /// NOTE: it is only possible to modity a refernce type in the tuple, like a class. You can not modify a value type, like int, float, bool, etc. + /// + public IEnumerable> Foreach() + { + if (Update(out var world_info)) + { + world_info.StructureEvents.EnqueueEvents++; + for (int archetype_index = 0; archetype_index < archetype_count; ++archetype_index) + { + var archetype = world_info.archetypes[matching_archetypes[archetype_index]].data; + int count = archetype.entity_count; + var entities = archetype.entities; + if (count > 0 && archetype.TryGetArray(out C1[] c1) && archetype.TryGetArray(out C2[] c2)) + { + for (int e = 0; e < count; ++e) + yield return Tuple.Create(entities[e], c1[e], c2[e]); + } + } + world_info.StructureEvents.EnqueueEvents--; + } + } + /// + /// iterates over the entities that match the query and returns a tuple of the entity and the components + /// possible to use continue and break + /// NOTE: it is only possible to modity a refernce type in the tuple, like a class. You can not modify a value type, like int, float, bool, etc. + /// + public IEnumerable> Foreach() + { + if (Update(out var world_info)) + { + world_info.StructureEvents.EnqueueEvents++; + for (int archetype_index = 0; archetype_index < archetype_count; ++archetype_index) + { + var archetype = world_info.archetypes[matching_archetypes[archetype_index]].data; + int count = archetype.entity_count; + var entities = archetype.entities; + + if (count > 0 && archetype.TryGetArray(out C1[] c1) && archetype.TryGetArray(out C2[] c2) && archetype.TryGetArray(out C3[] c3)) + { + for (int e = 0; e < count; ++e) + yield return Tuple.Create(entities[e], c1[e], c2[e], c3[e]); + } + } + world_info.StructureEvents.EnqueueEvents--; + } + } + /// + /// iterates over the entities that match the query and returns a tuple of the entity and the components + /// possible to use continue and break + /// NOTE: it is only possible to modity a refernce type in the tuple, like a class. You can not modify a value type, like int, float, bool, etc. + /// + public IEnumerable> Foreach() + { + if (Update(out var world_info)) + { + world_info.StructureEvents.EnqueueEvents++; + for (int archetype_index = 0; archetype_index < archetype_count; ++archetype_index) + { + var archetype = world_info.archetypes[matching_archetypes[archetype_index]].data; + int count = archetype.entity_count; + var entities = archetype.entities; + if (count > 0 && archetype.TryGetArray(out C1[] c1) && archetype.TryGetArray(out C2[] c2) && archetype.TryGetArray(out C3[] c3) && archetype.TryGetArray(out C4[] c4)) + { + for (int e = 0; e < count; ++e) + yield return Tuple.Create(entities[e], c1[e], c2[e], c3[e], c4[e]); + } + } + world_info.StructureEvents.EnqueueEvents--; + } + } + /// + /// iterates over the entities that match the query and returns a tuple of the entity and the components + /// possible to use continue and break + /// NOTE: it is only possible to modity a refernce type in the tuple, like a class. You can not modify a value type, like int, float, bool, etc. + /// + public IEnumerable> Foreach() + { + if (Update(out var world_info)) + { + world_info.StructureEvents.EnqueueEvents++; + for (int archetype_index = 0; archetype_index < archetype_count; ++archetype_index) + { + var archetype = world_info.archetypes[matching_archetypes[archetype_index]].data; + int count = archetype.entity_count; + var entities = archetype.entities; + if (count > 0 && archetype.TryGetArray(out C1[] c1) && archetype.TryGetArray(out C2[] c2) && archetype.TryGetArray(out C3[] c3) && archetype.TryGetArray(out C4[] c4) && archetype.TryGetArray(out C5[] c5)) + { + for (int e = 0; e < count; ++e) + yield return Tuple.Create(entities[e], c1[e], c2[e], c3[e], c4[e], c5[e]); + } + } + world_info.StructureEvents.EnqueueEvents--; + } + } + /// + /// iterates over the entities that match the query and returns a tuple of the entity and the components + /// possible to use continue and break + /// NOTE: it is only possible to modity a refernce type in the tuple, like a class. You can not modify a value type, like int, float, bool, etc. + /// + public IEnumerable> Foreach() + { + if (Update(out var world_info)) + { + world_info.StructureEvents.EnqueueEvents++; + for (int archetype_index = 0; archetype_index < archetype_count; ++archetype_index) + { + var archetype = world_info.archetypes[matching_archetypes[archetype_index]].data; + int count = archetype.entity_count; + var entities = archetype.entities; + if (count > 0 && archetype.TryGetArray(out C1[] c1) && archetype.TryGetArray(out C2[] c2) && archetype.TryGetArray(out C3[] c3) && archetype.TryGetArray(out C4[] c4) && archetype.TryGetArray(out C5[] c5) && archetype.TryGetArray(out C6[] c6)) + { + for (int e = 0; e < count; ++e) + yield return Tuple.Create(entities[e], c1[e], c2[e], c3[e], c4[e], c5[e], c6[e]); + } + } + world_info.StructureEvents.EnqueueEvents--; + } + } + /// + /// iterates over the entities that match the query and returns a tuple of the entity and the components + /// possible to use continue and break + /// NOTE: it is only possible to modity a refernce type in the tuple, like a class. You can not modify a value type, like int, float, bool, etc. + /// + public IEnumerable>> Foreach() + { + if (Update(out var world_info)) + { + world_info.StructureEvents.EnqueueEvents++; + for (int archetype_index = 0; archetype_index < archetype_count; ++archetype_index) + { + var archetype = world_info.archetypes[matching_archetypes[archetype_index]].data; + int count = archetype.entity_count; + var entities = archetype.entities; + if (count > 0 && archetype.TryGetArray(out C1[] c1) && archetype.TryGetArray(out C2[] c2) && archetype.TryGetArray(out C3[] c3) && archetype.TryGetArray(out C4[] c4) && archetype.TryGetArray(out C5[] c5) && archetype.TryGetArray(out C6[] c6) && archetype.TryGetArray(out C7[] c7)) + { + for (int e = 0; e < count; ++e) + yield return Tuple.Create(entities[e], c1[e], c2[e], c3[e], c4[e], c5[e], Tuple.Create(c6[e], c7[e])); + } + } + world_info.StructureEvents.EnqueueEvents--; + } + } + /// + /// iterates over the entities that match the query and returns a tuple of the entity and the components + /// possible to use continue and break + /// NOTE: it is only possible to modity a refernce type in the tuple, like a class. You can not modify a value type, like int, float, bool, etc. + /// + public IEnumerable>> Foreach() + { + if (Update(out var world_info)) + { + world_info.StructureEvents.EnqueueEvents++; + for (int archetype_index = 0; archetype_index < archetype_count; ++archetype_index) + { + var archetype = world_info.archetypes[matching_archetypes[archetype_index]].data; + int count = archetype.entity_count; + var entities = archetype.entities; + if (count > 0 && archetype.TryGetArray(out C1[] c1) && archetype.TryGetArray(out C2[] c2) && archetype.TryGetArray(out C3[] c3) && archetype.TryGetArray(out C4[] c4) && archetype.TryGetArray(out C5[] c5) && archetype.TryGetArray(out C6[] c6) && archetype.TryGetArray(out C7[] c7) && archetype.TryGetArray(out C8[] c8)) + { + for (int e = 0; e < count; ++e) + yield return Tuple.Create(entities[e], c1[e], c2[e], c3[e], c4[e], c5[e], Tuple.Create(c6[e], c7[e], c8[e])); + } + } + world_info.StructureEvents.EnqueueEvents--; + } + } + /// + /// iterates over the entities that match the query and returns a tuple of the entity and the components + /// possible to use continue and break + /// NOTE: it is only possible to modity a refernce type in the tuple, like a class. You can not modify a value type, like int, float, bool, etc. + /// + public IEnumerable>> Foreach() + { + if (Update(out var world_info)) + { + world_info.StructureEvents.EnqueueEvents++; + for (int archetype_index = 0; archetype_index < archetype_count; ++archetype_index) + { + var archetype = world_info.archetypes[matching_archetypes[archetype_index]].data; + int count = archetype.entity_count; + var entities = archetype.entities; + if (count > 0 && archetype.TryGetArray(out C1[] c1) && archetype.TryGetArray(out C2[] c2) && archetype.TryGetArray(out C3[] c3) && archetype.TryGetArray(out C4[] c4) && archetype.TryGetArray(out C5[] c5) && archetype.TryGetArray(out C6[] c6) && archetype.TryGetArray(out C7[] c7) && archetype.TryGetArray(out C8[] c8) && archetype.TryGetArray(out C9[] c9)) + { + for (int e = 0; e < count; ++e) + yield return Tuple.Create(entities[e], c1[e], c2[e], c3[e], c4[e], c5[e], Tuple.Create(c6[e], c7[e], c8[e], c9[e])); + } + } + world_info.StructureEvents.EnqueueEvents--; + } + } + /// + /// iterates over the entities that match the query and returns a tuple of the entity and the components + /// possible to use continue and break + /// NOTE: it is only possible to modity a refernce type in the tuple, like a class. You can not modify a value type, like int, float, bool, etc. + /// + public IEnumerable>> Foreach() + { + if (Update(out var world_info)) + { + world_info.StructureEvents.EnqueueEvents++; + for (int archetype_index = 0; archetype_index < archetype_count; ++archetype_index) + { + var archetype = world_info.archetypes[matching_archetypes[archetype_index]].data; + int count = archetype.entity_count; + var entities = archetype.entities; + if (count > 0 && archetype.TryGetArray(out C1[] c1) && archetype.TryGetArray(out C2[] c2) && archetype.TryGetArray(out C3[] c3) && archetype.TryGetArray(out C4[] c4) && archetype.TryGetArray(out C5[] c5) && archetype.TryGetArray(out C6[] c6) && archetype.TryGetArray(out C7[] c7) && archetype.TryGetArray(out C8[] c8) && archetype.TryGetArray(out C9[] c9) && archetype.TryGetArray(out C10[] c10)) + { + for (int e = 0; e < count; ++e) + yield return Tuple.Create(entities[e], c1[e], c2[e], c3[e], c4[e], c5[e], Tuple.Create(c6[e], c7[e], c8[e], c9[e], c10[e])); + } + } + world_info.StructureEvents.EnqueueEvents--; + } + } + /// + /// iterates over the entities that match the query and returns a tuple of the entity and the components + /// possible to use continue and break + /// NOTE: it is only possible to modity a refernce type in the tuple, like a class. You can not modify a value type, like int, float, bool, etc. + /// + public IEnumerable>> Foreach() + { + if (Update(out var world_info)) + { + world_info.StructureEvents.EnqueueEvents++; + for (int archetype_index = 0; archetype_index < archetype_count; ++archetype_index) + { + var archetype = world_info.archetypes[matching_archetypes[archetype_index]].data; + int count = archetype.entity_count; + var entities = archetype.entities; + if (count > 0 && archetype.TryGetArray(out C1[] c1) && archetype.TryGetArray(out C2[] c2) && archetype.TryGetArray(out C3[] c3) && archetype.TryGetArray(out C4[] c4) && archetype.TryGetArray(out C5[] c5) && archetype.TryGetArray(out C6[] c6) && archetype.TryGetArray(out C7[] c7) && archetype.TryGetArray(out C8[] c8) && archetype.TryGetArray(out C9[] c9) && archetype.TryGetArray(out C10[] c10) && archetype.TryGetArray(out C11[] c11)) + { + for (int e = 0; e < count; ++e) + yield return Tuple.Create(entities[e], c1[e], c2[e], c3[e], c4[e], c5[e], Tuple.Create(c6[e], c7[e], c8[e], c9[e], c10[e], c11[e])); + } + } + world_info.StructureEvents.EnqueueEvents--; + } + } + /// + /// iterates over the entities that match the query and returns a tuple of the entity and the components + /// possible to use continue and break + /// NOTE: it is only possible to modity a refernce type in the tuple, like a class. You can not modify a value type, like int, float, bool, etc. + /// + public IEnumerable>> Foreach() + { + if (Update(out var world_info)) + { + world_info.StructureEvents.EnqueueEvents++; + for (int archetype_index = 0; archetype_index < archetype_count; ++archetype_index) + { + var archetype = world_info.archetypes[matching_archetypes[archetype_index]].data; + int count = archetype.entity_count; + var entities = archetype.entities; + if (count > 0 && archetype.TryGetArray(out C1[] c1) && archetype.TryGetArray(out C2[] c2) && archetype.TryGetArray(out C3[] c3) && archetype.TryGetArray(out C4[] c4) && archetype.TryGetArray(out C5[] c5) && archetype.TryGetArray(out C6[] c6) && archetype.TryGetArray(out C7[] c7) && archetype.TryGetArray(out C8[] c8) && archetype.TryGetArray(out C9[] c9) && archetype.TryGetArray(out C10[] c10) && archetype.TryGetArray(out C11[] c11) && archetype.TryGetArray(out C12[] c12)) + { + for (int e = 0; e < count; ++e) + yield return Tuple.Create(entities[e], c1[e], c2[e], c3[e], c4[e], c5[e], Tuple.Create(c6[e], c7[e], c8[e], c9[e], c10[e], c11[e], c12[e])); + } + } + world_info.StructureEvents.EnqueueEvents--; + } + } } } \ No newline at end of file