using System; using Svelto.DataStructures; using Svelto.ECS; using Svelto.ECS.Hybrid; using Svelto.ECS.Internal; namespace TechbloxModdingAPI.Utility { public readonly ref struct RefCollection where T : struct, IBaseEntityComponent { private readonly bool managed; private readonly int count; private readonly NB nativeArray; private readonly MB managedArray; private readonly NativeEntityIDs nativeIDs; private readonly ManagedEntityIDs managedIDs; private readonly ExclusiveGroupStruct group; public RefCollection(int count, MB managedArray, ManagedEntityIDs managedIDs, ExclusiveGroupStruct group) { this.count = count; this.managedArray = managedArray; this.managedIDs = managedIDs; this.group = group; managed = true; nativeArray = default; nativeIDs = default; } public RefCollection(int count, NB nativeArray, NativeEntityIDs nativeIDs, ExclusiveGroupStruct group) { this.count = count; this.nativeArray = nativeArray; this.nativeIDs = nativeIDs; this.group = group; managed = false; } public Enumerator GetEnumerator() => new(this); /// /// The amount of items in the collection. /// public int Count => count; public T[] ToArray() => ToArray(a => a.Component); public TA[] ToArray(Func<(T Component, EGID ID), TA> transformFunction, Predicate<(T Component, EGID ID)> predicateFunction = null) { var result = new TA[Count]; int i = 0; foreach (var opt in this) { if (predicateFunction != null && !predicateFunction((opt.Get(), opt.EGID))) continue; result[i] = transformFunction((opt.Get(), opt.EGID)); i++; } return result; } public ref struct Enumerator { private RefCollection coll; private int index; public Enumerator(RefCollection collection) { index = -1; coll = collection; } public OptionalRef Current { get { if (coll.count <= index && index >= 0) return default; if (coll.managed) return new OptionalRef(coll.managedArray, (uint)index, new EGID(coll.managedIDs[index], coll.group)); return new OptionalRef(coll.nativeArray, (uint)index, new EGID(coll.nativeIDs[index], coll.group)); } } public bool MoveNext() { return ++index < coll.count; } } } }