This commit is contained in:
Mikhail 2024-02-07 22:16:41 +08:00
parent ddceb74a20
commit 7b2c64274a
4 changed files with 78 additions and 19 deletions

View File

@ -1,4 +1,4 @@
using DCFApixels.DragonECS.Utils; using DCFApixels.DragonECS.Internal;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;

View File

@ -3,7 +3,6 @@ using DCFApixels.DragonECS.Utils;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using static Leopotam.EcsLite.EcsWorld;
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
@ -114,12 +113,12 @@ namespace DCFApixels.DragonECS
_poolsMediator = new PoolsMediator(this); _poolsMediator = new PoolsMediator(this);
_entityDispenser = new IntDispenser(0); _entityDispenser = new IntDispenser(0);
int poolsCapacity = config.Get_PoolsCapacity(); int poolsCapacity = ArrayUtility.NormalizeSizeToPowerOfTwo(config.Get_PoolsCapacity());
_pools = new IEcsPoolImplementation[poolsCapacity]; _pools = new IEcsPoolImplementation[poolsCapacity];
_poolComponentCounts = new int[poolsCapacity]; _poolComponentCounts = new int[poolsCapacity];
ArrayUtility.Fill(_pools, _nullPool); ArrayUtility.Fill(_pools, _nullPool);
_entitesCapacity = config.Get_EntitiesCapacity(); _entitesCapacity = ArrayUtility.NormalizeSizeToPowerOfTwo(config.Get_EntitiesCapacity());
_gens = new short[_entitesCapacity]; _gens = new short[_entitesCapacity];
_componentCounts = new short[_entitesCapacity]; _componentCounts = new short[_entitesCapacity];
@ -267,22 +266,20 @@ namespace DCFApixels.DragonECS
#endregion #endregion
#region Entity #region Entity
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int NewEntity() public int NewEntity()
{ {
//if (_isEnableReleaseDelEntBuffer && _freeSpace <= 1 && _delEntBufferCount > _delEntBufferMinCount)
// ReleaseDelEntityBufferAll();
int entityID = _entityDispenser.GetFree(); int entityID = _entityDispenser.GetFree();
_freeSpace--; _freeSpace--;
_entitiesCount++; _entitiesCount++;
if (_gens.Length <= entityID) if (_gens.Length <= entityID)
{ {
Upsize(_gens.Length << 1); Upsize_Internal(_gens.Length << 1);
} }
_gens[entityID] &= GEN_BITS; _gens[entityID] &= GEN_BITS;
_allEntites.Add(entityID); _allEntites.AddUnchecked(entityID);
_entityListeners.InvokeOnNewEntity(entityID); _entityListeners.InvokeOnNewEntity(entityID);
return entityID; return entityID;
} }
@ -291,8 +288,23 @@ namespace DCFApixels.DragonECS
int e = NewEntity(); int e = NewEntity();
return GetEntityLong(e); return GetEntityLong(e);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void TryDelEntity(int entityID)
{
if (IsUsed(entityID))
{
DelEntity(entityID);
}
}
public void DelEntity(int entityID) public void DelEntity(int entityID)
{ {
#if DEBUG
if(IsUsed(entityID) == false)
{
Throw.UndefinedException();
}
#endif
_allEntites.Remove(entityID); _allEntites.Remove(entityID);
_delEntBuffer[_delEntBufferCount++] = entityID; _delEntBuffer[_delEntBufferCount++] = entityID;
_gens[entityID] |= DEATH_GEN_BIT; _gens[entityID] |= DEATH_GEN_BIT;
@ -446,16 +458,13 @@ namespace DCFApixels.DragonECS
#endregion #endregion
#region Upsize #region Upsize
[MethodImpl(MethodImplOptions.NoInlining)]
public void Upsize(int minSize) public void Upsize(int minSize)
{ {
if (minSize < Capacity) Upsize_Internal(ArrayUtility.NormalizeSizeToPowerOfTwo(minSize));
{ }
return; [MethodImpl(MethodImplOptions.NoInlining)]
} private void Upsize_Internal(int newSize)
{
int newSize = 1 << (BitsUtility.GetHighBitNumber(minSize - 1) + 1);
Array.Resize(ref _gens, newSize); Array.Resize(ref _gens, newSize);
Array.Resize(ref _componentCounts, newSize); Array.Resize(ref _componentCounts, newSize);
Array.Resize(ref _delEntBuffer, newSize); Array.Resize(ref _delEntBuffer, newSize);

View File

@ -1,3 +1,4 @@
using DCFApixels.DragonECS.Internal;
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
@ -54,6 +55,7 @@ namespace DCFApixels.DragonECS
} }
_mediator.RegisterComponent(entityID, _componentTypeID, _maskBit); _mediator.RegisterComponent(entityID, _componentTypeID, _maskBit);
_listeners.InvokeOnAddAndGet(entityID); _listeners.InvokeOnAddAndGet(entityID);
_componentResetHandler.Reset(ref _items[itemIndex]);
return ref _items[itemIndex]; return ref _items[itemIndex];
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -93,6 +95,7 @@ namespace DCFApixels.DragonECS
_listeners.InvokeOnAdd(entityID); _listeners.InvokeOnAdd(entityID);
} }
_listeners.InvokeOnGet(entityID); _listeners.InvokeOnGet(entityID);
_componentResetHandler.Reset(ref _items[itemIndex]);
return ref _items[itemIndex]; return ref _items[itemIndex];
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -146,7 +149,7 @@ namespace DCFApixels.DragonECS
_mapping = new int[world.Capacity]; _mapping = new int[world.Capacity];
_recycledItems = new int[world.Config.Get_PoolRecycledComponentsCapacity()]; _recycledItems = new int[world.Config.Get_PoolRecycledComponentsCapacity()];
_recycledItemsCount = 0; _recycledItemsCount = 0;
_items = new T[world.Config.Get_PoolComponentsCapacity()]; _items = new T[ArrayUtility.NormalizeSizeToPowerOfTwo(world.Config.Get_PoolComponentsCapacity())];
_itemsCount = 0; _itemsCount = 0;
} }
void IEcsPoolImplementation.OnWorldResize(int newSize) void IEcsPoolImplementation.OnWorldResize(int newSize)
@ -157,7 +160,9 @@ namespace DCFApixels.DragonECS
void IEcsPoolImplementation.OnReleaseDelEntityBuffer(ReadOnlySpan<int> buffer) void IEcsPoolImplementation.OnReleaseDelEntityBuffer(ReadOnlySpan<int> buffer)
{ {
foreach (var entityID in buffer) foreach (var entityID in buffer)
{
TryDel(entityID); TryDel(entityID);
}
} }
#endregion #endregion

