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)
{
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)

View File

@ -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<EcsWorld>();
private static IdDispenser _worldIdDispenser = new IdDispenser(4, 0, OnWorldIdDispenser);
private static List<DataReleaser> _dataReleaseres = new List<DataReleaser>();
//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<T>(int worldID) => ref WorldComponentPool<T>.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;
}
}

View File

@ -51,18 +51,18 @@ namespace DCFApixels.DragonECS.Internal
#region Use/Reserve/Realese
/// <summary>Marks as used and returns next free id.</summary>
public int UseFree() //+
public int UseFree()
{
int ptr = _usedCount;
CheckOrResize(ptr);
CheckIDOrUpsize(ptr);
int id = _dense[ptr];
Move_FromFree_ToUsed(id);
return id;
}
/// <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 (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));
}