diff --git a/src/EcsWorld.cs b/src/EcsWorld.cs index cff42e2..311bf7e 100644 --- a/src/EcsWorld.cs +++ b/src/EcsWorld.cs @@ -101,21 +101,21 @@ namespace DCFApixels.DragonECS if (worldID < 0) { worldID = (short)_worldIdDispenser.UseFree(); - if (worldID >= Worlds.Length) - { - Array.Resize(ref Worlds, Worlds.Length << 1); - } } else { - if (Worlds[worldID] != null) + if (worldID != _worldIdDispenser.NullID) { + _worldIdDispenser.Use(worldID); + } + if (_worlds[worldID] != null) + { + _worldIdDispenser.Release(worldID); Throw.UndefinedException(); } - _worldIdDispenser.Use(worldID); } id = worldID; - Worlds[worldID] = this; + _worlds[worldID] = this; _poolsMediator = new PoolsMediator(this); @@ -133,7 +133,7 @@ namespace DCFApixels.DragonECS _gens = null; _pools = null; _nullPool = null; - Worlds[id] = null; + _worlds[id] = null; ReleaseData(id); _worldIdDispenser.Release(id); _isDestroyed = true; @@ -393,14 +393,14 @@ namespace DCFApixels.DragonECS } private void Densify() //уплотнение свободных айдишников { - _entityDispenser.Sort(); + _entityDispenser.Sort(); } #endregion #region Upsize public void Upsize(int minSize) { - _entityDispenser.UpSize(minSize); + _entityDispenser.Upsize(minSize); } [MethodImpl(MethodImplOptions.NoInlining)] private void OnEntityDispenserResized(int newSize) diff --git a/src/EcsWorld.static.cs b/src/EcsWorld.static.cs index 882b3ed..baa3a89 100644 --- a/src/EcsWorld.static.cs +++ b/src/EcsWorld.static.cs @@ -21,15 +21,15 @@ namespace DCFApixels.DragonECS private const int DEL_ENT_BUFFER_SIZE_OFFSET = 5; private const int DEL_ENT_BUFFER_MIN_SIZE = 64; - private static EcsWorld[] Worlds = new EcsWorld[4]; - private static IdDispenser _worldIdDispenser = new IdDispenser(4, 0); + private static EcsWorld[] _worlds = Array.Empty(); + private static IdDispenser _worldIdDispenser = new IdDispenser(4, 0, OnWorldIdDispenser); private static List _dataReleaseres = new List(); //public static int Copacity => Worlds.Length; static EcsWorld() { - Worlds[0] = new NullWorld(); + _worlds[0] = new NullWorld(); } private static void ReleaseData(int worldID) { @@ -38,8 +38,12 @@ namespace DCFApixels.DragonECS _dataReleaseres[i].Release(worldID); } } + public static void OnWorldIdDispenser(int newSize) + { + Array.Resize(ref _worlds, newSize); + } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static EcsWorld GetWorld(int worldID) => Worlds[worldID]; + public static EcsWorld GetWorld(int worldID) => _worlds[worldID]; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ref T GetData(int worldID) => ref WorldComponentPool.GetForWorld(worldID); @@ -81,9 +85,9 @@ namespace DCFApixels.DragonECS } public static int GetItemIndex(int worldID) { - if (_mapping.Length < Worlds.Length) + if (_mapping.Length < _worlds.Length) { - Array.Resize(ref _mapping, Worlds.Length); + Array.Resize(ref _mapping, _worlds.Length); } ref short itemIndex = ref _mapping[worldID]; if (itemIndex <= 0) @@ -101,7 +105,7 @@ namespace DCFApixels.DragonECS { Array.Resize(ref _items, _items.Length << 1); } - _interface.Init(ref _items[itemIndex], Worlds[worldID]); + _interface.Init(ref _items[itemIndex], _worlds[worldID]); _dataReleaseres.Add(new Releaser()); } return itemIndex; @@ -111,7 +115,7 @@ namespace DCFApixels.DragonECS ref short itemIndex = ref _mapping[worldID]; if (itemIndex != 0) { - _interface.OnDestroy(ref _items[itemIndex], Worlds[worldID]); + _interface.OnDestroy(ref _items[itemIndex], _worlds[worldID]); _recycledItems[_recycledItemsCount++] = itemIndex; } } diff --git a/src/Internal/IdDispenser.cs b/src/Internal/IdDispenser.cs index df19e0f..685e1cc 100644 --- a/src/Internal/IdDispenser.cs +++ b/src/Internal/IdDispenser.cs @@ -51,18 +51,18 @@ namespace DCFApixels.DragonECS.Internal #region Use/Reserve/Realese /// Marks as used and returns next free id. - public int UseFree() //+ + public int UseFree() { int ptr = _usedCount; - CheckOrResize(ptr); + CheckIDOrUpsize(ptr); int id = _dense[ptr]; Move_FromFree_ToUsed(id); return id; } /// Marks as used a free or reserved id, after this id cannot be retrieved via UseFree. - public void Use(int id) //+ + public void Use(int id) { - CheckOrResize(id); + CheckIDOrUpsize(id); #if DEBUG if (IsUsed(id) || IsNullID(id)) { @@ -73,9 +73,9 @@ namespace DCFApixels.DragonECS.Internal Move_FromFree_ToUsed(id); } - public void Release(int id) //+ + public void Release(int id) { - CheckOrResize(id); + CheckIDOrUpsize(id); #if DEBUG if (IsFree(id) || IsNullID(id)) { @@ -172,13 +172,13 @@ namespace DCFApixels.DragonECS.Internal } #endregion - #region UpSize + #region Upsize [MethodImpl(MethodImplOptions.NoInlining)] - public void UpSize(int minSize) + public void Upsize(int minSize) { if (minSize > _size) { - UpSize_Internal(minSize); + Upsize_Internal(minSize); } } #endregion @@ -189,7 +189,7 @@ namespace DCFApixels.DragonECS.Internal _nullID = nullID; if (nullID >= 0) { - CheckOrResize(nullID); + CheckIDOrUpsize(nullID); Swap(nullID, _usedCount++); } } @@ -205,11 +205,11 @@ namespace DCFApixels.DragonECS.Internal return true; } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void CheckOrResize(int id) + private void CheckIDOrUpsize(int id) { if (id >= _size) { - UpSize_Internal(id + 1); + Upsize_Internal(id + 1); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -234,7 +234,7 @@ namespace DCFApixels.DragonECS.Internal _sparse[sparseIndex] = denseIndex; } [MethodImpl(MethodImplOptions.NoInlining)] - private void UpSize_Internal(int minSize) + private void Upsize_Internal(int minSize) { Resize(ArrayUtility.NormalizeSizeToPowerOfTwo_ClampOverflow(minSize)); }