DragonECS/src/EcsWorld.cs

1160 lines
40 KiB
C#
Raw Normal View History

2024-11-01 12:41:10 +08:00
using DCFApixels.DragonECS.Core;
using DCFApixels.DragonECS.Internal;
using DCFApixels.DragonECS.PoolsCore;
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;
2024-06-13 18:04:18 +08:00
[Il2CppSetOption(Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
2024-04-28 19:43:10 +08:00
#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-05-01 13:38:08 +08:00
2024-04-28 19:43:10 +08:00
#if ENABLE_IL2CPP
[Il2CppSetOption(Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif
2024-06-13 18:04:18 +08:00
[MetaColor(MetaColor.DragonRose)]
[MetaGroup(EcsConsts.PACK_GROUP, EcsConsts.WORLDS_GROUP)]
[MetaDescription(EcsConsts.AUTHOR, "It is a container for entities and components.")]
2024-10-12 00:08:12 +08:00
[MetaID("AEF3557C92019C976FC48F90E95A9DA6")]
2024-04-09 00:19:43 +08:00
[DebuggerTypeProxy(typeof(DebuggerProxy))]
2024-06-05 14:39:19 +08:00
public partial class EcsWorld : IEntityStorage, IEcsMember
2023-06-21 01:37:05 +08:00
{
2024-10-31 16:27:53 +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-10-05 18:05:33 +08:00
EcsWorld IEntityStorage.World
{
get { return this; }
}
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
{
2024-08-24 21:02:53 +08:00
ReleaseDelEntityBufferAll();
2024-10-31 16:27:53 +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-09-09 18:21:21 +08:00
lock (_worldLock)
2024-03-07 03:17:51 +08:00
{
if (configs == null) { configs = ConfigContainer.Empty; }
bool nullWorld = this is NullWorld;
if (nullWorld == false && worldID == NULL_WORLD_ID)
{
EcsDebug.PrintWarning($"The world identifier cannot be {NULL_WORLD_ID}");
}
_configs = configs;
EcsWorldConfig config = configs.GetWorldConfigOrDefault();
2023-04-23 17:19:52 +08:00
if (worldID < 0 || (worldID == NULL_WORLD_ID && nullWorld == false))
2024-02-15 20:28:38 +08:00
{
worldID = (short)_worldIdDispenser.UseFree();
2024-02-15 20:28:38 +08:00
}
else
2024-02-15 00:53:20 +08:00
{
if (worldID != _worldIdDispenser.NullID)
{
_worldIdDispenser.Use(worldID);
}
if (_worlds[worldID] != null)
{
_worldIdDispenser.Release(worldID);
Throw.Exception("The world with the specified ID has already been created\r\n");
}
2024-02-15 00:53:20 +08:00
}
2024-10-31 16:27:53 +08:00
ID = worldID;
_worlds[worldID] = this;
_poolsMediator = new PoolsMediator(this);
2024-02-03 01:12:53 +08:00
int poolsCapacity = ArrayUtility.NormalizeSizeToPowerOfTwo(config.PoolsCapacity);
_pools = new IEcsPoolImplementation[poolsCapacity];
_poolSlots = new PoolSlot[poolsCapacity];
ArrayUtility.Fill(_pools, _nullPool);
2023-04-01 20:45:37 +08:00
int entitiesCapacity = ArrayUtility.NormalizeSizeToPowerOfTwo(config.EntitiesCapacity);
_entityDispenser = new IdDispenser(entitiesCapacity, 0, OnEntityDispenserResized);
2024-03-13 17:41:33 +08:00
2024-11-05 17:16:50 +08:00
_executorCoures = new Dictionary<(Type, object), IQueryExecutorImplementation>(config.PoolComponentsCapacity);
2024-11-05 15:50:03 +08:00
2024-11-05 17:16:50 +08:00
GetComponentTypeID<NullComponent>();
}
2023-03-02 14:42:44 +08:00
}
2023-04-20 20:10:16 +08:00
public void Destroy()
{
2024-09-09 18:21:21 +08:00
lock (_worldLock)
2024-03-10 21:38:43 +08:00
{
if (_isDestroyed)
{
EcsDebug.PrintWarning("The world is already destroyed");
return;
}
2024-10-31 16:27:53 +08:00
if (ID == NULL_WORLD_ID)
{
2024-03-10 21:47:28 +08:00
#if (DEBUG && !DISABLE_DEBUG)
Throw.World_WorldCantBeDestroyed();
2024-03-10 21:47:28 +08:00
#endif
return;
}
_listeners.InvokeOnWorldDestroy();
_entityDispenser = null;
_pools = null;
_nullPool = null;
2024-10-31 16:27:53 +08:00
_worlds[ID] = null;
ReleaseData(ID);
_worldIdDispenser.Release(ID);
_isDestroyed = true;
_poolTypeCode_2_CmpTypeIDs = null;
_cmpTypeCode_2_CmpTypeIDs = null;
foreach (var item in _executorCoures)
{
item.Value.Destroy();
}
//_entities - не обнуляется для работы entlong.IsAlive
2024-08-24 13:10:50 +08:00
}
}
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)]
2024-07-05 22:13:17 +08:00
public TAspect GetAspect<TAspect>() where TAspect : EcsAspect, new()
{
2024-10-03 08:20:22 +08:00
return Get<AspectCache<TAspect>>().Instance;
}
2023-06-27 05:30:45 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-10-03 08:20:22 +08:00
public void GetQueryCache<TExecutor, TAspect>(out TExecutor executor, out TAspect aspect)
2024-11-05 15:50:03 +08:00
where TExecutor : MaskQueryExecutor, new()
2024-10-03 08:20:22 +08:00
where TAspect : EcsAspect, new()
{
2024-11-03 18:57:56 +08:00
ref var cmp = ref Get<WhereQueryCache<TExecutor, TAspect>>();
2024-10-03 08:20:22 +08:00
executor = cmp.Executor;
aspect = cmp.Aspcet;
2023-05-30 15:20:27 +08:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref T Get<T>() where T : struct
{
2024-10-31 16:27:53 +08:00
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
{
2024-10-31 16:27:53 +08:00
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();
}
2024-10-31 16:27:53 +08:00
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-10-31 16:27:53 +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-10-31 16:27:53 +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
2024-11-01 12:41:10 +08:00
public bool IsMatchesMask(IComponentMask mask, int entityID)
2024-10-14 14:45:41 +08:00
{
return IsMatchesMask(mask.ToMask(this), entityID);
}
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-11-01 12:41:10 +08:00
if (mask.WorldID != ID) { Throw.World_MaskDoesntBelongWorld(); }
2023-11-22 20:28:11 +08:00
#endif
2024-11-01 12:41:10 +08:00
for (int i = 0, iMax = mask._incs.Length; i < iMax; i++)
2023-11-22 20:28:11 +08:00
{
2024-11-01 12:41:10 +08:00
if (!_pools[mask._incs[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-11-01 12:41:10 +08:00
for (int i = 0, iMax = mask._excs.Length; i < iMax; i++)
2023-11-22 20:28:11 +08:00
{
2024-11-01 12:41:10 +08:00
if (_pools[mask._excs[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
//TODO протестить Copy Clone Move Remove
#region CopyEntity
public unsafe void CopyEntity(int fromEntityID, int toEntityID)
2023-05-23 01:47:28 +08:00
{
2024-11-06 18:27:33 +08:00
const int BUFFER_THRESHOLD = 100;
int count = GetComponentsCount(fromEntityID);
int* poolIdsPtr;
if (count < BUFFER_THRESHOLD)
2023-05-23 01:47:28 +08:00
{
2024-11-06 18:27:33 +08:00
int* ptr = stackalloc int[count];
poolIdsPtr = ptr;
}
else
{
poolIdsPtr = UnmanagedArrayUtility.New<int>(count);
}
UnsafeArray<int> ua = UnsafeArray<int>.Manual(poolIdsPtr, count);
GetComponentTypeIDsFor_Internal(fromEntityID, poolIdsPtr, count);
for (int i = 0; i < count; i++)
{
_pools[poolIdsPtr[i]].Copy(fromEntityID, toEntityID);
}
if (count >= BUFFER_THRESHOLD)
{
UnmanagedArrayUtility.Free(poolIdsPtr);
}
2024-11-06 18:27:33 +08:00
//foreach (var pool in _pools)
//{
// if (pool.Has(fromEntityID))
// {
// pool.Copy(fromEntityID, toEntityID);
// }
//}
}
public void CopyEntity(int fromEntityID, int toEntityID, ReadOnlySpan<int> componentTypeIDs)
{
foreach (var poolID in componentTypeIDs)
{
var pool = _pools[poolID];
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
}
}
}
2024-11-06 18:27:33 +08:00
public unsafe void CopyEntity(int fromEntityID, EcsWorld toWorld, int toEntityID)
{
2024-11-06 18:27:33 +08:00
const int BUFFER_THRESHOLD = 100;
int count = GetComponentsCount(fromEntityID);
int* poolIdsPtr;
if (count < BUFFER_THRESHOLD)
{
2024-11-06 18:27:33 +08:00
int* ptr = stackalloc int[count];
poolIdsPtr = ptr;
}
else
{
poolIdsPtr = UnmanagedArrayUtility.New<int>(count);
}
GetComponentTypeIDsFor_Internal(fromEntityID, poolIdsPtr, count);
for (int i = 0; i < count; i++)
{
_pools[poolIdsPtr[i]].Copy(fromEntityID, toWorld, toEntityID);
}
if (count >= BUFFER_THRESHOLD)
{
UnmanagedArrayUtility.Free(poolIdsPtr);
}
//foreach (var pool in _pools)
//{
// if (pool.Has(fromEntityID))
// {
// pool.Copy(fromEntityID, toWorld, toEntityID);
// }
//}
}
public void CopyEntity(int fromEntityID, EcsWorld toWorld, int toEntityID, ReadOnlySpan<int> componentTypeIDs)
{
foreach (var poolID in componentTypeIDs)
{
var pool = _pools[poolID];
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
}
}
#endregion
#region CloneEntity
public int CloneEntity(int entityID)
2023-05-23 01:47:28 +08:00
{
2023-12-20 19:06:17 +08:00
int newEntity = NewEntity();
CopyEntity(entityID, newEntity);
2023-05-23 01:47:28 +08:00
return newEntity;
}
public int CloneEntity(int entityID, ReadOnlySpan<int> componentTypeIDs)
{
2023-12-20 19:06:17 +08:00
int newEntity = NewEntity();
CopyEntity(entityID, newEntity, componentTypeIDs);
return newEntity;
}
public int CloneEntity(int entityID, EcsWorld toWorld)
{
int newEntity = NewEntity();
CopyEntity(entityID, toWorld, newEntity);
return newEntity;
}
public int CloneEntity(int entityID, EcsWorld toWorld, ReadOnlySpan<int> componentTypeIDs)
{
int newEntity = NewEntity();
CopyEntity(entityID, toWorld, newEntity, componentTypeIDs);
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
#region MoveComponents
2024-11-06 18:27:33 +08:00
public unsafe void MoveComponents(int fromEntityID, int toEntityID)
{
2024-11-06 18:27:33 +08:00
const int BUFFER_THRESHOLD = 100;
int count = GetComponentsCount(fromEntityID);
int* poolIdsPtr;
if (count < BUFFER_THRESHOLD)
{
2024-11-06 18:27:33 +08:00
int* ptr = stackalloc int[count];
poolIdsPtr = ptr;
}
else
{
poolIdsPtr = UnmanagedArrayUtility.New<int>(count);
}
GetComponentTypeIDsFor_Internal(fromEntityID, poolIdsPtr, count);
for (int i = 0; i < count; i++)
{
var pool = _pools[poolIdsPtr[i]];
pool.Copy(fromEntityID, toEntityID);
pool.Del(fromEntityID);
}
if (count >= BUFFER_THRESHOLD)
{
UnmanagedArrayUtility.Free(poolIdsPtr);
}
}
public void MoveComponents(int fromEntityID, int toEntityID, ReadOnlySpan<int> componentTypeIDs)
{
foreach (var poolID in componentTypeIDs)
{
var pool = _pools[poolID];
if (pool.Has(fromEntityID))
{
pool.Copy(fromEntityID, toEntityID);
pool.Del(fromEntityID);
}
}
}
#endregion
#region RemoveComponents
2024-11-06 18:27:33 +08:00
public unsafe void RemoveComponents(int fromEntityID, int toEntityID)
{
2024-11-06 18:27:33 +08:00
const int BUFFER_THRESHOLD = 100;
int count = GetComponentsCount(fromEntityID);
int* poolIdsPtr;
if (count < BUFFER_THRESHOLD)
{
2024-11-06 18:27:33 +08:00
int* ptr = stackalloc int[count];
poolIdsPtr = ptr;
}
else
{
poolIdsPtr = UnmanagedArrayUtility.New<int>(count);
}
GetComponentTypeIDsFor_Internal(fromEntityID, poolIdsPtr, count);
for (int i = 0; i < count; i++)
{
_pools[poolIdsPtr[i]].Del(fromEntityID);
}
if (count >= BUFFER_THRESHOLD)
{
UnmanagedArrayUtility.Free(poolIdsPtr);
}
}
2024-11-06 14:28:57 +08:00
public void RemoveComponents(int fromEntityID, int toEntityID, ReadOnlySpan<int> componentTypeIDs)
{
foreach (var poolID in componentTypeIDs)
{
var pool = _pools[poolID];
if (pool.Has(fromEntityID))
{
pool.Del(fromEntityID);
}
}
}
#endregion
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++)
{
2024-08-03 22:11:55 +08:00
if (_groups[i].TryGetTarget(out EcsGroup group))
{
2024-08-14 21:23:34 +08:00
if (group.IsReleased)
2024-08-03 22:11:55 +08:00
{
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
2024-09-09 18:21:21 +08:00
[ThreadStatic]
private static int[] _componentIDsBuffer;
2024-03-07 03:30:18 +08:00
public ReadOnlySpan<int> GetComponentTypeIDsFor(int entityID)
{
2024-11-06 16:08:33 +08:00
int count = GetComponentTypeIDsFor_Internal(entityID, ref _componentIDsBuffer);
return new ReadOnlySpan<int>(_componentIDsBuffer, 0, count);
}
2024-10-02 22:13:10 +08:00
public void GetComponentPoolsFor(int entityID, List<IEcsPool> list)
{
list.Clear();
2024-11-06 16:08:33 +08:00
int count = GetComponentTypeIDsFor_Internal(entityID, ref _componentIDsBuffer);
2024-10-02 22:13:10 +08:00
for (int i = 0; i < count; i++)
{
list.Add(_pools[_componentIDsBuffer[i]]);
}
}
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-11-06 16:08:33 +08:00
int count = GetComponentTypeIDsFor_Internal(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-11-06 16:08:33 +08:00
int count = GetComponentTypeIDsFor_Internal(entityID, ref _componentIDsBuffer);
for (int i = 0; i < count; i++)
{
typeSet.Add(_pools[_componentIDsBuffer[i]].ComponentType);
}
}
2024-11-06 16:08:33 +08:00
private unsafe int GetComponentTypeIDsFor_Internal(int entityID, ref int[] componentIDs)
{
2024-11-06 16:08:33 +08:00
var itemsCount = GetComponentsCount(entityID);
2024-11-06 18:27:33 +08:00
if (componentIDs == null)
2024-09-09 18:21:21 +08:00
{
2024-11-06 16:08:33 +08:00
componentIDs = new int[itemsCount];
2024-09-09 18:21:21 +08:00
}
2024-11-06 18:27:33 +08:00
if (componentIDs.Length < itemsCount)
2024-11-06 16:08:33 +08:00
{
Array.Resize(ref componentIDs, itemsCount);
}
2024-11-06 18:27:33 +08:00
if (itemsCount <= 0) { return 0; }
2024-11-06 16:08:33 +08:00
fixed (int* ptr = componentIDs)
{
2024-11-06 18:27:33 +08:00
GetComponentTypeIDsFor_Internal(entityID, ptr, itemsCount);
2024-11-06 16:08:33 +08:00
}
return itemsCount;
}
2024-11-06 18:27:33 +08:00
private unsafe void GetComponentTypeIDsFor_Internal(int entityID, int* componentIDs, int itemsCount)
2024-11-06 16:08:33 +08:00
{
2024-11-06 18:27:33 +08:00
const int COMPONENT_MASK_CHUNK_SIZE_HALF = COMPONENT_MASK_CHUNK_SIZE / 2;
// проверка на itemsCount <= 0 не обяательна, алгоритм не ломается,
// только впустую отрабатыват по всем чанкам,
// но как правильно для пустых сущностей этот алгоритм не применим.
2024-03-03 05:42:01 +08:00
int poolIndex = 0;
2024-11-06 16:08:33 +08:00
int bit;
for (int chunkIndex = entityID << _entityComponentMaskLengthBitShift,
chunkIndexMax = chunkIndex + _entityComponentMaskLength;
chunkIndex < chunkIndexMax;
chunkIndex++)
2023-12-31 21:03:00 +08:00
{
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-11-06 16:08:33 +08:00
if ((chunk & 0x0000FFFF) != 0)
{
bit = 0x0000_0001;
2024-11-06 18:27:33 +08:00
while (bit < 0x0001_0000)
2024-11-06 16:08:33 +08:00
{
if ((chunk & bit) != 0)
{
2024-11-06 18:27:33 +08:00
*componentIDs = poolIndex;
2024-11-06 16:08:33 +08:00
componentIDs++;
itemsCount--;
if (itemsCount <= 0) { return; }
}
poolIndex++;
bit <<= 1;
}
}
2024-11-06 18:27:33 +08:00
else
{
poolIndex += COMPONENT_MASK_CHUNK_SIZE_HALF;
}
2024-11-06 16:08:33 +08:00
if ((chunk & -0x7FFF0000) != 0)
2024-02-26 10:43:37 +08:00
{
2024-11-06 16:08:33 +08:00
bit = 0x0001_0000;
while (bit != 0x0000_0000)
2024-03-03 04:49:35 +08:00
{
2024-11-06 16:08:33 +08:00
if ((chunk & bit) != 0)
{
2024-11-06 18:27:33 +08:00
*componentIDs = poolIndex;
2024-11-06 16:08:33 +08:00
componentIDs++;
itemsCount--;
if (itemsCount <= 0) { return; }
}
2024-11-06 16:08:33 +08:00
poolIndex++;
bit <<= 1;
2024-03-03 04:49:35 +08:00
}
2024-02-26 10:43:37 +08:00
}
2024-11-06 18:27:33 +08:00
else
{
poolIndex += COMPONENT_MASK_CHUNK_SIZE_HALF;
}
2023-12-31 21:03:00 +08:00
}
2023-05-23 01:47:28 +08:00
}
}
#endregion
2024-03-07 03:17:51 +08:00
#region EntitySlot
[StructLayout(LayoutKind.Sequential)]
2024-03-07 03:17:51 +08:00
private struct EntitySlot
{
public static readonly EntitySlot Empty = new EntitySlot(SLEEPING_GEN_FLAG, 0, false);
public short gen;
public short componentsCount;
public bool isUsed;
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()
{
2024-10-31 16:27:53 +08:00
return _entityDispenser.UsedToEcsSpan(ID);
2024-04-09 00:19:43 +08:00
}
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; } }
2024-10-31 16:27:53 +08:00
public short ID { get { return _world.ID; } }
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
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
2024-09-09 18:21:30 +08:00
public static class EcsWorldExtenssions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNullOrDetroyed(this EcsWorld self)
{
return self == null || self.IsDestroyed;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ReleaseDelEntityBufferAllAuto(this EcsWorld self)
{
if (self.IsEnableReleaseDelEntBuffer)
{
self.ReleaseDelEntityBufferAll();
}
}
}
2023-05-28 06:35:33 +08:00
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
}