diff --git a/src/EcsWorld.cs b/src/EcsWorld.cs index 194da51..1227d6a 100644 --- a/src/EcsWorld.cs +++ b/src/EcsWorld.cs @@ -31,7 +31,6 @@ namespace DCFApixels.DragonECS internal IEcsPoolImplementation[] _pools; private EcsNullPool _nullPool; - private int _poolsCount = 0; private EcsSubject[] _subjects; private EcsQueryExecutor[] _executors; @@ -48,15 +47,10 @@ namespace DCFApixels.DragonECS public int Capacity => _entitesCapacity; //_denseEntities.Length; public EcsReadonlyGroup Entities => _allEntites.Readonly; public ReadOnlySpan AllPools => _pools;// new ReadOnlySpan(pools, 0, _poolsCount); - public int PoolsCount => _poolsCount; #endregion #region Constructors/Destroy - static EcsWorld() - { - EcsNullWorld nullWorld = new EcsNullWorld(); - Worlds[0] = nullWorld; - } + static EcsWorld() => Worlds[0] = new EcsNullWorld(); public EcsWorld() : this(true) { } internal EcsWorld(bool isIndexable) { @@ -95,25 +89,24 @@ namespace DCFApixels.DragonECS public void Destroy() { _entityDispenser = null; - //_denseEntities = null; _gens = null; _pools = null; _nullPool = null; _subjects = null; _executors = null; - Worlds[uniqueID] = null; _worldIdDispenser.Release(uniqueID); } #endregion - #region GetComponentID + #region ComponentInfo public int GetComponentID() => WorldMetaStorage.GetComponentId(_worldTypeID); - public bool IsComponentTypeDeclared() => WorldMetaStorage.IsComponentTypeDeclared(_worldTypeID); - + public Type GetComponentType(int componentID) => WorldMetaStorage.GetComponentType(_worldTypeID, componentID); + public bool IsComponentTypeDeclared() => IsComponentTypeDeclared(typeof(T)); + public bool IsComponentTypeDeclared(Type type) => WorldMetaStorage.IsComponentTypeDeclared(_worldTypeID, type); #endregion - #region GetPool + #region Getters #if UNITY_2020_3_OR_NEWER [UnityEngine.Scripting.Preserve] #endif @@ -131,13 +124,9 @@ namespace DCFApixels.DragonECS var pool = new TPool(); _pools[uniqueID] = pool; pool.OnInit(this, uniqueID); - _poolsCount++; } return (TPool)_pools[uniqueID]; } - #endregion - - #region Subjects public TSubject GetSubject() where TSubject : EcsSubject { int uniqueID = WorldMetaStorage.GetSubjectId(_worldTypeID); @@ -147,9 +136,6 @@ namespace DCFApixels.DragonECS _subjects[uniqueID] = EcsSubject.Builder.Build(this); return (TSubject)_subjects[uniqueID]; } - #endregion - - #region Queries public TExecutor GetExecutor() where TExecutor : EcsQueryExecutor, new() { int id = WorldMetaStorage.GetExecutorId(_worldTypeID); @@ -163,6 +149,9 @@ namespace DCFApixels.DragonECS } return (TExecutor)_executors[id]; } + #endregion + + #region Where Query public EcsWhereResult WhereFor(EcsReadonlyGroup sourceGroup, out TSubject subject) where TSubject : EcsSubject { var executor = GetExecutor>(); @@ -185,27 +174,6 @@ namespace DCFApixels.DragonECS } #endregion - #region IsMatchesMask - public bool IsMatchesMask(EcsMask mask, int entityID) - { -#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS - if (mask._worldType != Archetype) - 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(entityID)) - return false; - } - for (int i = 0, iMax = mask._exc.Length; i < iMax; i++) - { - if (_pools[mask._exc[i]].Has(entityID)) - return false; - } - return true; - } - #endregion - #region Entity public int NewEmptyEntity() { @@ -247,7 +215,6 @@ namespace DCFApixels.DragonECS int e = NewEmptyEntity(); return GetEntityLong(e); } - public void DelEntity(int entityID) { _allEntites.Remove(entityID); @@ -265,6 +232,29 @@ namespace DCFApixels.DragonECS public bool IsAlive(int entityID, short gen) => _gens[entityID] == gen; [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool IsUsed(int entityID) => _gens[entityID] >= 0; + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public short GetGen(int entityID) => _gens[entityID]; + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public short GetComponentsCount(int entityID) => _componentCounts[entityID]; + + public bool IsMatchesMask(EcsMask mask, int entityID) + { +#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS + if (mask._worldType != Archetype) + throw new EcsFrameworkException("The types of the target world of the mask and this world are different."); +#endif + for (int i = 0, iMax = mask._inc.Length; i < iMax; i++) + { + 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(entityID)) + return false; + } + return true; + } public void ReleaseDelEntityBuffer() { ReadOnlySpan buffser = new ReadOnlySpan(_delEntBuffer, 0, _delEntBufferCount); @@ -275,10 +265,6 @@ namespace DCFApixels.DragonECS _entityDispenser.Release(_delEntBuffer[i]); _delEntBufferCount = 0; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public short GetGen(int entityID) => _gens[entityID]; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public short GetComponentsCount(int entityID) => _componentCounts[entityID]; public void DeleteEmptyEntites() { foreach (var e in _allEntites) @@ -287,11 +273,12 @@ namespace DCFApixels.DragonECS } } + #region Copy/Clone public void CopyEntity(int fromEntityID, int toEntityID) { foreach (var pool in _pools) { - if(pool.Has(fromEntityID)) pool.Copy(fromEntityID, toEntityID); + if (pool.Has(fromEntityID)) pool.Copy(fromEntityID, toEntityID); } } public int CloneEntity(int fromEntityID) @@ -305,24 +292,28 @@ namespace DCFApixels.DragonECS CopyEntity(fromEntityID, toEntityID); foreach (var pool in _pools) { - if (!pool.Has(fromEntityID)&& pool.Has(toEntityID)) - pool.Del(toEntityID); + if (!pool.Has(fromEntityID) && pool.Has(toEntityID)) + pool.Del(toEntityID); } } + #endregion + #region Components Increment [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void IncrementEntityComponentCount(int entityID) => _componentCounts[entityID]++; [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void DecrementEntityComponentCount(int entityID) { var count = --_componentCounts[entityID]; - if(count == 0 && _allEntites.Has(entityID)) DelEntity(entityID); + if (count == 0 && _allEntites.Has(entityID)) DelEntity(entityID); #if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS if (count < 0) throw new EcsFrameworkException("нарушен баланс инкремента/декремента компонентов"); #endif } #endregion + #endregion + #region Groups Pool internal void RegisterGroup(EcsGroup group) { @@ -363,13 +354,11 @@ namespace DCFApixels.DragonECS } } #endregion + } - #region NullWorld - private sealed class EcsNullWorld : EcsWorld - { - public EcsNullWorld() : base(false) { } - } - #endregion + internal sealed class EcsNullWorld : EcsWorld + { + public EcsNullWorld() : base(false) { } } public abstract class EcsWorld : EcsWorld @@ -422,7 +411,6 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetExecutorId(int worldID) => Executor.Get(worldID); - public static bool IsComponentTypeDeclared(int worldID) => IsComponentTypeDeclared(worldID, typeof(TComponent)); public static bool IsComponentTypeDeclared(int worldID, Type type) => _metas[worldID].IsDeclaredType(type); public static Type GetComponentType(int worldID, int componentID) => _metas[worldID].GetComponentType(componentID); diff --git a/src/Exceptions/EcsFrameworkException.cs b/src/Exceptions/EcsFrameworkException.cs deleted file mode 100644 index 5101ee4..0000000 --- a/src/Exceptions/EcsFrameworkException.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace DCFApixels.DragonECS -{ - [Serializable] - public class EcsFrameworkException : Exception - { - public EcsFrameworkException() { } - public EcsFrameworkException(string message) : base(EcsConsts.EXCEPTION_MESSAGE_PREFIX + message) { } - public EcsFrameworkException(string message, Exception inner) : base(EcsConsts.EXCEPTION_MESSAGE_PREFIX + message, inner) { } - protected EcsFrameworkException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } - } -} diff --git a/src/Exceptions/EcsRelationException.cs b/src/Exceptions/EcsRelationException.cs deleted file mode 100644 index 9b2e60e..0000000 --- a/src/Exceptions/EcsRelationException.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace DCFApixels.DragonECS -{ - [Serializable] - public class EcsRelationException : EcsFrameworkException - { - public EcsRelationException() { } - public EcsRelationException(string message) : base(EcsConsts.EXCEPTION_MESSAGE_PREFIX + message) { } - public EcsRelationException(string message, Exception inner) : base(EcsConsts.EXCEPTION_MESSAGE_PREFIX + message, inner) { } - protected EcsRelationException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } - } -} diff --git a/src/Exceptions/EcsRunnerImplementationException.cs b/src/Exceptions/EcsRunnerImplementationException.cs deleted file mode 100644 index b5f2ad5..0000000 --- a/src/Exceptions/EcsRunnerImplementationException.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace DCFApixels.DragonECS -{ - [Serializable] - public class EcsRunnerImplementationException : EcsFrameworkException - { - public EcsRunnerImplementationException() { } - public EcsRunnerImplementationException(string message) : base(EcsConsts.EXCEPTION_MESSAGE_PREFIX + message) { } - public EcsRunnerImplementationException(string message, Exception inner) : base(EcsConsts.EXCEPTION_MESSAGE_PREFIX + message, inner) { } - protected EcsRunnerImplementationException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } - } -} diff --git a/src/Pools/EcsPool.cs b/src/Pools/EcsPool.cs index 366af85..2fff699 100644 --- a/src/Pools/EcsPool.cs +++ b/src/Pools/EcsPool.cs @@ -2,10 +2,10 @@ using System; using System.Collections; using System.Collections.Generic; using System.Runtime.CompilerServices; +using static DCFApixels.DragonECS.EcsPoolThrowHalper; namespace DCFApixels.DragonECS { - using static EcsPoolThrowHalper; /// Pool for IEcsComponent components public sealed class EcsPool : IEcsPoolImplementation, IEnumerable //IEnumerable - IntelliSense hack where T : struct, IEcsComponent diff --git a/src/Pools/EcsPoolBase.cs b/src/Pools/EcsPoolBase.cs index 1628aeb..155a477 100644 --- a/src/Pools/EcsPoolBase.cs +++ b/src/Pools/EcsPoolBase.cs @@ -59,14 +59,6 @@ namespace DCFApixels.DragonECS { throw new EcsFrameworkException($"Entity({entityID}) has no component {typeof(T).Name}."); } - public static void ThrowInvalidComponentType(Type invalidType) - { - throw new EcsFrameworkException($"Invalid component type, {typeof(T).Name} required."); - } - public static void ThrowWorldDifferent(int entityID) - { - throw new EcsRelationException($"The world of the target entity({entityID}) in {typeof(T).Name} is not the same as the world of the other entities. "); - } } public static class IEcsPoolImplementationExtensions { diff --git a/src/Pools/EcsTagPool.cs b/src/Pools/EcsTagPool.cs index 0f3cf8b..75a670b 100644 --- a/src/Pools/EcsTagPool.cs +++ b/src/Pools/EcsTagPool.cs @@ -2,10 +2,10 @@ using System; using System.Collections; using System.Collections.Generic; using System.Runtime.CompilerServices; +using static DCFApixels.DragonECS.EcsPoolThrowHalper; namespace DCFApixels.DragonECS { - using static EcsPoolThrowHalper; public sealed class EcsTagPool : IEcsPoolImplementation, IEnumerable //IEnumerable - IntelliSense hack where T : struct, IEcsTagComponent { diff --git a/src/Utils/Exceptions.cs b/src/Utils/Exceptions.cs new file mode 100644 index 0000000..a965542 --- /dev/null +++ b/src/Utils/Exceptions.cs @@ -0,0 +1,22 @@ +using System; +using System.Runtime.Serialization; + +namespace DCFApixels.DragonECS +{ + [Serializable] + public class EcsFrameworkException : Exception + { + public EcsFrameworkException() { } + public EcsFrameworkException(string message) : base(EcsConsts.EXCEPTION_MESSAGE_PREFIX + message) { } + public EcsFrameworkException(string message, Exception inner) : base(EcsConsts.EXCEPTION_MESSAGE_PREFIX + message, inner) { } + protected EcsFrameworkException(SerializationInfo info, StreamingContext context) : base(info, context) { } + } + [Serializable] + public class EcsRunnerImplementationException : EcsFrameworkException + { + public EcsRunnerImplementationException() { } + public EcsRunnerImplementationException(string message) : base(EcsConsts.EXCEPTION_MESSAGE_PREFIX + message) { } + public EcsRunnerImplementationException(string message, Exception inner) : base(EcsConsts.EXCEPTION_MESSAGE_PREFIX + message, inner) { } + protected EcsRunnerImplementationException(SerializationInfo info, StreamingContext context) : base(info, context) { } + } +}