mirror of
https://github.com/DCFApixels/DragonECS.git
synced 2025-09-18 18:14:37 +08:00
update EcsWorld
This commit is contained in:
parent
7c5f799d8e
commit
3d2ae2ff31
@ -10,18 +10,20 @@ namespace DCFApixels.DragonECS
|
|||||||
public readonly short id;
|
public readonly short id;
|
||||||
private IEcsWorldConfig _config;
|
private IEcsWorldConfig _config;
|
||||||
|
|
||||||
private bool _isDestroyed;
|
private bool _isDestroyed = false;
|
||||||
|
|
||||||
private IdDispenser _entityDispenser;
|
private IdDispenser _entityDispenser;
|
||||||
private int _entitiesCount;
|
private int _entitiesCount = 0;
|
||||||
private int _entitesCapacity;
|
private int _entitiesCapacity = 0;
|
||||||
private short[] _gens; //старший бит указывает на то жива ли сущность
|
private short[] _gens = Array.Empty<short>(); //старший бит указывает на то жива ли сущность
|
||||||
private short[] _componentCounts;
|
private short[] _componentCounts = Array.Empty<short>();
|
||||||
|
|
||||||
private int[] _delEntBuffer;
|
private int[] _delEntBuffer = Array.Empty<int>();
|
||||||
private int _delEntBufferCount;
|
private int _delEntBufferCount = 0;
|
||||||
private bool _isEnableAutoReleaseDelEntBuffer = true;
|
private bool _isEnableAutoReleaseDelEntBuffer = true;
|
||||||
|
|
||||||
|
internal int[][] _entitiesComponentMasks = Array.Empty<int[]>();
|
||||||
|
|
||||||
private long _version = 0;
|
private long _version = 0;
|
||||||
|
|
||||||
private List<WeakReference<EcsGroup>> _groups = new List<WeakReference<EcsGroup>>();
|
private List<WeakReference<EcsGroup>> _groups = new List<WeakReference<EcsGroup>>();
|
||||||
@ -30,10 +32,6 @@ namespace DCFApixels.DragonECS
|
|||||||
private List<IEcsWorldEventListener> _listeners = new List<IEcsWorldEventListener>();
|
private List<IEcsWorldEventListener> _listeners = new List<IEcsWorldEventListener>();
|
||||||
private List<IEcsEntityEventListener> _entityListeners = new List<IEcsEntityEventListener>();
|
private List<IEcsEntityEventListener> _entityListeners = new List<IEcsEntityEventListener>();
|
||||||
|
|
||||||
internal int[][] _entitiesComponentMasks;
|
|
||||||
|
|
||||||
private readonly PoolsMediator _poolsMediator;
|
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
public IEcsWorldConfig Config
|
public IEcsWorldConfig Config
|
||||||
{
|
{
|
||||||
@ -58,7 +56,7 @@ namespace DCFApixels.DragonECS
|
|||||||
public int Capacity
|
public int Capacity
|
||||||
{
|
{
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
get { return _entitesCapacity; }
|
get { return _entitiesCapacity; }
|
||||||
}
|
}
|
||||||
public int DelEntBufferCount
|
public int DelEntBufferCount
|
||||||
{
|
{
|
||||||
@ -125,21 +123,8 @@ namespace DCFApixels.DragonECS
|
|||||||
_poolComponentCounts = new int[poolsCapacity];
|
_poolComponentCounts = new int[poolsCapacity];
|
||||||
ArrayUtility.Fill(_pools, _nullPool);
|
ArrayUtility.Fill(_pools, _nullPool);
|
||||||
|
|
||||||
_entitesCapacity = ArrayUtility.NormalizeSizeToPowerOfTwo(config.Get_EntitiesCapacity());
|
int entitiesCapacity = ArrayUtility.NormalizeSizeToPowerOfTwo(config.Get_EntitiesCapacity());
|
||||||
_entityDispenser = new IdDispenser(_entitesCapacity, 0);
|
_entityDispenser = new IdDispenser(entitiesCapacity, 0, OnEntityDispenserResized);
|
||||||
_gens = new short[_entitesCapacity];
|
|
||||||
_componentCounts = new short[_entitesCapacity];
|
|
||||||
|
|
||||||
ArrayUtility.Fill(_gens, DEATH_GEN_BIT);
|
|
||||||
_delEntBufferCount = 0;
|
|
||||||
_delEntBuffer = new int[_entitesCapacity];
|
|
||||||
_entitiesComponentMasks = new int[_entitesCapacity][];
|
|
||||||
|
|
||||||
int maskLength = _pools.Length / 32 + 1;
|
|
||||||
for (int i = 0; i < _entitesCapacity; i++)
|
|
||||||
{
|
|
||||||
_entitiesComponentMasks[i] = new int[maskLength];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
public void Destroy()
|
public void Destroy()
|
||||||
{
|
{
|
||||||
@ -200,37 +185,28 @@ namespace DCFApixels.DragonECS
|
|||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public int NewEntity()
|
public int NewEntity()
|
||||||
{
|
{
|
||||||
unchecked { _version++; }
|
|
||||||
int entityID = _entityDispenser.UseFree();
|
int entityID = _entityDispenser.UseFree();
|
||||||
_entitiesCount++;
|
CreateConcreteEntity(entityID);
|
||||||
|
|
||||||
if (_entitesCapacity <= entityID)
|
|
||||||
{
|
|
||||||
Upsize_Internal(_gens.Length << 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
_gens[entityID] &= GEN_MASK;
|
|
||||||
_entityListeners.InvokeOnNewEntity(entityID);
|
|
||||||
return entityID;
|
return entityID;
|
||||||
}
|
}
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void NewEntity(int entityID)
|
public void NewEntity(int entityID)
|
||||||
{
|
{
|
||||||
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
||||||
if (IsUsed(entityID))
|
if (IsUsed(entityID)) { Throw.World_EntityIsAlreadyСontained(entityID); }
|
||||||
{
|
|
||||||
Throw.World_EntityIsAlreadyСontained(entityID);
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
unchecked { _version++; }
|
|
||||||
_entityDispenser.Use(entityID);
|
_entityDispenser.Use(entityID);
|
||||||
_entitiesCount++;
|
CreateConcreteEntity(entityID);
|
||||||
|
}
|
||||||
if (_entitesCapacity <= entityID)
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
{
|
private void CreateConcreteEntity(int entityID)
|
||||||
Upsize_Internal(_gens.Length << 1);
|
{
|
||||||
|
unchecked { _version++; }
|
||||||
|
_entitiesCount++;
|
||||||
|
if (_entitiesCapacity <= entityID)
|
||||||
|
{
|
||||||
|
OnEntityDispenserResized(_gens.Length << 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
_gens[entityID] &= GEN_MASK;
|
_gens[entityID] &= GEN_MASK;
|
||||||
_entityListeners.InvokeOnNewEntity(entityID);
|
_entityListeners.InvokeOnNewEntity(entityID);
|
||||||
}
|
}
|
||||||
@ -423,20 +399,24 @@ namespace DCFApixels.DragonECS
|
|||||||
#region Upsize
|
#region Upsize
|
||||||
public void Upsize(int minSize)
|
public void Upsize(int minSize)
|
||||||
{
|
{
|
||||||
Upsize_Internal(ArrayUtility.NormalizeSizeToPowerOfTwo(minSize));
|
_entityDispenser.UpSize(minSize);
|
||||||
}
|
}
|
||||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||||
private void Upsize_Internal(int newSize)
|
private void OnEntityDispenserResized(int newSize)
|
||||||
{
|
{
|
||||||
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);
|
||||||
Array.Resize(ref _entitiesComponentMasks, newSize);
|
Array.Resize(ref _entitiesComponentMasks, newSize);
|
||||||
for (int i = _entitesCapacity; i < newSize; i++)
|
|
||||||
_entitiesComponentMasks[i] = new int[_pools.Length / 32 + 1];
|
|
||||||
|
|
||||||
ArrayUtility.Fill(_gens, DEATH_GEN_BIT, _entitesCapacity);
|
ArrayUtility.Fill(_gens, DEATH_GEN_BIT, _entitiesCapacity);
|
||||||
_entitesCapacity = newSize;
|
int maskLength = _pools.Length / 32 + 1;
|
||||||
|
for (int i = _entitiesCapacity; i < newSize; i++)
|
||||||
|
{
|
||||||
|
_entitiesComponentMasks[i] = new int[maskLength];
|
||||||
|
}
|
||||||
|
|
||||||
|
_entitiesCapacity = newSize;
|
||||||
|
|
||||||
for (int i = 0; i < _groups.Count; i++)
|
for (int i = 0; i < _groups.Count; i++)
|
||||||
{
|
{
|
||||||
|
@ -12,6 +12,8 @@ namespace DCFApixels.DragonECS
|
|||||||
internal IEcsPoolImplementation[] _pools;
|
internal IEcsPoolImplementation[] _pools;
|
||||||
internal int[] _poolComponentCounts;
|
internal int[] _poolComponentCounts;
|
||||||
|
|
||||||
|
private readonly PoolsMediator _poolsMediator;
|
||||||
|
|
||||||
private static EcsNullPool _nullPool = EcsNullPool.instance;
|
private static EcsNullPool _nullPool = EcsNullPool.instance;
|
||||||
|
|
||||||
#region Getters
|
#region Getters
|
||||||
@ -134,7 +136,7 @@ namespace DCFApixels.DragonECS
|
|||||||
Array.Resize(ref _poolComponentCounts, _pools.Length);
|
Array.Resize(ref _poolComponentCounts, _pools.Length);
|
||||||
ArrayUtility.Fill(_pools, _nullPool, oldCapacity, oldCapacity - _pools.Length);
|
ArrayUtility.Fill(_pools, _nullPool, oldCapacity, oldCapacity - _pools.Length);
|
||||||
|
|
||||||
for (int i = 0; i < _entitesCapacity; i++)
|
for (int i = 0; i < _entitiesCapacity; i++)
|
||||||
{
|
{
|
||||||
Array.Resize(ref _entitiesComponentMasks[i], _pools.Length / 32 + 1);
|
Array.Resize(ref _entitiesComponentMasks[i], _pools.Length / 32 + 1);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user