diff --git a/src/EcsFilter.cs b/src/EcsFilter.cs index 5056930..13beba7 100644 --- a/src/EcsFilter.cs +++ b/src/EcsFilter.cs @@ -7,7 +7,7 @@ namespace DCFApixels.DragonECS #region Properties public IEcsWorld World { get; } public EcsMask Mask { get; } - public IEcsReadonlyGroup Entities { get; } + public EcsReadonlyGroup Entities { get; } public int EntitiesCount { get; } #endregion } @@ -29,10 +29,10 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] get => _mask; } - public IEcsReadonlyGroup Entities + public EcsReadonlyGroup Entities { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => _entities; + get => _entities.Readonly; } public int EntitiesCount { diff --git a/src/EcsGroup.cs b/src/EcsGroup.cs index 40576a6..d6353f1 100644 --- a/src/EcsGroup.cs +++ b/src/EcsGroup.cs @@ -1,26 +1,33 @@ using System; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using delayedOp = System.Int32; namespace DCFApixels.DragonECS { - public interface IEcsReadonlyGroup + [StructLayout(LayoutKind.Sequential, Pack = 0, Size = 8)] + public readonly ref struct EcsReadonlyGroup { - int Count { get; } - bool Contains(int entityID); - EcsGroup.Enumerator GetEnumerator(); - } - public interface IEcsGroup : IEcsReadonlyGroup - { - void Add(int entityID); - void Remove(int entityID); + private readonly EcsGroup _source; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public EcsReadonlyGroup(EcsGroup source) => _source = source; + public int Count + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => _source.Count; + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool Contains(int entityID) => _source.Contains(entityID); + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public EcsGroup.Enumerator GetEnumerator() => _source.GetEnumerator(); } // не может содержать значение 0 // _delayedOps это int[] для отложенных операций, хранятся отложенные операции в виде int значения, если старший бит = 0 то это опреация добавленияб если = 1 то это операция вычитания // this collection can only store numbers greater than 0 - public class EcsGroup : IEcsGroup + public class EcsGroup { public const int DEALAYED_ADD = 0; public const int DEALAYED_REMOVE = int.MinValue; @@ -44,6 +51,11 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] get => _count; } + public EcsReadonlyGroup Readonly + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => new EcsReadonlyGroup(this); + } #endregion #region Constrcutors @@ -86,7 +98,7 @@ namespace DCFApixels.DragonECS if(++_count >= _dense.Length) Array.Resize(ref _dense, _dense.Length << 1); - if (entityID > _sparse.Length) + if (entityID >= _sparse.Length) { int neadedSpace = _sparse.Length; while (entityID >= neadedSpace) @@ -128,20 +140,21 @@ namespace DCFApixels.DragonECS //TODO добавить автосоритровку при каждом GetEnumerator #region AddGroup/RemoveGroup - public void AddGroup(IEcsReadonlyGroup group) + public void AddGroup(EcsReadonlyGroup group) { - foreach (var item in group) - { - Add(item.id); - } + foreach (var item in group) Add(item.id); } - - public void RemoveGroup(IEcsReadonlyGroup group) + public void RemoveGroup(EcsReadonlyGroup group) { - foreach (var item in group) - { - Remove(item.id); - } + foreach (var item in group) Remove(item.id); + } + public void AddGroup(EcsGroup group) + { + foreach (var item in group) Add(item.id); + } + public void RemoveGroup(EcsGroup group) + { + foreach (var item in group) Remove(item.id); } #endregion @@ -179,7 +192,7 @@ namespace DCFApixels.DragonECS } #endregion - #region Utils + #region Enumerator public struct Enumerator : IDisposable { private readonly EcsGroup _source; diff --git a/src/EcsPool.cs b/src/EcsPool.cs index ceba905..e25c161 100644 --- a/src/EcsPool.cs +++ b/src/EcsPool.cs @@ -6,7 +6,11 @@ namespace DCFApixels.DragonECS { public interface IEcsPool { + public int EntitiesCount { get; } + public int Capacity { get; } + public IEcsWorld World { get; } + public Type DataType { get; } public int ID { get; } public bool Has(int index); public void Write(int index); @@ -30,7 +34,12 @@ namespace DCFApixels.DragonECS } public IEcsWorld World => _source; + public Type DataType => typeof(void); public int ID => -1; + + public int EntitiesCount => 0; + public int Capacity => 1; + public void Del(int index) { } public bool Has(int index) => false; public void Write(int index) { } @@ -47,7 +56,11 @@ namespace DCFApixels.DragonECS private IEcsComponentReset _componentResetHandler; #region Properites + public int EntitiesCount => _sparseSet.Count; + public int Capacity => _sparseSet.CapacityDense; + public IEcsWorld World => _source; + public Type DataType => typeof(T); public int ID => _id; #endregion @@ -112,6 +125,7 @@ namespace DCFApixels.DragonECS #endregion } + #region ComponentResetHandler internal static class ComponentResetHandler { [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -137,4 +151,5 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Reset(ref T component) => _fakeInstnace.Reset(ref component); } + #endregion } diff --git a/src/EcsWorld.cs b/src/EcsWorld.cs index 6c05a43..5c29815 100644 --- a/src/EcsWorld.cs +++ b/src/EcsWorld.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Runtime.CompilerServices; namespace DCFApixels.DragonECS @@ -13,6 +14,12 @@ namespace DCFApixels.DragonECS public int ID { get; } #endregion + + #region GetterMethods + public ReadOnlySpan GetAllPools(); + + #endregion + #region Methods public EcsPool GetPool() where T : struct; public EcsFilter Filter() where TInc : struct, IInc; @@ -68,6 +75,11 @@ namespace DCFApixels.DragonECS private EcsFilter[] _filters; + #region GetterMethods + public ReadOnlySpan GetAllPools() => new ReadOnlySpan(_pools); + + #endregion + #region Properties public bool IsEmpty => _entities.Count < 0; public Type ArchetypeType => typeof(TArchetype);