diff --git a/Engine/src/ECS/Archetype/Entities.cs b/Engine/src/ECS/Archetype/Entities.cs new file mode 100644 index 000000000..8aaad5082 --- /dev/null +++ b/Engine/src/ECS/Archetype/Entities.cs @@ -0,0 +1,81 @@ +// Copyright (c) Ullrich Praetz - https://github.com/friflo. All rights reserved. +// See LICENSE file in the project root for full license information. + +using System.Collections; +using System.Collections.Generic; + +// ReSharper disable once CheckNamespace +namespace Friflo.Engine.ECS; + +public readonly struct Entities : IReadOnlyList +{ +#region properties + public int Count => count; + #endregion + +#region interal fields + internal readonly int[] ids; // 8 + internal readonly EntityStore store; // 8 + internal readonly int start; // 4 + public readonly int count; // 4 + #endregion + +#region general + internal Entities(int[] ids, EntityStore store, int start, int count) { + this.ids = ids; + this.store = store; + this.start = start; + this.count = count; + } + + public Entity this[int index] => throw new System.NotImplementedException(); + #endregion + + +#region IEnumerator + public EntityEnumerator GetEnumerator() => new EntityEnumerator (this); + + // --- IEnumerable + IEnumerator IEnumerable.GetEnumerator() => new EntityEnumerator (this); + + // --- IEnumerable<> + IEnumerator IEnumerable.GetEnumerator() => new EntityEnumerator (this); + #endregion +} + + +public struct EntityEnumerator : IEnumerator +{ + private readonly int[] ids; // 8 + private readonly EntityStore store; // 8 + private readonly int start; // 4 + private readonly int last; // 8 + private int index; // 4 + + internal EntityEnumerator(in Entities entities) { + ids = entities.ids; + store = entities.store; + start = entities.start; + last = start + entities.count; + index = start; + } + + // --- IEnumerator + public void Reset() => index = start; + + readonly object IEnumerator.Current => new Entity(store, ids[index]); + + public Entity Current => new Entity(store, ids[index]); + + // --- IEnumerator + public bool MoveNext() + { + if (index < last) { + index++; + return true; + } + return false; + } + + public readonly void Dispose() { } +} \ No newline at end of file diff --git a/Engine/src/ECS/Entity/Store/Entities.cs b/Engine/src/ECS/Entity/Store/Entities.cs index cc5c4889b..4bf43072c 100644 --- a/Engine/src/ECS/Entity/Store/Entities.cs +++ b/Engine/src/ECS/Entity/Store/Entities.cs @@ -246,9 +246,3 @@ internal void DeleteEntityEvent(Entity entity) } } -/// -/// Reserved symbol name. -/// If exposing public it needs to store an array of 's.
-/// Similar to . -///
-internal struct Entities; \ No newline at end of file