View File

@ -5,18 +5,61 @@ using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace DCFApixels.DragonECS.Utils namespace DCFApixels.DragonECS.Internal
{ {
internal static class ArrayUtility internal static class ArrayUtility
{ {
private static int GetHighBitNumber(uint bits)
{
if (bits == 0)
{
return -1;
}
int bit = 0;
if ((bits & 0xFFFF0000) != 0)
{
bits >>= 16;
bit |= 16;
}
if ((bits & 0xFF00) != 0)
{
bits >>= 8;
bit |= 8;
}
if ((bits & 0xF0) != 0)
{
bits >>= 4;
bit |= 4;
}
if ((bits & 0xC) != 0)
{
bits >>= 2;
bit |= 2;
}
if ((bits & 0x2) != 0)
{
bit |= 1;
}
return bit;
}
public static int NormalizeSizeToPowerOfTwo(int minSize)
{
return 1 << (GetHighBitNumber((uint)minSize - 1u) + 1);
}
public static void Fill<T>(T[] array, T value, int startIndex = 0, int length = -1) public static void Fill<T>(T[] array, T value, int startIndex = 0, int length = -1)
{ {
if (length < 0) if (length < 0)
{
length = array.Length; length = array.Length;
}
else else
{
length = startIndex + length; length = startIndex + length;
}
for (int i = startIndex; i < length; i++) for (int i = startIndex; i < length; i++)
{
array[i] = value; array[i] = value;
}
} }
} }
internal readonly struct EnumerableInt : IEnumerable<int> internal readonly struct EnumerableInt : IEnumerable<int>
@ -113,7 +156,9 @@ namespace DCFApixels.DragonECS.Utils
{ {
T* clone = New<T>(length); T* clone = New<T>(length);
for (int i = 0; i < length; i++) for (int i = 0; i < length; i++)
{
clone[i] = sourcePtr[i]; clone[i] = sourcePtr[i];
}
return clone; return clone;
} }