|
- using System;
- using Svelto.DataStructures;
- using Svelto.ECS;
- using Svelto.ECS.Hybrid;
- using Svelto.ECS.Internal;
-
- namespace TechbloxModdingAPI.Utility
- {
- public readonly ref struct RefCollection<T> where T : struct, IBaseEntityComponent
- {
- private readonly bool managed;
- private readonly int count;
- private readonly NB<T> nativeArray;
- private readonly MB<T> managedArray;
- private readonly NativeEntityIDs nativeIDs;
- private readonly ManagedEntityIDs managedIDs;
- private readonly ExclusiveGroupStruct group;
-
- public RefCollection(int count, MB<T> 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<T> nativeArray, NativeEntityIDs nativeIDs, ExclusiveGroupStruct group)
- {
- this.count = count;
- this.nativeArray = nativeArray;
- this.nativeIDs = nativeIDs;
- this.group = group;
- managed = false;
- managedArray = default;
- managedIDs = default;
- }
-
- public Enumerator GetEnumerator() => new(this);
-
- /// <summary>
- /// The amount of items in the collection.
- /// </summary>
- public int Count => count;
-
- public T[] ToArray() => ToArray(a => a.Component);
-
- public TA[] ToArray<TA>(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<T> coll;
- private int index;
-
- public Enumerator(RefCollection<T> collection)
- {
- index = -1;
- coll = collection;
- }
-
- public OptionalRef<T> Current
- {
- get
- {
- if (coll.count <= index && index >= 0) return default;
- if (coll.managed)
- return new OptionalRef<T>(coll.managedArray, (uint)index,
- new EGID(coll.managedIDs[index], coll.group));
- return new OptionalRef<T>(coll.nativeArray, (uint)index,
- new EGID(coll.nativeIDs[index], coll.group));
- }
- }
-
- public bool MoveNext()
- {
- return ++index < coll.count;
- }
- }
- }
- }
|