update EcsWorld

This commit is contained in:
Mikhail 2024-02-15 20:28:38 +08:00
parent b0f5ea97c0
commit e1260a96fc
3 changed files with 35 additions and 31 deletions

View File

@ -101,21 +101,21 @@ namespace DCFApixels.DragonECS
if (worldID < 0) if (worldID < 0)
{ {
worldID = (short)_worldIdDispenser.UseFree(); worldID = (short)_worldIdDispenser.UseFree();
if (worldID >= Worlds.Length)
{
Array.Resize(ref Worlds, Worlds.Length << 1);
}
} }
else else
{ {
if (Worlds[worldID] != null) if (worldID != _worldIdDispenser.NullID)
{ {
Throw.UndefinedException();
}
_worldIdDispenser.Use(worldID); _worldIdDispenser.Use(worldID);
} }
if (_worlds[worldID] != null)
{
_worldIdDispenser.Release(worldID);
Throw.UndefinedException();
}
}
id = worldID; id = worldID;
Worlds[worldID] = this; _worlds[worldID] = this;
_poolsMediator = new PoolsMediator(this); _poolsMediator = new PoolsMediator(this);
@ -133,7 +133,7 @@ namespace DCFApixels.DragonECS
_gens = null; _gens = null;
_pools = null; _pools = null;
_nullPool = null; _nullPool = null;
Worlds[id] = null; _worlds[id] = null;
ReleaseData(id); ReleaseData(id);
_worldIdDispenser.Release(id); _worldIdDispenser.Release(id);
_isDestroyed = true; _isDestroyed = true;
@ -400,7 +400,7 @@ namespace DCFApixels.DragonECS
#region Upsize #region Upsize
public void Upsize(int minSize) public void Upsize(int minSize)
{ {
_entityDispenser.UpSize(minSize); _entityDispenser.Upsize(minSize);
} }
[MethodImpl(MethodImplOptions.NoInlining)] [MethodImpl(MethodImplOptions.NoInlining)]
private void OnEntityDispenserResized(int newSize) private void OnEntityDispenserResized(int newSize)

View File

@ -21,15 +21,15 @@ namespace DCFApixels.DragonECS
private const int DEL_ENT_BUFFER_SIZE_OFFSET = 5; private const int DEL_ENT_BUFFER_SIZE_OFFSET = 5;
private const int DEL_ENT_BUFFER_MIN_SIZE = 64; private const int DEL_ENT_BUFFER_MIN_SIZE = 64;
private static EcsWorld[] Worlds = new EcsWorld[4]; private static EcsWorld[] _worlds = Array.Empty<EcsWorld>();
private static IdDispenser _worldIdDispenser = new IdDispenser(4, 0); private static IdDispenser _worldIdDispenser = new IdDispenser(4, 0, OnWorldIdDispenser);
private static List<DataReleaser> _dataReleaseres = new List<DataReleaser>(); private static List<DataReleaser> _dataReleaseres = new List<DataReleaser>();
//public static int Copacity => Worlds.Length; //public static int Copacity => Worlds.Length;
static EcsWorld() static EcsWorld()
{ {
Worlds[0] = new NullWorld(); _worlds[0] = new NullWorld();
} }
private static void ReleaseData(int worldID) private static void ReleaseData(int worldID)
{ {
@ -38,8 +38,12 @@ namespace DCFApixels.DragonECS
_dataReleaseres[i].Release(worldID); _dataReleaseres[i].Release(worldID);
} }
} }
public static void OnWorldIdDispenser(int newSize)
{
Array.Resize(ref _worlds, newSize);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static EcsWorld GetWorld(int worldID) => Worlds[worldID]; public static EcsWorld GetWorld(int worldID) => _worlds[worldID];
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T GetData<T>(int worldID) => ref WorldComponentPool<T>.GetForWorld(worldID); public static ref T GetData<T>(int worldID) => ref WorldComponentPool<T>.GetForWorld(worldID);
@ -81,9 +85,9 @@ namespace DCFApixels.DragonECS
} }
public static int GetItemIndex(int worldID) 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]; ref short itemIndex = ref _mapping[worldID];
if (itemIndex <= 0) if (itemIndex <= 0)
@ -101,7 +105,7 @@ namespace DCFApixels.DragonECS
{ {
Array.Resize(ref _items, _items.Length << 1); 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()); _dataReleaseres.Add(new Releaser());
} }
return itemIndex; return itemIndex;
@ -111,7 +115,7 @@ namespace DCFApixels.DragonECS
ref short itemIndex = ref _mapping[worldID]; ref short itemIndex = ref _mapping[worldID];
if (itemIndex != 0) if (itemIndex != 0)
{ {
_interface.OnDestroy(ref _items[itemIndex], Worlds[worldID]); _interface.OnDestroy(ref _items[itemIndex], _worlds[worldID]);
_recycledItems[_recycledItemsCount++] = itemIndex; _recycledItems[_recycledItemsCount++] = itemIndex;
} }
} }

