From b26a255a907fa53ae0a3e0152a4ff71ed9edff9e Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Thu, 20 Apr 2023 18:23:23 +0800 Subject: [PATCH] fix changes --- src/EcsComponentMask.cs | 16 ++ src/EcsGroup.cs | 16 +- src/EcsMask.cs | 296 ++++++++++++--------------- src/EcsPool.cs | 95 ++++----- src/EcsQuery.cs | 14 +- src/EcsQueryMember.cs | 42 ++-- src/EcsWorld.cs | 105 +++++----- src/Interfaces/IEcsComponentReset.cs | 37 ++-- src/Interfaces/IEcsTable.cs | 2 +- src/TestPool.cs | 103 ++++++++++ 10 files changed, 415 insertions(+), 311 deletions(-) create mode 100644 src/EcsComponentMask.cs diff --git a/src/EcsComponentMask.cs b/src/EcsComponentMask.cs new file mode 100644 index 0000000..91544a0 --- /dev/null +++ b/src/EcsComponentMask.cs @@ -0,0 +1,16 @@ +using System; + +namespace DCFApixels.DragonECS +{ + public class EcsComponentMask + { + internal Type WorldArchetypeType; + internal int[] Inc; + internal int[] Exc; + public override string ToString() + { + return $"Inc({string.Join(", ", Inc)}) Exc({string.Join(", ", Exc)})"; + } + } + +} diff --git a/src/EcsGroup.cs b/src/EcsGroup.cs index 5a303e1..131db48 100644 --- a/src/EcsGroup.cs +++ b/src/EcsGroup.cs @@ -264,7 +264,7 @@ namespace DCFApixels.DragonECS public void CopyFrom(EcsGroup group) { #if (DEBUG && !DISABLE_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS - if (group.World != _source) throw new ArgumentException("groupFilter.World != World"); + if (group.World != _source) throw new ArgumentException("groupFilter.WorldIndex != WorldIndex"); #endif if(_count > 0) Clear(); @@ -287,7 +287,7 @@ namespace DCFApixels.DragonECS public void UnionWith(EcsGroup group) { #if (DEBUG && !DISABLE_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS - if (_source != group.World) throw new ArgumentException("World != groupFilter.World"); + if (_source != group.World) throw new ArgumentException("WorldIndex != groupFilter.WorldIndex"); #endif foreach (var item in group) if (!Contains(item.id)) @@ -301,7 +301,7 @@ namespace DCFApixels.DragonECS public void ExceptWith(EcsGroup group) { #if (DEBUG && !DISABLE_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS - if (_source != group.World) throw new ArgumentException("World != groupFilter.World"); + if (_source != group.World) throw new ArgumentException("WorldIndex != groupFilter.WorldIndex"); #endif foreach (var item in this) if (group.Contains(item.id)) @@ -315,7 +315,7 @@ namespace DCFApixels.DragonECS public void AndWith(EcsGroup group) { #if (DEBUG && !DISABLE_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS - if (World != group.World) throw new ArgumentException("World != groupFilter.World"); + if (World != group.World) throw new ArgumentException("WorldIndex != groupFilter.WorldIndex"); #endif foreach (var item in this) if (!group.Contains(item.id)) @@ -329,7 +329,7 @@ namespace DCFApixels.DragonECS public void XorWith(EcsGroup group) { #if (DEBUG && !DISABLE_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS - if (_source != group.World) throw new ArgumentException("World != groupFilter.World"); + if (_source != group.World) throw new ArgumentException("WorldIndex != groupFilter.WorldIndex"); #endif foreach (var item in group) if (Contains(item.id)) @@ -345,7 +345,7 @@ namespace DCFApixels.DragonECS public static EcsGroup Except(EcsGroup a, EcsGroup b) { #if (DEBUG && !DISABLE_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS - if (a._source != b._source) throw new ArgumentException("a.World != b.World"); + if (a._source != b._source) throw new ArgumentException("a.WorldIndex != b.WorldIndex"); #endif EcsGroup result = a._source.GetGroupFromPool(); foreach (var item in a) @@ -359,7 +359,7 @@ namespace DCFApixels.DragonECS public static EcsGroup And(EcsGroup a, EcsGroup b) { #if (DEBUG && !DISABLE_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS - if (a._source != b._source) throw new ArgumentException("a.World != b.World"); + if (a._source != b._source) throw new ArgumentException("a.WorldIndex != b.WorldIndex"); #endif EcsGroup result = a._source.GetGroupFromPool(); foreach (var item in a) @@ -373,7 +373,7 @@ namespace DCFApixels.DragonECS public static EcsGroup Union(EcsGroup a, EcsGroup b) { #if (DEBUG && !DISABLE_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS - if (a._source != b._source) throw new ArgumentException("a.World != b.World"); + if (a._source != b._source) throw new ArgumentException("a.WorldIndex != b.WorldIndex"); #endif EcsGroup result = a._source.GetGroupFromPool(); foreach (var item in a) diff --git a/src/EcsMask.cs b/src/EcsMask.cs index 0d6a637..53ace7c 100644 --- a/src/EcsMask.cs +++ b/src/EcsMask.cs @@ -23,7 +23,7 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), }; } } @@ -33,8 +33,8 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), }; } } @@ -44,9 +44,9 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), }; } } @@ -56,10 +56,10 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), }; } } @@ -69,11 +69,11 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), }; } } @@ -83,12 +83,12 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), }; } } @@ -98,13 +98,13 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), }; } } @@ -114,14 +114,14 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), }; } } @@ -131,15 +131,15 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), }; } } @@ -149,16 +149,16 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), }; } } @@ -168,17 +168,17 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), }; } } @@ -188,18 +188,18 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), }; } } @@ -217,7 +217,7 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), }; } } @@ -227,8 +227,8 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), }; } } @@ -238,9 +238,9 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), }; } } @@ -250,10 +250,10 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), }; } } @@ -263,11 +263,11 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), }; } } @@ -277,47 +277,23 @@ namespace DCFApixels.DragonECS { return new int[] { - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), - ComponentIndexer.GetComponentId(ComponentIndexer.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), + WorldMetaStorage.GetComponentId(WorldMetaStorage.GetWorldId()), }; } } #endregion #region EcsMask - public class EcsComponentMask - { - internal Type WorldArchetypeType; - internal int[] Inc; - internal int[] Exc; - public override string ToString() - { - return $"Inc({string.Join(", ", Inc)}) Exc({string.Join(", ", Exc)})"; - } - } public sealed class EcsMask : EcsComponentMask { - // internal readonly Type WorldArchetypeType; internal readonly int UniqueID; - //internal readonly int[] Inc; - //internal readonly int[] Exc; - - //internal int IncCount - //{ - // [MethodImpl(MethodImplOptions.AggressiveInlining)] - // get => Inc.Length; - //} - //internal int ExcCount - //{ - // [MethodImpl(MethodImplOptions.AggressiveInlining)] - // get => Exc.Length; - //} internal EcsMask(Type worldArchetypeType, int uniqueID, int[] inc, int[] exc) { WorldArchetypeType = worldArchetypeType; @@ -357,43 +333,43 @@ namespace DCFApixels.DragonECS { static Activator() { - // var inc_ = new TInc().GetComponentsIDs(); - // var exc_ = new TExc().GetComponentsIDs(); - // Array.Sort(inc_); - // Array.Sort(exc_); - // - // Type thisType = typeof(Activator); - // - // Type sortedIncType = typeof(TInc); - // if (sortedIncType.IsGenericType) - // { - // Type[] sortedInc = new Type[inc_.Length]; - // for (int i = 0; i < sortedInc.Length; i++) - // sortedInc[i] = EcsWorld.ComponentType.types[inc_[i]]; - // sortedIncType = sortedIncType.GetGenericTypeDefinition().MakeGenericType(sortedInc); - // } - // Type sortedExcType = typeof(TExc); - // if (sortedExcType.IsGenericType) - // { - // Type[] sortedExc = new Type[exc_.Length]; - // for (int i = 0; i < sortedExc.Length; i++) - // sortedExc[i] = EcsWorld.ComponentType.types[exc_[i]]; - // sortedExcType = sortedExcType.GetGenericTypeDefinition().MakeGenericType(sortedExc); - // } - // - // Type targetType = typeof(Activator<,>).MakeGenericType(typeof(TWorldArchetype), sortedIncType, sortedExcType); - // - // if (targetType != thisType) - // { - // instance = (EcsMask)targetType.GetField(nameof(instance), BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic).GetValue(null); - // return; - // } - // - // var uniqueID = _count++; - // if (_count >= _capacity) - // _capacity <<= 1; - // - // instance = new EcsMask(typeof(TWorldArchetype), uniqueID, inc_, exc_); + var inc_ = new TInc().GetComponentsIDs(); + var exc_ = new TExc().GetComponentsIDs(); + Array.Sort(inc_); + Array.Sort(exc_); + + Type thisType = typeof(Activator); + + Type sortedIncType = typeof(TInc); + if (sortedIncType.IsGenericType) + { + Type[] sortedInc = new Type[inc_.Length]; + for (int i = 0; i < sortedInc.Length; i++) + sortedInc[i] = EcsWorld.ComponentType.types[inc_[i]]; + sortedIncType = sortedIncType.GetGenericTypeDefinition().MakeGenericType(sortedInc); + } + Type sortedExcType = typeof(TExc); + if (sortedExcType.IsGenericType) + { + Type[] sortedExc = new Type[exc_.Length]; + for (int i = 0; i < sortedExc.Length; i++) + sortedExc[i] = EcsWorld.ComponentType.types[exc_[i]]; + sortedExcType = sortedExcType.GetGenericTypeDefinition().MakeGenericType(sortedExc); + } + + Type targetType = typeof(Activator<,>).MakeGenericType(typeof(TWorldArchetype), sortedIncType, sortedExcType); + + if (targetType != thisType) + { + instance = (EcsMask)targetType.GetField(nameof(instance), BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic).GetValue(null); + return; + } + + var uniqueID = _count++; + if (_count >= _capacity) + _capacity <<= 1; + + instance = new EcsMask(typeof(TWorldArchetype), uniqueID, inc_, exc_); } public readonly static EcsMask instance; diff --git a/src/EcsPool.cs b/src/EcsPool.cs index 1906252..4b2470c 100644 --- a/src/EcsPool.cs +++ b/src/EcsPool.cs @@ -8,7 +8,6 @@ namespace DCFApixels.DragonECS { #region Properties public Type ComponentType { get; } - public int ComponentID { get; } public IEcsWorld World { get; } public int Count { get; } public int Capacity { get; } @@ -19,6 +18,9 @@ namespace DCFApixels.DragonECS public void Write(int entityID); public bool Has(int entityID); public void Del(int entityID); + #endregion + + #region Internal internal void OnWorldResize(int newSize); #endregion } @@ -30,7 +32,7 @@ namespace DCFApixels.DragonECS } public struct NullComponent { } - public sealed class EcsNullPool : EcsPool, IEcsPool + public sealed class EcsNullPool : IEcsPool { public static EcsNullPool instance => new EcsNullPool(null); private IEcsWorld _source; @@ -47,25 +49,24 @@ namespace DCFApixels.DragonECS #region Methods public ref NullComponent Add(int entity) => ref fakeComponent; - public override bool Has(int index) => false; + public bool Has(int index) => false; public ref NullComponent Read(int entity) => ref fakeComponent; public ref NullComponent Write(int entity) => ref fakeComponent; public void Del(int index) { } void IEcsPool.Write(int entityID) { } void IEcsPool.Add(int entityID) { } void IEcsPool.OnWorldResize(int newSize) { } - internal override void OnWorldResize(int newSize) { } #endregion } - public abstract class EcsPool - { - public abstract bool Has(int entityID); - internal abstract void OnWorldResize(int newSize); - } - public sealed class EcsPool : EcsPool, IEcsPool + + public sealed class EcsPool : IEcsPool where T : struct { - private readonly int _componentID; + public static EcsPool Builder(IEcsWorld source) + { + return new EcsPool(source, 512, default); + } + private readonly IEcsWorld _source; private int[] _mapping;// index = entityID / value = itemIndex;/ value = 0 = no entityID @@ -75,21 +76,19 @@ namespace DCFApixels.DragonECS private int _recycledItemsCount; private IEcsComponentReset _componentResetHandler; - private PoolRunnres _poolRunnres; + private PoolRunners _poolRunners; #region Properites public int Count => _itemsCount; public int Capacity => _items.Length; public IEcsWorld World => _source; public Type ComponentType => typeof(T); - public int ComponentID => _componentID; #endregion #region Constructors - internal EcsPool(IEcsWorld source, int id, int capacity, PoolRunnres poolRunnres) + internal EcsPool(IEcsWorld source, int capacity, PoolRunners poolRunnres) { _source = source; - _componentID = id; _mapping = new int[source.Capacity]; _recycledItems = new int[128]; @@ -97,8 +96,8 @@ namespace DCFApixels.DragonECS _items = new T[capacity]; _itemsCount = 0; - _componentResetHandler = IEcsComponentReset.Handler; - _poolRunnres = poolRunnres; + _componentResetHandler = EcsComponentResetHandler.instance; + _poolRunners = poolRunnres; } #endregion @@ -110,45 +109,45 @@ namespace DCFApixels.DragonECS private ProfilerMarker _delMark = new ProfilerMarker("EcsPoo.Del"); public ref T Add(int entityID) { - // using (_addMark.Auto()) - // { - ref int itemIndex = ref _mapping[entityID]; - if (itemIndex <= 0) + // using (_addMark.Auto()) + // { + ref int itemIndex = ref _mapping[entityID]; + if (itemIndex <= 0) + { + if (_recycledItemsCount > 0) { - if (_recycledItemsCount > 0) - { - itemIndex = _recycledItems[--_recycledItemsCount]; - _itemsCount++; - } - else - { - itemIndex = ++_itemsCount; - if (itemIndex >= _items.Length) - Array.Resize(ref _items, _items.Length << 1); - } + itemIndex = _recycledItems[--_recycledItemsCount]; + _itemsCount++; + } + else + { + itemIndex = ++_itemsCount; + if (itemIndex >= _items.Length) + Array.Resize(ref _items, _items.Length << 1); + } - _mapping[entityID] = itemIndex; - _poolRunnres.add.OnComponentAdd(entityID); + _mapping[entityID] = itemIndex; + _poolRunners.add.OnComponentAdd(entityID); } - _poolRunnres.write.OnComponentWrite(entityID); - return ref _items[itemIndex]; + _poolRunners.write.OnComponentWrite(entityID); + return ref _items[itemIndex]; // } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public ref T Write(int entityID) { - // using (_writeMark.Auto()) - _poolRunnres.write.OnComponentWrite(entityID); - return ref _items[_mapping[entityID]]; + // using (_writeMark.Auto()) + _poolRunners.write.OnComponentWrite(entityID); + return ref _items[_mapping[entityID]]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public ref T Read(int entityID) { - // using (_readMark.Auto()) + // using (_readMark.Auto()) return ref _items[_mapping[entityID]]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public sealed override bool Has(int entityID) + public bool Has(int entityID) { // using (_hasMark.Auto()) return _mapping[entityID] > 0; @@ -164,8 +163,8 @@ namespace DCFApixels.DragonECS _recycledItems[_recycledItemsCount++] = itemIndex; itemIndex = 0; _itemsCount--; - _poolRunnres.del.OnComponentDel(entityID); - // } + _poolRunners.del.OnComponentDel(entityID); + // } } #endregion @@ -180,22 +179,12 @@ namespace DCFApixels.DragonECS } #endregion - #region Object - public override bool Equals(object obj) => base.Equals(obj); - public override int GetHashCode() => _source.GetHashCode() ^ ~ComponentID; - #endregion - #region Internal [MethodImpl(MethodImplOptions.AggressiveInlining)] void IEcsPool.OnWorldResize(int newSize) { Array.Resize(ref _mapping, newSize); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal sealed override void OnWorldResize(int newSize) - { - Array.Resize(ref _mapping, newSize); - } #endregion } } diff --git a/src/EcsQuery.cs b/src/EcsQuery.cs index 67472a6..30ae684 100644 --- a/src/EcsQuery.cs +++ b/src/EcsQuery.cs @@ -77,12 +77,20 @@ namespace DCFApixels.DragonECS } #endregion } + public abstract class EcsHierarchyQuery : EcsQueryBase + { + private ProfilerMarker _getEnumerator = new ProfilerMarker("EcsHierarchyQuery.Execute"); - public abstract class EcsJoinQuery : EcsQueryBase + protected override void OnBuildAfter() { } + public override void Execute() + { + } + } + public abstract class EcsGraphQuery : EcsQueryBase { private EcsPool attachPool; - private ProfilerMarker _getEnumerator = new ProfilerMarker("EcsQuery.Where"); + private ProfilerMarker _getEnumerator = new ProfilerMarker("EcsGraphQuery.Execute"); protected sealed override void OnBuildAfter() { attachPool = World.GetPool(); @@ -102,7 +110,7 @@ namespace DCFApixels.DragonECS public abstract class EcsQuery : EcsQueryBase { - private ProfilerMarker _getEnumerator = new ProfilerMarker("EcsQuery.Where"); + private ProfilerMarker _getEnumerator = new ProfilerMarker("EcsQuery.Execute"); protected sealed override void OnBuildAfter() { } public sealed override void Execute() { diff --git a/src/EcsQueryMember.cs b/src/EcsQueryMember.cs index e466903..f5293c5 100644 --- a/src/EcsQueryMember.cs +++ b/src/EcsQueryMember.cs @@ -84,54 +84,54 @@ namespace DCFApixels.DragonECS // #region select_readonly // [StructLayout(LayoutKind.Sequential, Pack = 8, Size = 8)] - // public readonly struct inc_readonly_ : IEcsQueryReadonlyField - // where TComponent : struct + // public readonly struct inc_readonly_ : IEcsQueryReadonlyField + // where T : struct // { - // internal readonly EcsPool pool; + // internal readonly EcsPool pool; // [MethodImpl(MethodImplOptions.AggressiveInlining)] - // internal inc_readonly_(EcsPool pool) => this.pool = pool; + // internal inc_readonly_(EcsPool pool) => this.pool = pool; // [MethodImpl(MethodImplOptions.AggressiveInlining)] - // public ref TComponent Read(ent entityID) => ref pool.Read(entityID.uniqueID); + // public ref T Read(ent entityID) => ref pool.Read(entityID.uniqueID); // [MethodImpl(MethodImplOptions.AggressiveInlining)] // public bool Has(ent entityID) => pool.Has(entityID.uniqueID); // [MethodImpl(MethodImplOptions.AggressiveInlining)] // - // public static implicit operator inc_readonly_(EcsQueryBuilderBase buider) => buider.Include(); - // public static implicit operator inc_readonly_(inc_ o) => new inc_readonly_(o.pool); + // public static implicit operator inc_readonly_(EcsQueryBuilderBase buider) => buider.Include(); + // public static implicit operator inc_readonly_(inc_ o) => new inc_readonly_(o.pool); // } // // [StructLayout(LayoutKind.Sequential, Pack = 8, Size = 8)] - // public readonly struct exc_readonly_ : IEcsQueryReadonlyField - // where TComponent : struct + // public readonly struct exc_readonly_ : IEcsQueryReadonlyField + // where T : struct // { - // internal readonly EcsPool pool; + // internal readonly EcsPool pool; // [MethodImpl(MethodImplOptions.AggressiveInlining)] - // internal exc_readonly_(EcsPool pool) => this.pool = pool; + // internal exc_readonly_(EcsPool pool) => this.pool = pool; // [MethodImpl(MethodImplOptions.AggressiveInlining)] - // public ref TComponent Read(ent entityID) => ref pool.Read(entityID.uniqueID); + // public ref T Read(ent entityID) => ref pool.Read(entityID.uniqueID); // [MethodImpl(MethodImplOptions.AggressiveInlining)] // public bool Has(ent entityID) => pool.Has(entityID.uniqueID); // [MethodImpl(MethodImplOptions.AggressiveInlining)] // - // public static implicit operator exc_readonly_(EcsQueryBuilderBase buider) => buider.Exclude(); - // public static implicit operator exc_readonly_(exc_ o) => new exc_readonly_(o.pool); + // public static implicit operator exc_readonly_(EcsQueryBuilderBase buider) => buider.Exclude(); + // public static implicit operator exc_readonly_(exc_ o) => new exc_readonly_(o.pool); // } // // [StructLayout(LayoutKind.Sequential, Pack = 8, Size = 8)] - // public readonly struct opt_readonly_ : IEcsQueryReadonlyField - // where TComponent : struct + // public readonly struct opt_readonly_ : IEcsQueryReadonlyField + // where T : struct // { - // internal readonly EcsPool pool; + // internal readonly EcsPool pool; // [MethodImpl(MethodImplOptions.AggressiveInlining)] - // internal opt_readonly_(EcsPool pool) => this.pool = pool; + // internal opt_readonly_(EcsPool pool) => this.pool = pool; // [MethodImpl(MethodImplOptions.AggressiveInlining)] - // public ref TComponent Read(ent entityID) => ref pool.Read(entityID.uniqueID); + // public ref T Read(ent entityID) => ref pool.Read(entityID.uniqueID); // [MethodImpl(MethodImplOptions.AggressiveInlining)] // public bool Has(ent entityID) => pool.Has(entityID.uniqueID); // [MethodImpl(MethodImplOptions.AggressiveInlining)] // - // public static implicit operator opt_readonly_(EcsQueryBuilderBase buider) => buider.Optional(); - // public static implicit operator opt_readonly_(opt_ o) => new opt_readonly_(o.pool); + // public static implicit operator opt_readonly_(EcsQueryBuilderBase buider) => buider.Optional(); + // public static implicit operator opt_readonly_(opt_ o) => new opt_readonly_(o.pool); // } // #endregion // diff --git a/src/EcsWorld.cs b/src/EcsWorld.cs index 2129adb..ab93668 100644 --- a/src/EcsWorld.cs +++ b/src/EcsWorld.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using static DCFApixels.DragonECS.WorldMetaStorage; namespace DCFApixels.DragonECS { @@ -43,7 +44,7 @@ namespace DCFApixels.DragonECS where TWorldArchetype : EcsWorld { private const int DEL_ENT_BUFFER_SIZE_OFFSET = 2; - private readonly int _worldArchetypeID = ComponentIndexer.GetWorldId(); + private readonly int _worldArchetypeID = WorldMetaStorage.GetWorldId(); private IntDispenser _entityDispenser; private int _entitiesCount; @@ -52,10 +53,12 @@ namespace DCFApixels.DragonECS //private short[] _componentCounts; //TODO private EcsGroup _allEntites; - private int[] _delEntBuffer; //буфер удаления нужен для того чтобы запускать некоторые процесыы связанные с удалением сущьности не по одному при каждом удалении, а пачкой + //буфер удаления откладывает освобождение андишников сущьностей. + //Нужен для того чтобы запускать некоторые процесыы связанные с удалением сущьности не по одному при каждом удалении, а пачкой + private int[] _delEntBuffer; private int _delEntBufferCount; - private EcsPool[] _pools; + private IEcsPool[] _pools; private EcsNullPool _nullPool; private EcsQueryBase[] _queries; @@ -65,13 +68,13 @@ namespace DCFApixels.DragonECS private List> _groups; private Stack _groupsPool = new Stack(64); - private PoolRunnres _poolRunnres; + private PoolRunners _poolRunners; private IEcsEntityCreate _entityCreate; private IEcsEntityDestroy _entityDestry; #region GetterMethods - public ReadOnlySpan GetAllPools() => new ReadOnlySpan(_pools); - public int GetComponentID() => ComponentIndexer.GetComponentId(_worldArchetypeID);////ComponentType.uniqueID; + public ReadOnlySpan GetAllPools() => new ReadOnlySpan(_pools); + public int GetComponentID() => WorldMetaStorage.GetComponentId(_worldArchetypeID);////ComponentType.uniqueID; #endregion @@ -91,7 +94,7 @@ namespace DCFApixels.DragonECS if (!_pipeline.IsInit) pipline.Init(); _entityDispenser = new IntDispenser(0); _nullPool = EcsNullPool.instance; - _pools = new EcsPool[512]; + _pools = new IEcsPool[512]; ArrayUtility.Fill(_pools, _nullPool); _gens = new short[512]; @@ -102,9 +105,9 @@ namespace DCFApixels.DragonECS _groups = new List>(); _allEntites = GetGroupFromPool(); - _queries = new EcsQuery[QueryType.capacity]; + _queries = new EcsQuery[128]; - _poolRunnres = new PoolRunnres(_pipeline); + _poolRunners = new PoolRunners(_pipeline); _entityCreate = _pipeline.GetRunner(); _entityDestry = _pipeline.GetRunner(); _pipeline.GetRunner>().Inject((TWorldArchetype)this); @@ -114,9 +117,10 @@ namespace DCFApixels.DragonECS #endregion #region GetPool - public EcsPool GetPool() where T : struct + public EcsPool GetPool() + where TComponent : struct { - int uniqueID = ComponentIndexer.GetComponentId(_worldArchetypeID); + int uniqueID = WorldMetaStorage.GetComponentId(_worldArchetypeID); if (uniqueID >= _pools.Length) { @@ -126,10 +130,9 @@ namespace DCFApixels.DragonECS } if (_pools[uniqueID] == _nullPool) - { - _pools[uniqueID] = new EcsPool(this, uniqueID, 512, _poolRunnres); - } - return (EcsPool)_pools[uniqueID]; + _pools[uniqueID] = new EcsPool(this, 512, _poolRunners); + + return (EcsPool)_pools[uniqueID]; } #endregion @@ -142,9 +145,9 @@ namespace DCFApixels.DragonECS } public TQuery Select() where TQuery : EcsQueryBase { - int uniqueID = QueryType.uniqueID; - if (_queries.Length < QueryType.capacity) - Array.Resize(ref _queries, QueryType.capacity); + int uniqueID = WorldMetaStorage.GetQueryId(_worldArchetypeID); + if (uniqueID >= _queries.Length) + Array.Resize(ref _queries, _queries.Length << 1); if (_queries[uniqueID] == null) _queries[uniqueID] = EcsQueryBase.Builder.Build(this); return (TQuery)_queries[uniqueID]; @@ -274,53 +277,37 @@ namespace DCFApixels.DragonECS { #if (DEBUG && !DISABLE_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS if (group.World != this) - throw new ArgumentException("groupFilter.World != this"); + throw new ArgumentException("groupFilter.WorldIndex != this"); #endif group.Clear(); _groupsPool.Push(group); } #endregion - - #region Utils - internal static class QueryType - { - public static int increment = 0; - public static int capacity = 128; - } - internal static class QueryType - { - public static int uniqueID; - static QueryType() - { - uniqueID = QueryType.increment++; - if (QueryType.increment > QueryType.capacity) - QueryType.capacity <<= 1; - } - } - #endregion } #region Utils [StructLayout(LayoutKind.Sequential, Pack = 8, Size = 24)] - internal readonly struct PoolRunnres + internal readonly struct PoolRunners { public readonly IEcsComponentAdd add; public readonly IEcsComponentWrite write; public readonly IEcsComponentDel del; - public PoolRunnres(EcsPipeline pipeline) + public PoolRunners(EcsPipeline pipeline) { add = pipeline.GetRunner(); write = pipeline.GetRunner(); del = pipeline.GetRunner(); } } - public static class ComponentIndexer + public static class WorldMetaStorage { private static List resizer = new List(); private static int tokenCount = 0; private static int[] componentCounts = new int[0]; - private static class World + private static int[] queryCounts = new int[0]; + + private static class WorldIndex { public static int id = GetToken(); } @@ -328,23 +315,30 @@ namespace DCFApixels.DragonECS { tokenCount++; Array.Resize(ref componentCounts, tokenCount); + Array.Resize(ref queryCounts, tokenCount); foreach (var item in resizer) item.Resize(tokenCount); return tokenCount - 1; } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int GetWorldId() => World.id; + public static int GetWorldId() => WorldIndex.id; [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int GetComponentId(int worldID) => Component.Get(worldID); + public static int GetComponentId(int worldID) => Component.Get(worldID); + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int GetQueryId(int worldID) => Query.Get(worldID); private abstract class Resizer { public abstract void Resize(int size); } private sealed class Resizer : Resizer { - public override void Resize(int size) => Array.Resize(ref Component.ids, size); + public override void Resize(int size) + { + Array.Resize(ref Component.ids, size); + Array.Resize(ref Query.ids, size); + } } - private static class Component + private static class Component { public static int[] ids; static Component() @@ -352,7 +346,7 @@ namespace DCFApixels.DragonECS ids = new int[tokenCount]; for (int i = 0; i < ids.Length; i++) ids[i] = -1; - resizer.Add(new Resizer()); + resizer.Add(new Resizer()); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int Get(int token) @@ -363,6 +357,25 @@ namespace DCFApixels.DragonECS return id; } } + private static class Query + { + public static int[] ids; + static Query() + { + ids = new int[tokenCount]; + for (int i = 0; i < ids.Length; i++) + ids[i] = -1; + resizer.Add(new Resizer()); + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int Get(int token) + { + ref int id = ref ids[token]; + if (id < 0) + id = queryCounts[token]++; + return id; + } + } } #endregion } diff --git a/src/Interfaces/IEcsComponentReset.cs b/src/Interfaces/IEcsComponentReset.cs index 161ad83..8abb356 100644 --- a/src/Interfaces/IEcsComponentReset.cs +++ b/src/Interfaces/IEcsComponentReset.cs @@ -1,34 +1,33 @@ -using System.Runtime.CompilerServices; -using System; +using System; using System.Linq; +using System.Runtime.CompilerServices; namespace DCFApixels.DragonECS { public interface IEcsComponentReset { public void Reset(ref T component); - - - private static IEcsComponentReset _handler; - public static IEcsComponentReset Handler + } + public static class EcsComponentResetHandler + { + public static readonly IEcsComponentReset instance; + public static readonly bool isHasHandler; + static EcsComponentResetHandler() { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get + Type targetType = typeof(T); + if (targetType.GetInterfaces().Contains(typeof(IEcsComponentReset<>).MakeGenericType(targetType))) { - if(_handler == null) - { - Type targetType = typeof(T); - if (targetType.GetInterfaces().Contains(typeof(IEcsComponentReset<>).MakeGenericType(targetType))) - _handler = (IEcsComponentReset)Activator.CreateInstance(typeof(ComponentResetHandler<>).MakeGenericType(targetType)); - else - _handler = new ComponentResetDummy(); - } - return _handler; + instance = (IEcsComponentReset)Activator.CreateInstance(typeof(ComponentResetHandler<>).MakeGenericType(targetType)); + isHasHandler = true; + } + else + { + instance = new ComponentResetDummyHandler(); + isHasHandler = false; } } } - - internal sealed class ComponentResetDummy : IEcsComponentReset + internal sealed class ComponentResetDummyHandler : IEcsComponentReset { [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Reset(ref T component) => component = default; diff --git a/src/Interfaces/IEcsTable.cs b/src/Interfaces/IEcsTable.cs index d0c5e5f..dc6517e 100644 --- a/src/Interfaces/IEcsTable.cs +++ b/src/Interfaces/IEcsTable.cs @@ -16,7 +16,7 @@ namespace DCFApixels.DragonECS #region Methods public int GetComponentID(); public EcsPool GetPool() where T : struct; - public ReadOnlySpan GetAllPools(); + public ReadOnlySpan GetAllPools(); public TQuery Where(out TQuery query) where TQuery : EcsQueryBase; public TQuery Select() where TQuery : EcsQueryBase; diff --git a/src/TestPool.cs b/src/TestPool.cs index a34ae1a..a0dd72a 100644 --- a/src/TestPool.cs +++ b/src/TestPool.cs @@ -12,6 +12,11 @@ namespace DCFApixels.DragonECS.Test { return new PoolToken(1); } + + public IEcsPool GetPoolX() + { + return null; + } } public readonly struct PoolToken @@ -33,4 +38,102 @@ namespace DCFApixels.DragonECS.Test _token = world.RegisterPool(); } } + + public interface IPool { } + public class Pool1 : IEcsPool + where TComponent : struct + { + public Type ComponentType => throw new NotImplementedException(); + + public IEcsWorld World => throw new NotImplementedException(); + + public int Count => throw new NotImplementedException(); + + public int Capacity => throw new NotImplementedException(); + + public ref TComponent Add(int entityID) + { + throw new NotImplementedException(); + } + + public void Del(int entityID) + { + throw new NotImplementedException(); + } + + public bool Has(int entityID) + { + throw new NotImplementedException(); + } + + public ref TComponent Read(int entityID) + { + throw new NotImplementedException(); + } + + public ref TComponent Write(int entityID) + { + throw new NotImplementedException(); + } + + void IEcsPool.Add(int entityID) + { + throw new NotImplementedException(); + } + + void IEcsPool.OnWorldResize(int newSize) + { + throw new NotImplementedException(); + } + + void IEcsPool.Write(int entityID) + { + throw new NotImplementedException(); + } + } + + public interface IComponentPool + where TPool : IEcsPool + where TComponent : struct + { } + + public interface IComponent1 : IComponentPool, TComponent> + where TComponent : struct + { } + public interface IComponent2 : IComponentPool, TComponent> + where TComponent : struct + { } + + public struct ComponentX1 : IComponent1 { } + public struct ComponentX2 : IComponent2 { } + + public static class Pool1Ext + { + public static Pool1 GetPool(this TestWorld self) + where TComponent : struct, IComponent1 + { + return (Pool1)self.GetPoolX(); + } + } + + public static class Pool2Ext + { + public static EcsPool GetPool(this TestWorld self) + where TComponent : struct, IComponent2 + { + return (EcsPool)self.GetPoolX(); + } + } + + public class Foo + { + private TestWorld world; + public void Do() + { + var poola = world.GetPool(); + } + } + + } +