From 29dc82568542a5e05bdbd10fe994da5064218bac Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Sun, 25 Jun 2023 23:13:51 +0800 Subject: [PATCH] simple refactoring --- src/EcsAspect.cs | 6 +++--- src/Pools/EcsPool.cs | 23 +++++++++-------------- src/Pools/EcsPoolBase.cs | 7 ++++++- src/Pools/EcsTagPool.cs | 18 ++++++++---------- src/entlong.cs | 2 +- 5 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/EcsAspect.cs b/src/EcsAspect.cs index 3c47c0b..6a6a2fc 100644 --- a/src/EcsAspect.cs +++ b/src/EcsAspect.cs @@ -179,10 +179,10 @@ namespace DCFApixels.DragonECS } #endregion - private struct Combined + private readonly struct Combined { - public EcsAspect aspect; - public int order; + public readonly EcsAspect aspect; + public readonly int order; public Combined(EcsAspect aspect, int order) { this.aspect = aspect; diff --git a/src/Pools/EcsPool.cs b/src/Pools/EcsPool.cs index cc1cb6d..131b2ff 100644 --- a/src/Pools/EcsPool.cs +++ b/src/Pools/EcsPool.cs @@ -7,11 +7,11 @@ using static DCFApixels.DragonECS.EcsPoolThrowHalper; namespace DCFApixels.DragonECS { /// Pool for IEcsComponent components - public sealed class EcsPool : IEcsPoolImplementation, IEcsStructsPool, IEnumerable //IEnumerable - IntelliSense hack + public sealed class EcsPool : IEcsPoolImplementation, IEcsStructPool, IEnumerable //IEnumerable - IntelliSense hack where T : struct, IEcsComponent { private EcsWorld _source; - private int _id; + private int _componentID; private int[] _mapping;// index = entityID / value = itemIndex;/ value = 0 = no entityID private T[] _items; //dense @@ -19,15 +19,15 @@ namespace DCFApixels.DragonECS private int[] _recycledItems; private int _recycledItemsCount; - private IEcsComponentReset _componentResetHandler; - private IEcsComponentCopy _componentCopyHandler; + private IEcsComponentReset _componentResetHandler = EcsComponentResetHandler.instance; + private IEcsComponentCopy _componentCopyHandler = EcsComponentCopyHandler.instance; - private List _listeners; + private List _listeners = new List(); #region Properites public int Count => _itemsCount; public int Capacity => _items.Length; - public int ComponentID => _id; + public int ComponentID => _componentID; public Type ComponentType => typeof(T); public EcsWorld World => _source; #endregion @@ -36,7 +36,7 @@ namespace DCFApixels.DragonECS void IEcsPoolImplementation.OnInit(EcsWorld world, int componentID) { _source = world; - _id = componentID; + _componentID = componentID; const int capacity = 512; @@ -45,11 +45,6 @@ namespace DCFApixels.DragonECS _recycledItemsCount = 0; _items = new T[capacity]; _itemsCount = 0; - - _listeners = new List(); - - _componentResetHandler = EcsComponentResetHandler.instance; - _componentCopyHandler = EcsComponentCopyHandler.instance; } #endregion @@ -171,8 +166,8 @@ namespace DCFApixels.DragonECS void IEcsPool.AddRaw(int entityID, object dataRaw) => Add(entityID) = (T)dataRaw; object IEcsPool.GetRaw(int entityID) => Read(entityID); void IEcsPool.SetRaw(int entityID, object dataRaw) => Get(entityID) = (T)dataRaw; - ref readonly T IEcsStructsPool.Read(int entityID) => ref Read(entityID); - ref T IEcsStructsPool.Get(int entityID) => ref Get(entityID); + ref readonly T IEcsStructPool.Read(int entityID) => ref Read(entityID); + ref T IEcsStructPool.Get(int entityID) => ref Get(entityID); #endregion #region Listeners diff --git a/src/Pools/EcsPoolBase.cs b/src/Pools/EcsPoolBase.cs index bb4d4cd..f07681f 100644 --- a/src/Pools/EcsPoolBase.cs +++ b/src/Pools/EcsPoolBase.cs @@ -30,12 +30,17 @@ namespace DCFApixels.DragonECS void RemoveListener(IEcsPoolEventListener listener); #endregion } - public interface IEcsStructsPool + public interface IEcsStructPool : IEcsPool { ref T Add(int entityID); ref readonly T Read(int entityID); ref T Get(int entityID); } + public interface IEcsClassPool : IEcsPool + { + T Add(int entityID); + T Get(int entityID); + } /// Only used to implement a custom pool. In other contexts use IEcsPool or IEcsPool. public interface IEcsPoolImplementation : IEcsPool { diff --git a/src/Pools/EcsTagPool.cs b/src/Pools/EcsTagPool.cs index 01c958f..a435a19 100644 --- a/src/Pools/EcsTagPool.cs +++ b/src/Pools/EcsTagPool.cs @@ -6,23 +6,23 @@ using static DCFApixels.DragonECS.EcsPoolThrowHalper; namespace DCFApixels.DragonECS { - public sealed class EcsTagPool : IEcsPoolImplementation, IEcsStructsPool, IEnumerable //IEnumerable - IntelliSense hack + public sealed class EcsTagPool : IEcsPoolImplementation, IEcsStructPool, IEnumerable //IEnumerable - IntelliSense hack where T : struct, IEcsTagComponent { private EcsWorld _source; - private int _id; + private int _componentID; private bool[] _mapping;// index = entityID / value = itemIndex;/ value = 0 = no entityID private int _count; - private List _listeners; + private List _listeners = new List(); private T _fakeComponent; #region Properites public int Count => _count; int IEcsPool.Capacity => -1; - public int ComponentID => _id; + public int ComponentID => _componentID; public Type ComponentType => typeof(T); public EcsWorld World => _source; #endregion @@ -31,12 +31,10 @@ namespace DCFApixels.DragonECS void IEcsPoolImplementation.OnInit(EcsWorld world, int componentID) { _source = world; - _id = componentID; + _componentID = componentID; _mapping = new bool[world.Capacity]; _count = 0; - - _listeners = new List(); } #endregion @@ -131,19 +129,19 @@ namespace DCFApixels.DragonECS #endregion #region Other - ref T IEcsStructsPool.Add(int entityID) + ref T IEcsStructPool.Add(int entityID) { Add(entityID); return ref _fakeComponent; } - ref readonly T IEcsStructsPool.Read(int entityID) + ref readonly T IEcsStructPool.Read(int entityID) { #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS if (!Has(entityID)) ThrowNotHaveComponent(entityID); #endif return ref _fakeComponent; } - ref T IEcsStructsPool.Get(int entityID) + ref T IEcsStructPool.Get(int entityID) { #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS if (!Has(entityID)) ThrowNotHaveComponent(entityID); diff --git a/src/entlong.cs b/src/entlong.cs index 904db21..2a4430a 100644 --- a/src/entlong.cs +++ b/src/entlong.cs @@ -33,7 +33,7 @@ namespace DCFApixels.DragonECS public bool IsNull { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => this == NULL; + get => full == 0l; } public int ID {