DragonECS/src/EcsWorld.cs

883 lines
30 KiB
C#
Raw Normal View History

using DCFApixels.DragonECS.Internal;
using System;
2023-03-02 14:42:44 +08:00
using System.Collections.Generic;
2024-04-09 00:19:43 +08:00
using System.Diagnostics;
2023-03-02 14:42:44 +08:00
using System.Runtime.CompilerServices;
2024-02-29 22:42:33 +08:00
using System.Runtime.InteropServices;
2023-03-02 14:42:44 +08:00
namespace DCFApixels.DragonECS
{
2024-04-28 19:43:10 +08:00
#if ENABLE_IL2CPP
using Unity.IL2CPP.CompilerServices;
#endif
2024-03-07 07:48:18 +08:00
public class EcsWorldConfig
{
public static readonly EcsWorldConfig Default = new EcsWorldConfig();
public readonly int EntitiesCapacity;
public readonly int GroupCapacity;
public readonly int PoolsCapacity;
public readonly int PoolComponentsCapacity;
public readonly int PoolRecycledComponentsCapacity;
public EcsWorldConfig(int entitiesCapacity = 512, int groupCapacity = 512, int poolsCapacity = 512, int poolComponentsCapacity = 512, int poolRecycledComponentsCapacity = 512 / 2)
{
EntitiesCapacity = entitiesCapacity;
GroupCapacity = groupCapacity;
PoolsCapacity = poolsCapacity;
PoolComponentsCapacity = poolComponentsCapacity;
PoolRecycledComponentsCapacity = poolRecycledComponentsCapacity;
}
}
2024-04-28 19:43:10 +08:00
#if ENABLE_IL2CPP
[Il2CppSetOption(Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif
2024-04-09 00:19:43 +08:00
[DebuggerTypeProxy(typeof(DebuggerProxy))]
public partial class EcsWorld : IEntityStorage
2023-06-21 01:37:05 +08:00
{
public readonly short id;
2024-03-07 07:48:18 +08:00
private IConfigContainer _configs;
2024-02-15 18:11:24 +08:00
private bool _isDestroyed = false;
2024-02-13 21:00:32 +08:00
private IdDispenser _entityDispenser;
2024-02-15 18:11:24 +08:00
private int _entitiesCount = 0;
private int _entitiesCapacity = 0;
2024-02-29 22:42:33 +08:00
private EntitySlot[] _entities = Array.Empty<EntitySlot>();
2023-04-20 11:37:27 +08:00
2024-02-15 18:11:24 +08:00
private int[] _delEntBuffer = Array.Empty<int>();
private int _delEntBufferCount = 0;
private bool _isEnableAutoReleaseDelEntBuffer = true;
2023-04-18 19:35:42 +08:00
2024-02-16 21:17:20 +08:00
internal int _entityComponentMaskLength;
2024-04-18 00:36:05 +08:00
internal int _entityComponentMaskLengthBitShift;
2024-02-16 21:17:20 +08:00
internal int[] _entityComponentMasks = Array.Empty<int>();
2024-03-03 04:49:35 +08:00
private const int COMPONENT_MASK_CHUNK_SIZE = 32;
2024-02-15 18:11:24 +08:00
//"лениво" обновляется только для NewEntity
private long _deleteLeakedEntitesLastVersion = 0;
//обновляется в NewEntity и в DelEntity
private long _version = 0;
2023-06-18 00:39:25 +08:00
private List<WeakReference<EcsGroup>> _groups = new List<WeakReference<EcsGroup>>();
2023-04-18 19:35:42 +08:00
private Stack<EcsGroup> _groupsPool = new Stack<EcsGroup>(64);
2023-04-01 20:45:37 +08:00
2023-06-18 00:39:25 +08:00
private List<IEcsWorldEventListener> _listeners = new List<IEcsWorldEventListener>();
private List<IEcsEntityEventListener> _entityListeners = new List<IEcsEntityEventListener>();
2023-04-01 20:45:37 +08:00
2023-03-02 14:42:44 +08:00
#region Properties
2024-03-07 07:48:18 +08:00
public IConfigContainer Configs
2024-02-03 01:12:53 +08:00
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-07 06:46:44 +08:00
get { return _configs; }
2024-02-03 01:12:53 +08:00
}
public long Version
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _version; }
}
public bool IsDestroyed
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _isDestroyed; }
}
public int Count
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _entitiesCount; }
}
public int Capacity
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-02-15 18:11:24 +08:00
get { return _entitiesCapacity; }
}
public int DelEntBufferCount
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _delEntBufferCount; }
}
public bool IsEnableReleaseDelEntBuffer
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _isEnableAutoReleaseDelEntBuffer; }
}
2023-11-22 16:08:49 +08:00
2024-02-13 21:00:32 +08:00
public EcsSpan Entities
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (_isEnableAutoReleaseDelEntBuffer)
{
ReleaseDelEntityBufferAll();
}
2024-02-13 21:00:32 +08:00
return _entityDispenser.UsedToEcsSpan(id);
}
}
2024-03-07 21:24:16 +08:00
public int PoolsCount
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _poolsCount; }
}
public ReadOnlySpan<IEcsPool> AllPools
{
// new ReadOnlySpan<IEcsPoolImplementation>(pools, 0, _poolsCount);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _pools; }
}
2023-04-23 22:55:13 +08:00
#endregion
2023-04-20 20:10:16 +08:00
#region Constructors/Destroy
2024-03-07 07:48:18 +08:00
public EcsWorld(EcsWorldConfig config, short worldID = -1) : this(config == null ? ConfigContainer.Empty : new ConfigContainer().Set(config), worldID) { }
public EcsWorld(IConfigContainer configs = null, short worldID = -1)
{
2024-03-07 07:48:18 +08:00
if (configs == null) { configs = ConfigContainer.Empty; }
2024-03-07 03:17:51 +08:00
bool nullWorld = this is NullWorld;
2024-03-07 21:24:16 +08:00
if (nullWorld == false && worldID == NULL_WORLD_ID)
2024-03-07 03:17:51 +08:00
{
EcsDebug.PrintWarning($"The world identifier cannot be {NULL_WORLD_ID}");
}
2024-03-07 06:46:44 +08:00
_configs = configs;
2024-03-07 07:48:18 +08:00
EcsWorldConfig config = configs.GetWorldConfigOrDefault();
2023-04-23 17:19:52 +08:00
2024-03-07 03:17:51 +08:00
if (worldID < 0 || (worldID == NULL_WORLD_ID && nullWorld == false))
{
2024-02-15 00:53:20 +08:00
worldID = (short)_worldIdDispenser.UseFree();
}
2024-02-15 00:53:20 +08:00
else
{
2024-02-15 20:28:38 +08:00
if (worldID != _worldIdDispenser.NullID)
{
_worldIdDispenser.Use(worldID);
}
if (_worlds[worldID] != null)
2024-02-15 00:53:20 +08:00
{
2024-02-15 20:28:38 +08:00
_worldIdDispenser.Release(worldID);
2024-04-22 17:49:24 +08:00
Throw.Exception("The world with the specified ID has already been created\r\n");
2024-02-15 00:53:20 +08:00
}
}
2024-02-15 01:04:54 +08:00
id = worldID;
2024-02-15 20:28:38 +08:00
_worlds[worldID] = this;
2024-02-03 01:12:53 +08:00
_poolsMediator = new PoolsMediator(this);
2024-03-07 06:46:44 +08:00
int poolsCapacity = ArrayUtility.NormalizeSizeToPowerOfTwo(config.PoolsCapacity);
2024-02-03 01:12:53 +08:00
_pools = new IEcsPoolImplementation[poolsCapacity];
_poolComponentCounts = new int[poolsCapacity];
2023-05-26 00:24:38 +08:00
ArrayUtility.Fill(_pools, _nullPool);
2023-04-01 20:45:37 +08:00
2024-03-07 06:46:44 +08:00
int entitiesCapacity = ArrayUtility.NormalizeSizeToPowerOfTwo(config.EntitiesCapacity);
2024-02-15 18:11:24 +08:00
_entityDispenser = new IdDispenser(entitiesCapacity, 0, OnEntityDispenserResized);
2024-03-13 17:41:33 +08:00
GetComponentTypeID<NullComponent>();
2023-03-02 14:42:44 +08:00
}
2023-04-20 20:10:16 +08:00
public void Destroy()
{
2024-03-03 01:25:45 +08:00
if (_isDestroyed)
{
2024-03-10 21:38:43 +08:00
EcsDebug.PrintWarning("The world is already destroyed");
2024-03-03 01:25:45 +08:00
return;
}
2024-03-11 06:48:43 +08:00
if (id == NULL_WORLD_ID)
2024-03-10 21:38:43 +08:00
{
2024-03-10 21:47:28 +08:00
#if (DEBUG && !DISABLE_DEBUG)
2024-03-10 21:38:43 +08:00
Throw.World_WorldCantBeDestroyed();
2024-03-10 21:47:28 +08:00
#endif
return;
2024-03-10 21:38:43 +08:00
}
2024-03-10 04:36:58 +08:00
_listeners.InvokeOnWorldDestroy();
2023-04-20 20:10:16 +08:00
_entityDispenser = null;
2023-05-26 00:24:38 +08:00
_pools = null;
2023-04-20 20:10:16 +08:00
_nullPool = null;
2024-02-15 20:28:38 +08:00
_worlds[id] = null;
2023-06-21 15:03:33 +08:00
ReleaseData(id);
_worldIdDispenser.Release(id);
_isDestroyed = true;
2024-02-14 21:13:00 +08:00
_poolTypeCode_2_CmpTypeIDs = null;
2024-02-24 03:02:41 +08:00
_cmpTypeCode_2_CmpTypeIDs = null;
2024-03-10 04:36:58 +08:00
//_entities - не обнуляется для работы entlong.IsAlive
}
2024-02-11 22:10:05 +08:00
//public void Clear() { }
2023-03-02 14:42:44 +08:00
#endregion
2023-05-30 04:32:09 +08:00
#region Getters
2023-05-28 05:53:08 +08:00
#if UNITY_2020_3_OR_NEWER
[UnityEngine.Scripting.Preserve]
#endif
2023-07-02 16:45:40 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-06-22 14:31:13 +08:00
public TAspect GetAspect<TAspect>() where TAspect : EcsAspect
{
2023-06-27 05:30:45 +08:00
return Get<AspectCache<TAspect>>().instance;
}
2023-06-27 05:30:45 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-05-27 15:59:46 +08:00
public TExecutor GetExecutor<TExecutor>() where TExecutor : EcsQueryExecutor, new()
{
2023-06-27 05:30:45 +08:00
return Get<ExcecutorCache<TExecutor>>().instance;
2023-05-30 15:20:27 +08:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref T Get<T>() where T : struct
{
return ref WorldComponentPool<T>.GetForWorld(id);
}
2023-07-02 16:17:13 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref T GetUnchecked<T>() where T : struct
{
return ref WorldComponentPool<T>.GetForWorldUnchecked(id);
}
2023-07-03 02:44:35 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T Get<T>(int worldID) where T : struct
{
return ref WorldComponentPool<T>.GetForWorld(worldID);
}
2023-07-03 02:44:35 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T GetUnchecked<T>(int worldID) where T : struct
{
return ref WorldComponentPool<T>.GetForWorldUnchecked(worldID);
}
2023-05-30 04:32:09 +08:00
#endregion
2023-03-26 11:19:03 +08:00
#region Entity
2024-02-14 03:04:05 +08:00
#region New/Del
2024-02-10 20:54:09 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-12-20 19:06:17 +08:00
public int NewEntity()
2023-03-02 14:42:44 +08:00
{
2024-02-13 21:00:32 +08:00
int entityID = _entityDispenser.UseFree();
2024-02-15 18:11:24 +08:00
CreateConcreteEntity(entityID);
return entityID;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-04-16 12:46:09 +08:00
public int NewEntity(int entityID)
{
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
2024-02-15 18:11:24 +08:00
if (IsUsed(entityID)) { Throw.World_EntityIsAlreadyСontained(entityID); }
#endif
_entityDispenser.Use(entityID);
2024-02-15 18:11:24 +08:00
CreateConcreteEntity(entityID);
2024-04-16 12:46:09 +08:00
return entityID;
2024-02-15 18:11:24 +08:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void CreateConcreteEntity(int entityID)
{
2024-02-26 10:43:37 +08:00
UpVersionLeaked();
_entitiesCount++;
2024-04-16 12:46:09 +08:00
ref var slot = ref _entities[entityID];
slot.isUsed = true;
2024-04-28 18:36:24 +08:00
if (slot.gen >= 0)
2024-04-16 12:46:09 +08:00
{ //если gen был пробужен у не мертвой сущности, то для отличия от мертвой, нужно инкрементировать и усыпить
slot.gen++;
slot.gen &= SLEEP_GEN_MASK;
}
_entityListeners.InvokeOnNewEntity(entityID);
}
2023-12-20 19:06:17 +08:00
public entlong NewEntityLong()
{
2024-04-16 12:46:09 +08:00
int entityID = NewEntity();
return GetEntityLong(entityID);
}
public entlong NewEntityLong(int entityID)
{
NewEntity(entityID);
return GetEntityLong(entityID);
2023-03-26 11:19:03 +08:00
}
2024-02-07 22:16:41 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void TryDelEntity(int entityID)
{
if (IsUsed(entityID))
{
DelEntity(entityID);
}
}
2024-02-11 22:10:05 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-04-16 12:46:09 +08:00
public void DelEntity(entlong entity)
{
DelEntity(entity.ID);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void DelEntity(int entityID)
2023-03-26 11:19:03 +08:00
{
2024-02-11 22:10:05 +08:00
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
2024-02-26 10:43:37 +08:00
if (IsUsed(entityID) == false) { Throw.World_EntityIsNotContained(entityID); }
2024-02-07 22:16:41 +08:00
#endif
2024-02-26 10:43:37 +08:00
UpVersion();
_delEntBuffer[_delEntBufferCount++] = entityID;
2024-02-29 22:42:33 +08:00
_entities[entityID].isUsed = false;
_entitiesCount--;
2023-06-10 18:58:43 +08:00
_entityListeners.InvokeOnDelEntity(entityID);
2023-04-20 11:37:27 +08:00
}
#endregion
#region Other
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public EcsSpan ToSpan()
{
2024-02-29 22:42:33 +08:00
if (_isEnableAutoReleaseDelEntBuffer)
{
ReleaseDelEntityBufferAll();
}
return _entityDispenser.UsedToEcsSpan(id);
}
2023-04-24 16:48:18 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-07-02 21:39:44 +08:00
public unsafe entlong GetEntityLong(int entityID)
{
2024-02-29 03:28:13 +08:00
long x = (long)id << 48 | (long)GetGen(entityID) << 32 | (long)entityID;
2023-07-02 21:39:44 +08:00
return *(entlong*)&x;
}
2023-04-24 16:48:18 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-02-29 03:28:13 +08:00
public unsafe EntitySlotInfo GetEntitySlotInfoDebug(int entityID)
{
2024-02-29 22:42:33 +08:00
return new EntitySlotInfo(entityID, _entities[entityID].gen, id);
2024-02-29 03:28:13 +08:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-02-16 21:17:20 +08:00
public bool IsAlive(int entityID, short gen)
{
2024-02-29 22:42:33 +08:00
ref var slot = ref _entities[entityID];
return slot.gen == gen && slot.isUsed;
2024-02-16 21:17:20 +08:00
}
2023-04-24 16:48:18 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-02-16 21:17:20 +08:00
public bool IsUsed(int entityID)
{
2024-02-29 22:42:33 +08:00
return _entities[entityID].isUsed;
2024-02-16 21:17:20 +08:00
}
2023-05-30 04:32:09 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-02-16 21:17:20 +08:00
public short GetGen(int entityID)
{
2024-02-29 03:28:13 +08:00
unchecked
{
2024-04-16 12:46:09 +08:00
ref var slotGen = ref _entities[entityID].gen;
2024-02-29 03:28:13 +08:00
if (slotGen < 0)
2024-04-16 12:46:09 +08:00
{ //если gen меньше 0 значит он спящий, спящие нужно инкремировать
2024-02-29 03:28:13 +08:00
slotGen++;
2024-04-16 12:46:09 +08:00
slotGen &= WAKE_UP_GEN_MASK;
2024-02-29 03:28:13 +08:00
}
return slotGen;
}
2024-02-16 21:17:20 +08:00
}
2023-05-30 04:32:09 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-02-16 21:17:20 +08:00
public short GetComponentsCount(int entityID)
{
2024-02-29 22:42:33 +08:00
return _entities[entityID].componentsCount;
2024-02-16 21:17:20 +08:00
}
2023-05-30 04:32:09 +08:00
public bool IsMatchesMask(EcsMask mask, int entityID)
{
2023-11-22 20:28:11 +08:00
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
2024-04-22 17:20:31 +08:00
if (mask._worldID != id) { Throw.World_MaskDoesntBelongWorld(); }
2023-11-22 20:28:11 +08:00
#endif
2024-04-22 17:20:31 +08:00
for (int i = 0, iMax = mask._incChunckMasks.Length; i < iMax; i++)
2023-11-22 20:28:11 +08:00
{
2024-04-22 17:20:31 +08:00
if (!_pools[mask._inc[i]].Has(entityID))
2024-02-26 10:43:37 +08:00
{
2023-11-22 20:28:11 +08:00
return false;
2024-02-26 10:43:37 +08:00
}
2023-11-22 20:28:11 +08:00
}
2024-04-22 17:20:31 +08:00
for (int i = 0, iMax = mask._excChunckMasks.Length; i < iMax; i++)
2023-11-22 20:28:11 +08:00
{
2024-04-22 17:20:31 +08:00
if (_pools[mask._exc[i]].Has(entityID))
2024-02-26 10:43:37 +08:00
{
2023-11-22 20:28:11 +08:00
return false;
2024-02-26 10:43:37 +08:00
}
2023-11-22 20:28:11 +08:00
}
return true;
2023-05-30 04:32:09 +08:00
}
2024-02-29 03:28:13 +08:00
#endregion
2024-02-29 03:28:13 +08:00
#region Leaked
public bool DeleteLeakedEntites()
{
if (_deleteLeakedEntitesLastVersion == _version)
{
return false;
}
int delCount = 0;
2024-02-13 21:00:32 +08:00
foreach (var e in Entities)
{
2024-03-08 20:25:42 +08:00
ref var ent = ref _entities[e];
if (ent.componentsCount <= 0 && ent.isUsed)
{
DelEntity(e);
delCount++;
}
}
2024-02-22 23:48:10 +08:00
if (delCount > 0)
{
EcsDebug.PrintWarning($"Detected and deleted {delCount} leaking entities.");
}
_deleteLeakedEntitesLastVersion = _version;
return delCount > 0;
}
2024-02-29 03:28:13 +08:00
public int CountLeakedEntitesDebug()
{
if (_deleteLeakedEntitesLastVersion == _version)
{
return 0;
}
int delCount = 0;
foreach (var e in Entities)
{
2024-03-08 20:34:58 +08:00
ref var ent = ref _entities[e];
if (ent.componentsCount <= 0 && ent.isUsed)
2024-02-29 03:28:13 +08:00
{
delCount++;
}
}
return delCount;
}
#endregion
2023-05-30 04:32:09 +08:00
#region Copy/Clone
2023-05-23 01:47:28 +08:00
public void CopyEntity(int fromEntityID, int toEntityID)
{
2023-05-26 00:24:38 +08:00
foreach (var pool in _pools)
2023-05-23 01:47:28 +08:00
{
2023-12-20 23:21:10 +08:00
if (pool.Has(fromEntityID))
2024-02-26 10:43:37 +08:00
{
pool.Copy(fromEntityID, toEntityID);
2024-02-26 10:43:37 +08:00
}
}
}
public void CopyEntity(int fromEntityID, EcsWorld toWorld, int toEntityID)
{
foreach (var pool in _pools)
{
if (pool.Has(fromEntityID))
2024-02-26 10:43:37 +08:00
{
pool.Copy(fromEntityID, toWorld, toEntityID);
2024-02-26 10:43:37 +08:00
}
2023-05-23 01:47:28 +08:00
}
}
public int CloneEntity(int fromEntityID)
{
2023-12-20 19:06:17 +08:00
int newEntity = NewEntity();
2023-05-23 01:47:28 +08:00
CopyEntity(fromEntityID, newEntity);
return newEntity;
}
public int CloneEntity(int fromEntityID, EcsWorld toWorld)
{
2023-12-20 19:06:17 +08:00
int newEntity = NewEntity();
CopyEntity(fromEntityID, toWorld, newEntity);
return newEntity;
}
2023-05-23 01:47:28 +08:00
public void CloneEntity(int fromEntityID, int toEntityID)
{
CopyEntity(fromEntityID, toEntityID);
2023-05-26 00:24:38 +08:00
foreach (var pool in _pools)
2023-05-23 01:47:28 +08:00
{
2023-05-30 04:32:09 +08:00
if (!pool.Has(fromEntityID) && pool.Has(toEntityID))
2024-02-26 10:43:37 +08:00
{
2023-05-30 04:32:09 +08:00
pool.Del(toEntityID);
2024-02-26 10:43:37 +08:00
}
2023-05-23 01:47:28 +08:00
}
}
//public void CloneEntity(int fromEntityID, EcsWorld toWorld, int toEntityID)
2023-05-30 04:32:09 +08:00
#endregion
2023-05-23 01:47:28 +08:00
2023-05-30 04:32:09 +08:00
#endregion
#region DelEntBuffer
public IsEnableAutoReleaseDelEntBufferScope DisableAutoReleaseDelEntBuffer()
2023-12-31 22:41:48 +08:00
{
return new IsEnableAutoReleaseDelEntBufferScope(this, false);
2023-12-31 22:41:48 +08:00
}
public IsEnableAutoReleaseDelEntBufferScope EnableAutoReleaseDelEntBuffer()
2023-12-31 22:41:48 +08:00
{
return new IsEnableAutoReleaseDelEntBufferScope(this, true);
2023-12-31 22:41:48 +08:00
}
private void SetEnableAutoReleaseDelEntBuffer(bool value)
{
_isEnableAutoReleaseDelEntBuffer = value;
}
public readonly struct IsEnableAutoReleaseDelEntBufferScope : IDisposable
2023-12-31 22:41:48 +08:00
{
private readonly EcsWorld _source;
private readonly bool _lastValue;
public IsEnableAutoReleaseDelEntBufferScope(EcsWorld source, bool value)
2023-12-31 22:41:48 +08:00
{
_lastValue = source._isEnableAutoReleaseDelEntBuffer;
source.SetEnableAutoReleaseDelEntBuffer(value);
2023-12-31 22:41:48 +08:00
_source = source;
}
public void End()
{
_source.SetEnableAutoReleaseDelEntBuffer(_lastValue);
}
void IDisposable.Dispose()
2023-12-31 22:41:48 +08:00
{
End();
2023-12-31 22:41:48 +08:00
}
}
public void ReleaseDelEntityBufferAll()
{
2024-02-10 20:54:09 +08:00
ReleaseDelEntityBuffer(_delEntBufferCount);
}
2024-02-16 21:17:20 +08:00
public unsafe void ReleaseDelEntityBuffer(int count)
{
if (_delEntBufferCount <= 0)
{
return;
}
unchecked { _version++; }
count = Math.Max(0, Math.Min(count, _delEntBufferCount));
_delEntBufferCount -= count;
2024-02-26 07:53:16 +08:00
int slisedCount = count;
for (int i = 0; i < slisedCount; i++)
{
int e = _delEntBuffer[i];
2024-02-29 22:42:33 +08:00
if (_entities[e].componentsCount <= 0)
2024-02-26 07:53:16 +08:00
{
int tmp = _delEntBuffer[i];
_delEntBuffer[i] = _delEntBuffer[--slisedCount];
_delEntBuffer[slisedCount] = tmp;
i--;
}
}
2024-02-16 21:17:20 +08:00
ReadOnlySpan<int> buffer = new ReadOnlySpan<int>(_delEntBuffer, _delEntBufferCount, count);
2024-02-26 07:53:16 +08:00
if (slisedCount > 0)
{
2024-02-26 07:53:16 +08:00
ReadOnlySpan<int> bufferSlised = new ReadOnlySpan<int>(_delEntBuffer, _delEntBufferCount, slisedCount);
for (int i = 0; i < _poolsCount; i++)
{
_pools[i].OnReleaseDelEntityBuffer(bufferSlised);
}
}
for (int i = 0; i < _groups.Count; i++)
{
if (_groups[i].TryGetTarget(out EcsGroup group) && group.IsReleased)
{
group.OnReleaseDelEntityBuffer_Internal(buffer);
}
else
{
RemoveGroupAt(i--);
}
}
2024-02-26 07:53:16 +08:00
2024-02-16 21:17:20 +08:00
_listeners.InvokeOnReleaseDelEntityBuffer(buffer);
for (int i = 0; i < buffer.Length; i++)
{
2024-02-16 21:17:20 +08:00
int e = buffer[i];
2024-02-11 19:59:56 +08:00
_entityDispenser.Release(e);
2024-02-29 22:42:33 +08:00
_entities[e].gen |= SLEEPING_GEN_FLAG;
}
2024-02-14 17:05:41 +08:00
Densify();
}
private void Densify() //уплотнение свободных айдишников
{
2024-02-15 20:28:38 +08:00
_entityDispenser.Sort();
}
#endregion
2023-11-22 20:28:11 +08:00
#region Upsize
2024-02-03 01:12:53 +08:00
public void Upsize(int minSize)
2023-11-22 20:28:11 +08:00
{
2024-02-15 20:28:38 +08:00
_entityDispenser.Upsize(minSize);
2024-02-07 22:16:41 +08:00
}
2024-04-09 00:19:43 +08:00
[MethodImpl(MethodImplOptions.NoInlining)]
2024-04-18 00:36:05 +08:00
private int CalcEntityComponentMaskLength()
2024-04-08 23:49:56 +08:00
{
int result = _pools.Length / COMPONENT_MASK_CHUNK_SIZE;
return (result < 2 ? 2 : result);
}
2024-04-18 00:36:05 +08:00
private void SetEntityComponentMaskLength(int value)
{
_entityComponentMaskLength = value;
_entityComponentMaskLengthBitShift = BitsUtility.GetHighBitNumber(value);
}
2024-02-07 22:16:41 +08:00
[MethodImpl(MethodImplOptions.NoInlining)]
2024-02-15 18:11:24 +08:00
private void OnEntityDispenserResized(int newSize)
2024-02-07 22:16:41 +08:00
{
2024-04-18 00:36:05 +08:00
SetEntityComponentMaskLength(CalcEntityComponentMaskLength()); //_pools.Length / COMPONENT_MASK_CHUNK_SIZE + 1;
2024-02-29 22:42:33 +08:00
Array.Resize(ref _entities, newSize);
2024-02-03 01:12:53 +08:00
Array.Resize(ref _delEntBuffer, newSize);
2024-02-16 21:17:20 +08:00
Array.Resize(ref _entityComponentMasks, newSize * _entityComponentMaskLength);
2023-11-22 20:28:11 +08:00
2024-02-29 22:42:33 +08:00
ArrayUtility.Fill(_entities, EntitySlot.Empty, _entitiesCapacity);
2024-02-15 18:11:24 +08:00
_entitiesCapacity = newSize;
2023-11-22 20:28:11 +08:00
for (int i = 0; i < _groups.Count; i++)
{
if (_groups[i].TryGetTarget(out EcsGroup group))
{
group.OnWorldResize_Internal(newSize);
2023-11-22 20:28:11 +08:00
}
else
{
RemoveGroupAt(i--);
2023-11-22 20:28:11 +08:00
}
}
foreach (var item in _pools)
2024-02-03 01:12:53 +08:00
{
item.OnWorldResize(newSize);
}
_listeners.InvokeOnWorldResize(newSize);
2023-11-22 20:28:11 +08:00
}
#endregion
2023-05-28 06:29:04 +08:00
#region Groups Pool
private void RemoveGroupAt(int index)
{
int last = _groups.Count - 1;
_groups[index] = _groups[last];
_groups.RemoveAt(last);
}
internal void RegisterGroup(EcsGroup group)
{
2023-04-17 22:58:52 +08:00
_groups.Add(new WeakReference<EcsGroup>(group));
}
2023-06-01 20:14:34 +08:00
internal EcsGroup GetFreeGroup()
2023-04-18 19:35:42 +08:00
{
2024-03-07 07:48:18 +08:00
EcsGroup result = _groupsPool.Count <= 0 ? new EcsGroup(this, _configs.GetWorldConfigOrDefault().GroupCapacity) : _groupsPool.Pop();
2023-05-28 06:29:04 +08:00
result._isReleased = false;
return result;
2023-04-18 19:35:42 +08:00
}
internal void ReleaseGroup(EcsGroup group)
2023-04-18 19:35:42 +08:00
{
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
2024-02-26 10:43:37 +08:00
if (group.World != this) { Throw.World_GroupDoesNotBelongWorld(); }
2023-04-18 19:35:42 +08:00
#endif
2023-05-28 06:29:04 +08:00
group._isReleased = true;
2023-04-18 19:35:42 +08:00
group.Clear();
_groupsPool.Push(group);
}
#endregion
2023-06-08 04:04:39 +08:00
#region Listeners
public void AddListener(IEcsWorldEventListener worldEventListener)
2023-06-08 04:04:39 +08:00
{
_listeners.Add(worldEventListener);
2023-06-08 04:04:39 +08:00
}
public void RemoveListener(IEcsWorldEventListener worldEventListener)
2023-06-08 04:04:39 +08:00
{
_listeners.Remove(worldEventListener);
2023-06-08 04:04:39 +08:00
}
public void AddListener(IEcsEntityEventListener entityEventListener)
2023-06-10 18:58:43 +08:00
{
_entityListeners.Add(entityEventListener);
2023-06-10 18:58:43 +08:00
}
public void RemoveListener(IEcsEntityEventListener entityEventListener)
2023-06-10 18:58:43 +08:00
{
_entityListeners.Remove(entityEventListener);
2023-06-10 18:58:43 +08:00
}
2023-06-08 04:04:39 +08:00
#endregion
2024-02-25 23:06:16 +08:00
#region Other
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-02-26 10:43:37 +08:00
public void AggressiveUpVersion() { UpVersion(); }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void UpVersion()
{
unchecked
{
_version++;
_deleteLeakedEntitesLastVersion++;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void UpVersionLeaked()
2024-02-25 23:06:16 +08:00
{
unchecked
{
_version++;
}
}
#endregion
2024-03-03 04:49:35 +08:00
#region Debug Components
private static int[] _componentIDsBuffer = new int[64];
2024-03-07 03:30:18 +08:00
public ReadOnlySpan<int> GetComponentTypeIDsFor(int entityID)
{
2024-03-07 03:30:18 +08:00
int count = GetComponentTypeIDsFor(entityID, ref _componentIDsBuffer);
return new ReadOnlySpan<int>(_componentIDsBuffer, 0, count);
}
2024-03-07 03:30:18 +08:00
public void GetComponentsFor(int entityID, List<object> list)
2023-05-23 01:47:28 +08:00
{
list.Clear();
2024-03-07 03:30:18 +08:00
int count = GetComponentTypeIDsFor(entityID, ref _componentIDsBuffer);
for (int i = 0; i < count; i++)
2023-05-23 01:47:28 +08:00
{
list.Add(_pools[_componentIDsBuffer[i]].GetRaw(entityID));
2023-12-31 21:03:00 +08:00
}
}
2024-03-07 03:30:18 +08:00
public void GetComponentTypesFor(int entityID, HashSet<Type> typeSet)
2023-12-31 21:03:00 +08:00
{
typeSet.Clear();
2024-03-07 03:30:18 +08:00
int count = GetComponentTypeIDsFor(entityID, ref _componentIDsBuffer);
for (int i = 0; i < count; i++)
{
typeSet.Add(_pools[_componentIDsBuffer[i]].ComponentType);
}
}
2024-03-07 03:30:18 +08:00
private int GetComponentTypeIDsFor(int entityID, ref int[] componentIDs)
{
int count = 0;
2023-12-31 21:03:00 +08:00
var itemsCount = GetComponentsCount(entityID);
if (itemsCount <= 0) { return count; }
2024-03-03 05:42:01 +08:00
int poolIndex = 0;
2024-03-03 04:49:35 +08:00
uint bit;
2024-03-03 05:42:01 +08:00
for (int chunkIndex = entityID * _entityComponentMaskLength, chunkIndexMax = chunkIndex + _entityComponentMaskLength; chunkIndex < chunkIndexMax; chunkIndex++)
2023-12-31 21:03:00 +08:00
{
2024-03-03 05:42:01 +08:00
bit = 0x0000_0001;
2024-03-03 04:49:35 +08:00
int chunk = _entityComponentMasks[chunkIndex];
if (chunk == 0)
{
poolIndex += COMPONENT_MASK_CHUNK_SIZE;
}
else
2023-12-31 21:03:00 +08:00
{
2024-03-03 04:49:35 +08:00
while (bit != 0)
2024-02-26 10:43:37 +08:00
{
2024-03-03 04:49:35 +08:00
if ((chunk & bit) != 0)
{
itemsCount--;
2024-03-03 22:46:09 +08:00
if (count > componentIDs.Length)
{
Array.Resize(ref componentIDs, count << 1);
}
componentIDs[count++] = _pools[poolIndex].ComponentTypeID;
2024-03-03 05:42:01 +08:00
if (itemsCount <= 0) { goto exit; }
2024-03-03 04:49:35 +08:00
}
2024-03-03 05:42:01 +08:00
bit <<= 1;
2024-03-03 04:49:35 +08:00
poolIndex++;
2024-02-26 10:43:37 +08:00
}
2023-12-31 21:03:00 +08:00
}
2023-05-23 01:47:28 +08:00
}
2024-03-03 04:49:35 +08:00
exit:;
return count;
2023-05-23 01:47:28 +08:00
}
#endregion
2024-03-07 03:17:51 +08:00
#region EntitySlot
[StructLayout(LayoutKind.Explicit, Pack = 4, Size = 4 * 3)]
private struct EntitySlot
{
public static readonly EntitySlot Empty = new EntitySlot(SLEEPING_GEN_FLAG, 0, false);
[FieldOffset(0)]
public short gen;
[FieldOffset(2)]
public short componentsCount;
[FieldOffset(4)]
public bool isUsed;
//[FieldOffset(5)]
//public bool isLocked;
public EntitySlot(short gen, short componentsCount, bool isUsed)
{
this.gen = gen;
this.componentsCount = componentsCount;
this.isUsed = isUsed;
}
}
#endregion
2024-04-09 00:19:43 +08:00
#region DebuggerProxy
private EcsSpan GetSpan_Debug()
{
return _entityDispenser.UsedToEcsSpan(id);
}
protected class DebuggerProxy
{
private EcsWorld _world;
public EntitySlotInfo[] Entities
{
get
{
EntitySlotInfo[] result = new EntitySlotInfo[_world.Count];
int i = 0;
foreach (var e in _world.ToSpan())
{
result[i++] = _world.GetEntitySlotInfoDebug(e);
}
return result;
}
}
2024-04-29 21:50:32 +08:00
public long Version { get { return _world.Version; } }
public IEcsPool[] Pools { get { return _world._pools; } }
public TypeMeta _TypeMeta { get { return _world.GetMeta(); } }
2024-04-09 00:19:43 +08:00
public DebuggerProxy(EcsWorld world)
{
_world = world;
}
}
#endregion
2023-05-30 04:32:09 +08:00
}
2023-05-28 05:53:08 +08:00
2024-03-28 20:32:24 +08:00
public static class EcsWorldExtenssions
{
public static bool IsNullOrDetroyed(this EcsWorld self)
{
return self == null || self.IsDestroyed;
}
}
2023-05-26 00:24:38 +08:00
#region Callbacks Interface
public interface IEcsWorldEventListener
{
void OnWorldResize(int newSize);
void OnReleaseDelEntityBuffer(ReadOnlySpan<int> buffer);
void OnWorldDestroy();
2023-06-10 18:58:43 +08:00
}
public interface IEcsEntityEventListener
{
void OnNewEntity(int entityID);
void OnDelEntity(int entityID);
}
2023-05-26 00:24:38 +08:00
internal static class WorldEventListExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void InvokeOnWorldResize(this List<IEcsWorldEventListener> self, int newSize)
{
2024-02-26 10:43:37 +08:00
for (int i = 0, iMax = self.Count; i < iMax; i++)
{
self[i].OnWorldResize(newSize);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void InvokeOnReleaseDelEntityBuffer(this List<IEcsWorldEventListener> self, ReadOnlySpan<int> buffer)
{
2024-02-26 10:43:37 +08:00
for (int i = 0, iMax = self.Count; i < iMax; i++)
{
self[i].OnReleaseDelEntityBuffer(buffer);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void InvokeOnWorldDestroy(this List<IEcsWorldEventListener> self)
{
2024-02-26 10:43:37 +08:00
for (int i = 0, iMax = self.Count; i < iMax; i++)
{
self[i].OnWorldDestroy();
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-06-10 18:58:43 +08:00
public static void InvokeOnNewEntity(this List<IEcsEntityEventListener> self, int entityID)
{
2024-02-26 10:43:37 +08:00
for (int i = 0, iMax = self.Count; i < iMax; i++)
{
self[i].OnNewEntity(entityID);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-06-10 18:58:43 +08:00
public static void InvokeOnDelEntity(this List<IEcsEntityEventListener> self, int entityID)
{
2024-02-26 10:43:37 +08:00
for (int i = 0, iMax = self.Count; i < iMax; i++)
{
self[i].OnDelEntity(entityID);
}
}
}
#endregion
2023-05-28 06:35:33 +08:00
#region Extensions
public static class IntExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-02-26 10:43:37 +08:00
public static entlong ToEntityLong(this int self, EcsWorld world)
{
return world.GetEntityLong(self);
}
2023-05-28 06:35:33 +08:00
}
#endregion
2024-02-03 01:12:53 +08:00
}