From 1ed1782e4a6134a5c495703b4431d7f3e8eb72b8 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Sat, 8 Apr 2023 00:47:35 +0800 Subject: [PATCH] refactoring --- src/EcsGroup.cs | 13 +-- src/EcsQuery.cs | 21 +---- src/EcsRelationTable.cs | 126 ++-------------------------- src/EcsWorld.cs | 88 +++++++++---------- src/Interfaces/IEcsReadonlyTable.cs | 31 ++++--- src/Utils/ArrayUtility.cs | 21 +++++ 6 files changed, 100 insertions(+), 200 deletions(-) create mode 100644 src/Utils/ArrayUtility.cs diff --git a/src/EcsGroup.cs b/src/EcsGroup.cs index c540f4b..6387d69 100644 --- a/src/EcsGroup.cs +++ b/src/EcsGroup.cs @@ -269,22 +269,13 @@ namespace DCFApixels.DragonECS get => _source.World.GetEntity(_dense[_index]); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool MoveNext() - { - // <= потму что отсчет начинается с индекса 1 - return ++_index <= _count && _count < _dense.Length; //_count < _dense.Length дает среде понять что проверки на выход за границы не нужны - } - + public bool MoveNext() => ++_index <= _count && _count<_dense.Length; // <= потму что отсчет начинается с индекса 1 //_count < _dense.Length дает среде понять что проверки на выход за границы не нужны [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Dispose() - { - _source.Unlock(); - } + public void Dispose() => _source.Unlock(); } #endregion } - public static class EcsGroupExtensions { [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/EcsQuery.cs b/src/EcsQuery.cs index 07761fd..8c1e1d1 100644 --- a/src/EcsQuery.cs +++ b/src/EcsQuery.cs @@ -5,27 +5,14 @@ using System.Runtime.CompilerServices; namespace DCFApixels.DragonECS { - public interface IEcsQuery - { - internal void AddEntity(int entityID); - internal void RemoveEntity(int entityID); - public EcsQueryMask Mask { get; } - } - public abstract class EcsQueryBase : IEcsQuery + public abstract class EcsQueryBase { internal EcsGroup group; internal EcsQueryMask mask; - - public EcsQueryMask Mask => mask; - - public void AddEntity(int entityID) => group.Add(entityID); - public void RemoveEntity(int entityID) => group.Remove(entityID); - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - void IEcsQuery.AddEntity(int entityID) => group.Add(entityID); + internal void AddEntity(int entityID) => group.Add(entityID); [MethodImpl(MethodImplOptions.AggressiveInlining)] - void IEcsQuery.RemoveEntity(int entityID) => group.Remove(entityID); + internal void RemoveEntity(int entityID) => group.Remove(entityID); } public abstract class EcsQuery : EcsQueryBase @@ -51,7 +38,7 @@ namespace DCFApixels.DragonECS private List _inc; private List _exc; - internal static TQuery Build(IEcsWorld world) where TQuery : IEcsQuery + internal static TQuery Build(IEcsWorld world) where TQuery : EcsQueryBase { Builder builder = new Builder(world); diff --git a/src/EcsRelationTable.cs b/src/EcsRelationTable.cs index 7d34788..61c0c3a 100644 --- a/src/EcsRelationTable.cs +++ b/src/EcsRelationTable.cs @@ -5,13 +5,13 @@ using System.Collections.Generic; namespace DCFApixels.DragonECS { - /* public interface IEcsRealationTable : IEcsReadonlyTable + public interface IEcsRealationTable { - public EcsFilter Relations() where TComponent : struct; - rr } - public sealed class EcsRelationTable : IEcsRealationTable - where TWorldArchetype : EcsRelationTableArchetypeBase + internal class EcsRelationWorld : EcsWorld> + where TRelationTableArhetype : EcsRelationTableArchetypeBase { } + public sealed class EcsRelationTable : IEcsRealationTable + where TTableArhetype : EcsRelationTableArchetypeBase { public readonly IEcsWorld leftWorld; public readonly IEcsWorld rightWorld; @@ -20,118 +20,6 @@ namespace DCFApixels.DragonECS private int[] _leftMapping; private int[] _rgihtMapping; - private int _relationsCount; - - private IEcsPool[] _pools; - private EcsNullPool _nullPool; - - #region Properties - public Type ArchetypeType => typeof(TWorldArchetype); - public int EntitesCount => _relationsCount; - public int EntitesCapacity => _relations.Length; - #endregion - - #region Constructors - internal EcsRelationTable(IEcsWorld leftWorld, IEcsWorld rightWorld) - { - this.leftWorld = leftWorld; - this.rightWorld = rightWorld; - - _relations = new int[512]; - _leftMapping = new int[512]; - _rgihtMapping = new int[512]; - - _relationsCount = 0; - } - #endregion - - #region RealtionControls - public void AddRelation(int leftEnttiyID, int rightEntityID) - { - - } - public void RemoveRelationLeft(int entityID) - { - - } - public void RemoveRelationRight(int entityID) - { - - } - #endregion - - public ReadOnlySpan GetAllPools() - { - throw new NotImplementedException(); - } - - public int GetComponentID() - { - throw new NotImplementedException(); - } - - public EcsPool GetPool() where T : struct - { - throw new NotImplementedException(); - } - - public EcsPool UncheckedGetPool() where T : struct - { - throw new NotImplementedException(); - } - - public EcsFilter Query() where TComponent : struct - { - throw new NotImplementedException(); - } - - public EcsFilter Filter() where TInc : struct, IInc - { - throw new NotImplementedException(); - } - - public EcsFilter Filter() - where TInc : struct, IInc - where TExc : struct, IExc - { - throw new NotImplementedException(); - } - - public bool IsMaskCompatible(int entity) where TInc : struct, IInc - { - throw new NotImplementedException(); - } - - public bool IsMaskCompatible(int entity) - where TInc : struct, IInc - where TExc : struct, IExc - { - throw new NotImplementedException(); - } - - public bool IsMaskCompatible(EcsMask mask, int entity) - { - throw new NotImplementedException(); - } - - public bool IsMaskCompatibleWithout(EcsMask mask, int entity, int otherPoolID) - { - throw new NotImplementedException(); - } - - void IEcsReadonlyTable.OnEntityComponentAdded(int entityID, int changedPoolID) - { - throw new NotImplementedException(); - } - - void IEcsReadonlyTable.OnEntityComponentRemoved(int entityID, int changedPoolID) - { - throw new NotImplementedException(); - } - - void IEcsReadonlyTable.RegisterGroup(EcsGroup group) - { - throw new NotImplementedException(); - } - }*/ + private EcsRelationWorld _relationWorld; + } } diff --git a/src/EcsWorld.cs b/src/EcsWorld.cs index 407137d..115d097 100644 --- a/src/EcsWorld.cs +++ b/src/EcsWorld.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using System.Transactions; namespace DCFApixels.DragonECS { @@ -17,8 +18,6 @@ namespace DCFApixels.DragonECS #endregion #region Entities - public TQuery Query(out TQuery entities) where TQuery : IEcsQuery; - public ent NewEntity(); public void DelEntity(ent entity); public bool EntityIsAlive(int entityID, short gen); @@ -71,12 +70,33 @@ namespace DCFApixels.DragonECS private List[] _filtersByIncludedComponents; private List[] _filtersByExcludedComponents; - private IEcsQuery[] _queries; + private EcsQueryBase[] _queries; private EcsPipeline _pipeline; private List _groups; + public IEcsRealationTable[] _relationTables; + + #region RelationTables + public IEcsRealationTable GetRelationTalbe(TWorldArhetype targetWorld) + where TWorldArhetype : EcsWorld + { + int targetID = targetWorld.ID; + if (targetID <= 0) + throw new ArgumentException("targetWorld.ID <= 0"); + + if(_relationTables.Length <= targetID) + Array.Resize(ref _relationTables, targetID + 8); + + if (_relationTables[targetID] == null) + { + // _relationTables[targetID]= new EcsRelationTable + } + + throw new NotImplementedException(); + } + #endregion #region RunnersCache private PoolRunnres _poolRunnres; @@ -112,10 +132,10 @@ namespace DCFApixels.DragonECS _entityDispenser = new IntDispenser(0); _nullPool = EcsNullPool.instance; _pools = new IEcsPool[512]; - FillArray(_pools, _nullPool); + ArrayUtility.Fill(_pools, _nullPool); _gens = new short[512]; - _queries = new EcsQuery[EntityArhetype.capacity]; + _queries = new EcsQuery[QueryType.capacity]; _groups = new List(128); _denseEntities = new int[512]; @@ -141,7 +161,7 @@ namespace DCFApixels.DragonECS { int oldCapacity = _pools.Length; Array.Resize(ref _pools, ComponentType.Capacity); - FillArray(_pools, _nullPool, oldCapacity, oldCapacity - _pools.Length); + ArrayUtility.Fill(_pools, _nullPool, oldCapacity, oldCapacity - _pools.Length); Array.Resize(ref _filtersByIncludedComponents, ComponentType.Capacity); Array.Resize(ref _filtersByExcludedComponents, ComponentType.Capacity); @@ -156,18 +176,18 @@ namespace DCFApixels.DragonECS //public EcsPool UncheckedGetPool() where T : struct => (EcsPool)_pools[ComponentType.uniqueID]; #endregion - #region Entities - public TQuery Query(out TQuery entities) where TQuery : IEcsQuery + #region Query + public TQuery Query(out TQuery query) where TQuery : EcsQueryBase { - int uniqueID = EntityArhetype.uniqueID; - if (_queries.Length < EntityArhetype.capacity) - Array.Resize(ref _queries, EntityArhetype.capacity); + int uniqueID = QueryType.uniqueID; + if (_queries.Length < QueryType.capacity) + Array.Resize(ref _queries, QueryType.capacity); if (_queries[uniqueID] == null) { _queries[uniqueID] = EcsQuery.Builder.Build(this); - var mask = _queries[uniqueID].Mask; - var filter = (EcsQueryBase)_queries[uniqueID]; + var mask = _queries[uniqueID].mask; + var filter = _queries[uniqueID]; for (int i = 0; i < mask.Inc.Length; i++) { @@ -200,8 +220,8 @@ namespace DCFApixels.DragonECS filter.AddEntity(entity); } } - entities = (TQuery)_queries[uniqueID]; - return entities; + query = (TQuery)_queries[uniqueID]; + return query; } #endregion @@ -217,20 +237,20 @@ namespace DCFApixels.DragonECS return IsMaskCompatible(EcsMaskMap.GetMask(), entityID); } - public bool IsMaskCompatible(EcsComponentMask mask, int entity) + public bool IsMaskCompatible(EcsComponentMask mask, int entityID) { #if (DEBUG && !DISABLE_DRAGONECS_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS if (mask.WorldArchetypeType != typeof(TWorldArchetype)) - throw new EcsFrameworkException("mask.WorldArchetypeType != typeof(TWorldArchetype)"); + throw new EcsFrameworkException("mask.WorldArchetypeType != typeof(TTableArhetype)"); #endif for (int i = 0, iMax = mask.Inc.Length; i < iMax; i++) { - if (!_pools[mask.Inc[i]].Has(entity)) + if (!_pools[mask.Inc[i]].Has(entityID)) return false; } for (int i = 0, iMax = mask.Exc.Length; i < iMax; i++) { - if (_pools[mask.Exc[i]].Has(entity)) + if (_pools[mask.Exc[i]].Has(entityID)) return false; } return true; @@ -240,7 +260,7 @@ namespace DCFApixels.DragonECS { #if (DEBUG && !DISABLE_DRAGONECS_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS if (mask.WorldArchetypeType != typeof(TWorldArchetype)) - throw new EcsFrameworkException("mask.WorldArchetypeType != typeof(TWorldArchetype)"); + throw new EcsFrameworkException("mask.WorldArchetypeType != typeof(TTableArhetype)"); #endif for (int i = 0, iMax = mask.Inc.Length; i < iMax; i++) { @@ -392,19 +412,19 @@ namespace DCFApixels.DragonECS #endregion #region Utils - internal static class EntityArhetype + internal static class QueryType { public static int increment = 0; public static int capacity = 128; } - internal static class EntityArhetype + internal static class QueryType { public static int uniqueID; - static EntityArhetype() + static QueryType() { - uniqueID = EntityArhetype.increment++; - if (EntityArhetype.increment > EntityArhetype.capacity) - EntityArhetype.capacity <<= 1; + uniqueID = QueryType.increment++; + if (QueryType.increment > QueryType.capacity) + QueryType.capacity <<= 1; } } internal static class ComponentType @@ -436,22 +456,6 @@ namespace DCFApixels.DragonECS ComponentType.types[uniqueID] = typeof(T); } } - - private void FillArray(T[] array, T value, int startIndex = 0, int length = -1) - { - if (length < 0) - { - length = array.Length; - } - else - { - length = startIndex + length; - } - for (int i = startIndex; i < length; i++) - { - array[i] = value; - } - } #endregion } diff --git a/src/Interfaces/IEcsReadonlyTable.cs b/src/Interfaces/IEcsReadonlyTable.cs index 773d043..917f4ce 100644 --- a/src/Interfaces/IEcsReadonlyTable.cs +++ b/src/Interfaces/IEcsReadonlyTable.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; namespace DCFApixels.DragonECS { @@ -7,26 +8,34 @@ namespace DCFApixels.DragonECS #region Properties /// Table Archetype public Type ArchetypeType { get; } - internal int Count { get; } - internal int Capacity { get; } + public int Count { get; } + public int Capacity { get; } #endregion #region Methods - public ReadOnlySpan GetAllPools(); - public int GetComponentID(); - public EcsPool GetPool() where T : struct; + public ReadOnlySpan GetAllPools(); + public TQuery Query(out TQuery query) where TQuery : EcsQueryBase; - public bool IsMaskCompatible(int entity) where TInc : struct, IInc; - public bool IsMaskCompatible(int entity) where TInc : struct, IInc where TExc : struct, IExc; - public bool IsMaskCompatible(EcsComponentMask mask, int entity); - public bool IsMaskCompatibleWithout(EcsComponentMask mask, int entity, int otherPoolID); + public int GetComponentID(); + public bool IsMaskCompatible(int entityID) where TInc : struct, IInc where TExc : struct, IExc; + public bool IsMaskCompatible(EcsComponentMask mask, int entityID); + public bool IsMaskCompatibleWithout(EcsComponentMask mask, int entity, int componentID); #endregion #region Internal Methods - internal void OnEntityComponentAdded(int entityID, int changedPoolID); - internal void OnEntityComponentRemoved(int entityID, int changedPoolID); + internal void OnEntityComponentAdded(int entityID, int componentID); + internal void OnEntityComponentRemoved(int entityID, int componentID); internal void RegisterGroup(EcsGroup group); #endregion } + + public static class IEcsReadonlyTableExtensions + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsMaskCompatible(this IEcsReadonlyTable self, int entityID) where TInc : struct, IInc + { + return self.IsMaskCompatible(entityID); + } + } } diff --git a/src/Utils/ArrayUtility.cs b/src/Utils/ArrayUtility.cs new file mode 100644 index 0000000..8f2a70f --- /dev/null +++ b/src/Utils/ArrayUtility.cs @@ -0,0 +1,21 @@ +namespace DCFApixels.DragonECS +{ + internal static class ArrayUtility + { + public static void Fill(T[] array, T value, int startIndex = 0, int length = -1) + { + if (length < 0) + { + length = array.Length; + } + else + { + length = startIndex + length; + } + for (int i = startIndex; i < length; i++) + { + array[i] = value; + } + } + } +}