View File

@ -51,18 +51,18 @@ namespace DCFApixels.DragonECS.Internal
#region Use/Reserve/Realese #region Use/Reserve/Realese
/// <summary>Marks as used and returns next free id.</summary> /// <summary>Marks as used and returns next free id.</summary>
public int UseFree() //+ public int UseFree()
{ {
int ptr = _usedCount; int ptr = _usedCount;
CheckOrResize(ptr); CheckIDOrUpsize(ptr);
int id = _dense[ptr]; int id = _dense[ptr];
Move_FromFree_ToUsed(id); Move_FromFree_ToUsed(id);
return id; return id;
} }
/// <summary>Marks as used a free or reserved id, after this id cannot be retrieved via UseFree.</summary> /// <summary>Marks as used a free or reserved id, after this id cannot be retrieved via UseFree.</summary>
public void Use(int id) //+ public void Use(int id)
{ {
CheckOrResize(id); CheckIDOrUpsize(id);
#if DEBUG #if DEBUG
if (IsUsed(id) || IsNullID(id)) if (IsUsed(id) || IsNullID(id))
{ {
@ -73,9 +73,9 @@ namespace DCFApixels.DragonECS.Internal
Move_FromFree_ToUsed(id); Move_FromFree_ToUsed(id);
} }
public void Release(int id) //+ public void Release(int id)
{ {
CheckOrResize(id); CheckIDOrUpsize(id);
#if DEBUG #if DEBUG
if (IsFree(id) || IsNullID(id)) if (IsFree(id) || IsNullID(id))
{ {
@ -172,13 +172,13 @@ namespace DCFApixels.DragonECS.Internal
} }
#endregion #endregion
#region UpSize #region Upsize
[MethodImpl(MethodImplOptions.NoInlining)] [MethodImpl(MethodImplOptions.NoInlining)]
public void UpSize(int minSize) public void Upsize(int minSize)
{ {
if (minSize > _size) if (minSize > _size)
{ {
UpSize_Internal(minSize); Upsize_Internal(minSize);
} }
} }
#endregion #endregion
@ -189,7 +189,7 @@ namespace DCFApixels.DragonECS.Internal
_nullID = nullID; _nullID = nullID;
if (nullID >= 0) if (nullID >= 0)
{ {
CheckOrResize(nullID); CheckIDOrUpsize(nullID);
Swap(nullID, _usedCount++); Swap(nullID, _usedCount++);
} }
} }
@ -205,11 +205,11 @@ namespace DCFApixels.DragonECS.Internal
return true; return true;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void CheckOrResize(int id) private void CheckIDOrUpsize(int id)
{ {
if (id >= _size) if (id >= _size)
{ {
UpSize_Internal(id + 1); Upsize_Internal(id + 1);
} }
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -234,7 +234,7 @@ namespace DCFApixels.DragonECS.Internal
_sparse[sparseIndex] = denseIndex; _sparse[sparseIndex] = denseIndex;
} }
[MethodImpl(MethodImplOptions.NoInlining)] [MethodImpl(MethodImplOptions.NoInlining)]
private void UpSize_Internal(int minSize) private void Upsize_Internal(int minSize)
{ {
Resize(ArrayUtility.NormalizeSizeToPowerOfTwo_ClampOverflow(minSize)); Resize(ArrayUtility.NormalizeSizeToPowerOfTwo_ClampOverflow(minSize));
} }