diff --git a/src/Builtin/Worlds.cs b/src/Builtin/Worlds.cs index d2cda2f..27f627a 100644 --- a/src/Builtin/Worlds.cs +++ b/src/Builtin/Worlds.cs @@ -10,8 +10,9 @@ namespace DCFApixels.DragonECS [MetaID("4EE3527C92015BAB0299CB7B4E2663D1")] public sealed class EcsDefaultWorld : EcsWorld, IInjectionUnit { - public EcsDefaultWorld(EcsWorldConfig config, short worldID = -1) : base(config, worldID) { } - public EcsDefaultWorld(IConfigContainer configs = null, short worldID = -1) : base(configs, worldID) { } + public EcsDefaultWorld() : base() { } + public EcsDefaultWorld(EcsWorldConfig config = null, string name = null, short worldID = -1) : base(config, name == null ? "Default" : name, worldID) { } + public EcsDefaultWorld(IConfigContainer configs, string name = null, short worldID = -1) : base(configs, name == null ? "Default" : name, worldID) { } void IInjectionUnit.InitInjectionNode(InjectionGraph nodes) { nodes.AddNode(this); } } /// EcsWrold for store event entities. @@ -22,8 +23,9 @@ namespace DCFApixels.DragonECS [MetaID("D7CE527C920160BCD765EFA72DBF8B89")] public sealed class EcsEventWorld : EcsWorld, IInjectionUnit { - public EcsEventWorld(EcsWorldConfig config, short worldID = -1) : base(config, worldID) { } - public EcsEventWorld(IConfigContainer configs = null, short worldID = -1) : base(configs, worldID) { } + public EcsEventWorld() : base() { } + public EcsEventWorld(EcsWorldConfig config = null, string name = null, short worldID = -1) : base(config, name == null ? "Events" : name, worldID) { } + public EcsEventWorld(IConfigContainer configs, string name = null, short worldID = -1) : base(configs, name == null ? "Events" : name, worldID) { } void IInjectionUnit.InitInjectionNode(InjectionGraph nodes) { nodes.AddNode(this); } } } diff --git a/src/Collections/EcsGroup.cs b/src/Collections/EcsGroup.cs index bd403ba..9492aff 100644 --- a/src/Collections/EcsGroup.cs +++ b/src/Collections/EcsGroup.cs @@ -190,7 +190,7 @@ namespace DCFApixels.DragonECS #region Pages internal int* TakePage() { - if(_groupSparsePagePoolCount <= 0) + if (_groupSparsePagePoolCount <= 0) { var x = UnmanagedArrayUtility.NewAndInit(EcsGroup.PAGE_SIZE); return x; @@ -308,17 +308,17 @@ namespace DCFApixels.DragonECS #endif return _dense[++index]; } -// [MethodImpl(MethodImplOptions.AggressiveInlining)] -// set -// { -// // TODO добавить лок енумератора на изменение -//#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS -// if (index < 0 || index >= Count) { Throw.ArgumentOutOfRange(); } -//#endif -// var oldValue = _dense[index]; -// _dense[index] = value; -// _sparse[oldValue] = 0; -// } + // [MethodImpl(MethodImplOptions.AggressiveInlining)] + // set + // { + // // TODO добавить лок енумератора на изменение + //#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS + // if (index < 0 || index >= Count) { Throw.ArgumentOutOfRange(); } + //#endif + // var oldValue = _dense[index]; + // _dense[index] = value; + // _sparse[oldValue] = 0; + // } } #endregion @@ -393,7 +393,7 @@ namespace DCFApixels.DragonECS //_sparse[entityID] = _count; PageSlot* page = _sparsePages + (entityID >> PageSlot.SHIFT); - if(page->Count == 0) + if (page->Count == 0) { page->Indexes = _source.TakePage(); } @@ -430,7 +430,7 @@ namespace DCFApixels.DragonECS _dense[page->Indexes[localEntityID]] = _dense[_count]; - int localLastIndex = _dense[_count--]; + int localLastIndex = _dense[_count--]; int* lastPageArray = (_sparsePages + (localLastIndex >> PageSlot.SHIFT))->Indexes; localLastIndex = localLastIndex & PageSlot.MASK; diff --git a/src/EcsPipeline.cs b/src/EcsPipeline.cs index 47c82c7..93460ad 100644 --- a/src/EcsPipeline.cs +++ b/src/EcsPipeline.cs @@ -11,6 +11,10 @@ using static DCFApixels.DragonECS.EcsConsts; namespace DCFApixels.DragonECS { public interface IEcsMember { } + public interface INamedMember + { + string Name { get; } + } [MetaColor(MetaColor.DragonRose)] [MetaGroup(PACK_GROUP, OTHER_GROUP)] diff --git a/src/EcsWorld.cs b/src/EcsWorld.cs index 48c7ae0..5c060a7 100644 --- a/src/EcsWorld.cs +++ b/src/EcsWorld.cs @@ -6,11 +6,13 @@ using System.Collections.Generic; using System.Diagnostics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +#if ENABLE_IL2CPP +using Unity.IL2CPP.CompilerServices; +#endif namespace DCFApixels.DragonECS { #if ENABLE_IL2CPP - using Unity.IL2CPP.CompilerServices; [Il2CppSetOption(Option.NullChecks, false)] [Il2CppSetOption(Option.ArrayBoundsChecks, false)] #endif @@ -41,10 +43,11 @@ namespace DCFApixels.DragonECS [MetaDescription(EcsConsts.AUTHOR, "It is a container for entities and components.")] [MetaID("AEF3557C92019C976FC48F90E95A9DA6")] [DebuggerTypeProxy(typeof(DebuggerProxy))] - public partial class EcsWorld : IEntityStorage, IEcsMember + public partial class EcsWorld : IEntityStorage, IEcsMember, INamedMember { public readonly short ID; - private IConfigContainer _configs; + private readonly IConfigContainer _configs; + private readonly string _name; private bool _isDestroyed = false; @@ -73,6 +76,7 @@ namespace DCFApixels.DragonECS #region Properties EcsWorld IEntityStorage.World { + [MethodImpl(MethodImplOptions.AggressiveInlining)] get { return this; } } public IConfigContainer Configs @@ -80,6 +84,11 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] get { return _configs; } } + public string Name + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get { return _name; } + } public long Version { [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -134,12 +143,15 @@ namespace DCFApixels.DragonECS #endregion #region Constructors/Destroy - public EcsWorld(EcsWorldConfig config, short worldID = -1) : this(config == null ? ConfigContainer.Empty : new ConfigContainer().Set(config), worldID) { } - public EcsWorld(IConfigContainer configs = null, short worldID = -1) + public EcsWorld() : this(ConfigContainer.Empty, null, -1) { } + public EcsWorld(EcsWorldConfig config = null, string name = null, short worldID = -1) : this(config == null ? ConfigContainer.Empty : new ConfigContainer().Set(config), name, worldID) { } + public EcsWorld(IConfigContainer configs, string name = null, short worldID = -1) { lock (_worldLock) { if (configs == null) { configs = ConfigContainer.Empty; } + if (name == null) { name = string.Empty; } + _name = name; bool nullWorld = this is NullWorld; if (nullWorld == false && worldID == NULL_WORLD_ID) { @@ -264,6 +276,18 @@ namespace DCFApixels.DragonECS #region New/Del [MethodImpl(MethodImplOptions.AggressiveInlining)] + public entlong NewEntityLong() + { + int entityID = NewEntity(); + return GetEntityLong(entityID); + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public entlong NewEntityLong(int entityID) + { + NewEntity(entityID); + return GetEntityLong(entityID); + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public int NewEntity() { int entityID = _entityDispenser.UseFree(); @@ -294,24 +318,26 @@ namespace DCFApixels.DragonECS _entityListeners.InvokeOnNewEntity(entityID); } - public entlong NewEntityLong() - { - int entityID = NewEntity(); - return GetEntityLong(entityID); - } - public entlong NewEntityLong(int entityID) - { - NewEntity(entityID); - return GetEntityLong(entityID); - } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void TryDelEntity(int entityID) + public bool TryDelEntity(entlong entity) + { + if (entity.TryGetID(out int entityID)) + { + TryDelEntity(entityID); + return true; + } + return false; + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool TryDelEntity(int entityID) { if (IsUsed(entityID)) { DelEntity(entityID); + return true; } + return false; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void DelEntity(entlong entity) @@ -360,6 +386,15 @@ namespace DCFApixels.DragonECS return slot.gen == gen && slot.isUsed; } [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool IsAlive(entlong entity) + { +#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS + if (entity.GetWorldIDUnchecked() != ID) { Throw.World_MaskDoesntBelongWorld(); } +#endif + ref var slot = ref _entities[entity.GetIDUnchecked()]; + return slot.gen == entity.GetIDUnchecked() && slot.isUsed; + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool IsUsed(int entityID) { return _entities[entityID].isUsed; diff --git a/src/EcsWorld.pools.cs b/src/EcsWorld.pools.cs index 03802f6..9dd0ee7 100644 --- a/src/EcsWorld.pools.cs +++ b/src/EcsWorld.pools.cs @@ -94,6 +94,10 @@ namespace DCFApixels.DragonECS { return DeclareOrGetComponentTypeID(EcsTypeCodeManager.Get(componentType)); } + public Type GetComponentType(int componentTypeID) + { + return _pools[componentTypeID].ComponentType; + } public bool IsComponentTypeDeclared() { return _cmpTypeCode_2_CmpTypeIDs.Contains((int)EcsTypeCodeManager.Get()); @@ -102,6 +106,7 @@ namespace DCFApixels.DragonECS { return _cmpTypeCode_2_CmpTypeIDs.Contains((int)EcsTypeCodeManager.Get(componentType)); } + //TODO пересмотреть нейминг или функцию public bool IsComponentTypeDeclared(int componentTypeID) { if (componentTypeID >= 0 && componentTypeID < _pools.Length) @@ -110,10 +115,6 @@ namespace DCFApixels.DragonECS } return false; } - public Type GetComponentType(int componentTypeID) - { - return _pools[componentTypeID].ComponentType; - } #endregion #region Declare @@ -364,10 +365,10 @@ namespace DCFApixels.DragonECS #endregion #region LockPool/UnLockPool - public void LockPool_Debug(int ComponentTypeID) + public void LockPool_Debug(int componentTypeID) { #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - ref var slot = ref _poolSlots[ComponentTypeID]; + ref var slot = ref _poolSlots[componentTypeID]; if (slot.locked == false) { slot.locked = true; @@ -376,14 +377,14 @@ namespace DCFApixels.DragonECS ReleaseDelEntityBufferAll(); } _lockedPoolCount++; - _pools[ComponentTypeID].OnLockedChanged_Debug(true); + _pools[componentTypeID].OnLockedChanged_Debug(true); } #endif } - public void UnlockPool_Debug(int ComponentTypeID) + public void UnlockPool_Debug(int componentTypeID) { #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - ref var slot = ref _poolSlots[ComponentTypeID]; + ref var slot = ref _poolSlots[componentTypeID]; if (slot.locked == true) { slot.locked = false; @@ -394,21 +395,21 @@ namespace DCFApixels.DragonECS _lockedPoolCount = 0; Throw.OpeningClosingMethodsBalanceError(); } - _pools[ComponentTypeID].OnLockedChanged_Debug(false); + _pools[componentTypeID].OnLockedChanged_Debug(false); } #endif } - public bool CheckPoolLocked_Debug(int ComponentTypeID) + public bool CheckPoolLocked_Debug(int componentTypeID) { #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - return _poolSlots[ComponentTypeID].locked; + return _poolSlots[componentTypeID].locked; #else return false; #endif } #endregion - #region PoolSlot + #region Utils internal struct PoolSlot { public int count; diff --git a/src/EcsWorld.static.cs b/src/EcsWorld.static.cs index 51a37ba..bf268b3 100644 --- a/src/EcsWorld.static.cs +++ b/src/EcsWorld.static.cs @@ -163,7 +163,7 @@ namespace DCFApixels.DragonECS } private sealed class NullWorld : EcsWorld { - internal NullWorld() : base(new EcsWorldConfig(4, 4, 4, 4, 4), 0) { } + internal NullWorld() : base(new EcsWorldConfig(4, 4, 4, 4, 4), null, 0) { } } #region Obsolete diff --git a/src/Pools/EcsPool.cs b/src/Pools/EcsPool.cs index c3f624b..a621c26 100644 --- a/src/Pools/EcsPool.cs +++ b/src/Pools/EcsPool.cs @@ -3,6 +3,7 @@ using DCFApixels.DragonECS.PoolsCore; using System; using System.Collections; using System.Collections.Generic; +using System.Diagnostics; using System.Runtime.CompilerServices; #if ENABLE_IL2CPP using Unity.IL2CPP.CompilerServices; @@ -25,6 +26,7 @@ namespace DCFApixels.DragonECS [MetaGroup(EcsConsts.PACK_GROUP, EcsConsts.POOLS_GROUP)] [MetaDescription(EcsConsts.AUTHOR, "Pool for IEcsComponent components.")] [MetaID("C501547C9201A4B03FC25632E4FAAFD7")] + [DebuggerDisplay("Count: {Count}")] public sealed class EcsPool : IEcsPoolImplementation, IEcsStructPool, IEnumerable //IEnumerable - IntelliSense hack where T : struct, IEcsComponent { diff --git a/src/Pools/EcsPoolBase.cs b/src/Pools/EcsPoolBase.cs index 3bed942..616de39 100644 --- a/src/Pools/EcsPoolBase.cs +++ b/src/Pools/EcsPoolBase.cs @@ -2,6 +2,7 @@ using DCFApixels.DragonECS.PoolsCore; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Runtime.CompilerServices; namespace DCFApixels.DragonECS.PoolsCore @@ -58,6 +59,7 @@ namespace DCFApixels.DragonECS.Internal [MetaDescription(EcsConsts.AUTHOR, "A placeholder type, an instance of this type replaces the null ref.")] [MetaTags(MetaTags.HIDDEN)] [MetaID("460E547C9201227A4956AC297F67B484")] + [DebuggerDisplay("-")] public sealed class EcsNullPool : IEcsPoolImplementation { public static readonly EcsNullPool instance = new EcsNullPool(); diff --git a/src/Pools/EcsTagPool.cs b/src/Pools/EcsTagPool.cs index 0fe8260..a510bf8 100644 --- a/src/Pools/EcsTagPool.cs +++ b/src/Pools/EcsTagPool.cs @@ -3,6 +3,8 @@ using System; using System.Collections; using System.Collections.Generic; using System.Runtime.CompilerServices; +using System.Diagnostics; + #if (DEBUG && !DISABLE_DEBUG) using System.Reflection; #endif @@ -27,6 +29,7 @@ namespace DCFApixels.DragonECS [MetaGroup(EcsConsts.PACK_GROUP, EcsConsts.POOLS_GROUP)] [MetaDescription(EcsConsts.AUTHOR, "Pool for IEcsTagComponent components. EcsTagPool is optimized for storing tag components or components without data.")] [MetaID("9D80547C9201E852E4F17324EAC1E15A")] + [DebuggerDisplay("Count: {Count}")] public sealed class EcsTagPool : IEcsPoolImplementation, IEcsStructPool, IEnumerable //IEnumerable - IntelliSense hack where T : struct, IEcsTagComponent { diff --git a/src/Utils/ITemplateNode.cs b/src/Utils/ITemplateNode.cs index 887dcfc..c4419ef 100644 --- a/src/Utils/ITemplateNode.cs +++ b/src/Utils/ITemplateNode.cs @@ -6,6 +6,16 @@ namespace DCFApixels.DragonECS } public static class ITemplateNodeExtensions { + public static int ApplyAndReturn(this ITemplateNode self, short worldID, int entityID) + { + self.Apply(worldID, entityID); + return entityID; + } + public static entlong ApplyAndReturnLong(this ITemplateNode self, short worldID, int entityID) + { + self.Apply(worldID, entityID); + return (EcsWorld.GetWorld(worldID), entityID); + } public static int NewEntity(this EcsWorld world, ITemplateNode template) { int e = world.NewEntity(); @@ -18,5 +28,18 @@ namespace DCFApixels.DragonECS template.Apply(world.ID, e.ID); return e; } + + public static int NewEntity(this EcsWorld world, int entityID, ITemplateNode template) + { + int e = world.NewEntity(entityID); + template.Apply(world.ID, e); + return e; + } + public static entlong NewEntityLong(this EcsWorld world, int entityID, ITemplateNode template) + { + entlong e = world.NewEntityLong(entityID); + template.Apply(world.ID, e.ID); + return e; + } } } \ No newline at end of file