refctoring

This commit is contained in:
Mikhail 2024-02-11 22:10:05 +08:00
parent 48e8ca930f
commit c7b8fd8d57
2 changed files with 23 additions and 22 deletions

View File

@ -22,8 +22,6 @@ namespace DCFApixels.DragonECS
private int[] _delEntBuffer; private int[] _delEntBuffer;
private int _delEntBufferCount; private int _delEntBufferCount;
private int _delEntBufferMinCount;
private int _freeSpace;
private bool _isEnableAutoReleaseDelEntBuffer = true; private bool _isEnableAutoReleaseDelEntBuffer = true;
private long _version = 0; private long _version = 0;
@ -138,8 +136,6 @@ namespace DCFApixels.DragonECS
_entitiesComponentMasks[i] = new int[maskLength]; _entitiesComponentMasks[i] = new int[maskLength];
} }
_delEntBufferMinCount = Math.Max(_delEntBuffer.Length >> DEL_ENT_BUFFER_SIZE_OFFSET, DEL_ENT_BUFFER_MIN_SIZE);
_allEntites = GetFreeGroup(); _allEntites = GetFreeGroup();
} }
public void Destroy() public void Destroy()
@ -155,6 +151,7 @@ namespace DCFApixels.DragonECS
_poolIds = null; _poolIds = null;
_componentIds = null; _componentIds = null;
} }
//public void Clear() { }
#endregion #endregion
#region Getters #region Getters
@ -194,10 +191,6 @@ namespace DCFApixels.DragonECS
} }
#endregion #endregion
#region Where Query
#endregion
#region Entity #region Entity
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public EcsSpan ToSpan() public EcsSpan ToSpan()
@ -209,7 +202,6 @@ namespace DCFApixels.DragonECS
{ {
unchecked { _version++; } unchecked { _version++; }
int entityID = _entityDispenser.GetFree(); int entityID = _entityDispenser.GetFree();
_freeSpace--;
_entitiesCount++; _entitiesCount++;
if (_gens.Length <= entityID) if (_gens.Length <= entityID)
@ -236,21 +228,20 @@ namespace DCFApixels.DragonECS
DelEntity(entityID); DelEntity(entityID);
} }
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void DelEntity(int entityID) public void DelEntity(int entityID)
{ {
#if DEBUG #if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
if (IsUsed(entityID) == false) if (IsUsed(entityID) == false)
{ {
Throw.UndefinedException(); Throw.World_EntityIsNotContained(entityID);
} }
#endif #endif
_allEntites.Remove(entityID); _allEntites.RemoveUnchecked(entityID);
_delEntBuffer[_delEntBufferCount++] = entityID; _delEntBuffer[_delEntBufferCount++] = entityID;
_gens[entityID] |= DEATH_GEN_BIT; _gens[entityID] |= DEATH_GEN_BIT;
_entitiesCount--; _entitiesCount--;
_entityListeners.InvokeOnDelEntity(entityID); _entityListeners.InvokeOnDelEntity(entityID);
//if (_delEntBufferCount >= _delEntBuffer.Length)
// ReleaseDelEntityBufferAll();
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe entlong GetEntityLong(int entityID) public unsafe entlong GetEntityLong(int entityID)
@ -259,19 +250,19 @@ namespace DCFApixels.DragonECS
return *(entlong*)&x; return *(entlong*)&x;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsAlive(int entityID, short gen) => _gens[entityID] == gen; public bool IsAlive(int entityID, short gen) { return _gens[entityID] == gen; }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsUsed(int entityID) => _gens[entityID] >= 0; public bool IsUsed(int entityID) { return _gens[entityID] >= 0; }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public short GetGen(int entityID) => _gens[entityID]; public short GetGen(int entityID) { return _gens[entityID]; }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public short GetComponentsCount(int entityID) => _componentCounts[entityID]; public short GetComponentsCount(int entityID) { return _componentCounts[entityID]; }
public bool IsMatchesMask(EcsMask mask, int entityID) public bool IsMatchesMask(EcsMask mask, int entityID)
{ {
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS #if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
if (mask.worldID != id) if (mask.worldID != id) { Throw.World_MaskDoesntBelongWorld(); }
throw new EcsFrameworkException("The types of the target world of the mask and this world are different.");
#endif #endif
for (int i = 0, iMax = mask.incChunckMasks.Length; i < iMax; i++) for (int i = 0, iMax = mask.incChunckMasks.Length; i < iMax; i++)
{ {
@ -388,7 +379,6 @@ namespace DCFApixels.DragonECS
unchecked { _gens[e]++; }//up gen unchecked { _gens[e]++; }//up gen
_gens[e] |= DEATH_GEN_BIT; _gens[e] |= DEATH_GEN_BIT;
} }
_freeSpace += count;
} }
#endregion #endregion
@ -407,7 +397,6 @@ namespace DCFApixels.DragonECS
for (int i = _entitesCapacity; i < newSize; i++) for (int i = _entitesCapacity; i < newSize; i++)
_entitiesComponentMasks[i] = new int[_pools.Length / 32 + 1]; _entitiesComponentMasks[i] = new int[_pools.Length / 32 + 1];
_delEntBufferMinCount = Math.Max(_delEntBuffer.Length >> DEL_ENT_BUFFER_SIZE_OFFSET, DEL_ENT_BUFFER_MIN_SIZE);
ArrayUtility.Fill(_gens, DEATH_GEN_BIT, _entitesCapacity); ArrayUtility.Fill(_gens, DEATH_GEN_BIT, _entitesCapacity);
_entitesCapacity = newSize; _entitesCapacity = newSize;

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using static Leopotam.EcsLite.EcsWorld;
namespace DCFApixels.DragonECS.Internal namespace DCFApixels.DragonECS.Internal
@ -72,6 +73,17 @@ namespace DCFApixels.DragonECS.Internal
{ {
throw new MethodAccessException("The Group does not belong in this world."); throw new MethodAccessException("The Group does not belong in this world.");
} }
[MethodImpl(MethodImplOptions.NoInlining)]
public static void World_MaskDoesntBelongWorld()
{
throw new EcsFrameworkException($"The mask doesn't belong in this world");
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static void World_EntityIsNotContained(int entityID)
{
throw new EcsFrameworkException($"An entity with identifier {entityID} is not contained in this world");
}
[MethodImpl(MethodImplOptions.NoInlining)] [MethodImpl(MethodImplOptions.NoInlining)]
internal static void Ent_ThrowIsNotAlive(entlong entity) internal static void Ent_ThrowIsNotAlive(entlong entity)