2023-05-26 06:18:09 +08:00
|
|
|
|
using DCFApixels.DragonECS.Internal;
|
|
|
|
|
using System;
|
2023-03-02 14:42:44 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
|
|
|
|
|
namespace DCFApixels.DragonECS
|
|
|
|
|
{
|
2024-02-11 01:29:18 +08:00
|
|
|
|
public abstract partial class EcsWorld : IEntityStorage
|
2023-06-21 01:37:05 +08:00
|
|
|
|
{
|
2023-06-18 02:42:20 +08:00
|
|
|
|
public readonly short id;
|
2024-02-03 01:12:53 +08:00
|
|
|
|
private IEcsWorldConfig _config;
|
2023-04-20 20:03:26 +08:00
|
|
|
|
|
2023-06-29 00:56:26 +08:00
|
|
|
|
private bool _isDestroyed;
|
|
|
|
|
|
2024-02-13 21:00:32 +08:00
|
|
|
|
private IdDispenser _entityDispenser;
|
2023-04-01 22:18:40 +08:00
|
|
|
|
private int _entitiesCount;
|
2023-04-18 19:35:42 +08:00
|
|
|
|
private int _entitesCapacity;
|
2023-05-26 00:24:38 +08:00
|
|
|
|
private short[] _gens; //старший бит указывает на то жива ли сущность
|
2023-04-26 16:45:37 +08:00
|
|
|
|
private short[] _componentCounts;
|
2023-04-20 11:37:27 +08:00
|
|
|
|
|
2023-04-20 18:23:23 +08:00
|
|
|
|
private int[] _delEntBuffer;
|
2023-04-20 11:37:27 +08:00
|
|
|
|
private int _delEntBufferCount;
|
2024-01-29 01:10:52 +08:00
|
|
|
|
private bool _isEnableAutoReleaseDelEntBuffer = true;
|
2023-04-18 19:35:42 +08:00
|
|
|
|
|
2024-02-11 00:42:49 +08:00
|
|
|
|
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-11-22 17:35:03 +08:00
|
|
|
|
internal int[][] _entitiesComponentMasks;
|
|
|
|
|
|
2023-12-06 18:58:06 +08:00
|
|
|
|
private readonly PoolsMediator _poolsMediator;
|
|
|
|
|
|
2023-03-02 14:42:44 +08:00
|
|
|
|
#region Properties
|
2024-02-03 01:12:53 +08:00
|
|
|
|
public IEcsWorldConfig Config
|
|
|
|
|
{
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
get { return _config; }
|
|
|
|
|
}
|
2024-02-11 01:16:47 +08:00
|
|
|
|
public long Version
|
|
|
|
|
{
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
get { return _version; }
|
|
|
|
|
}
|
2024-01-29 01:10:52 +08:00
|
|
|
|
public bool IsDestroyed
|
|
|
|
|
{
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
get { return _isDestroyed; }
|
|
|
|
|
}
|
|
|
|
|
public int Count
|
|
|
|
|
{
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
get { return _entitiesCount; }
|
|
|
|
|
}
|
|
|
|
|
public int Capacity
|
|
|
|
|
{
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
get { return _entitesCapacity; }
|
|
|
|
|
}
|
|
|
|
|
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
|
2024-01-29 01:10:52 +08:00
|
|
|
|
{
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_isEnableAutoReleaseDelEntBuffer)
|
|
|
|
|
{
|
|
|
|
|
ReleaseDelEntityBufferAll();
|
|
|
|
|
}
|
2024-02-13 21:00:32 +08:00
|
|
|
|
return _entityDispenser.UsedToEcsSpan(id);
|
2024-01-29 01:10:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public ReadOnlySpan<IEcsPoolImplementation> 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-02-15 00:53:20 +08:00
|
|
|
|
public EcsWorld(IEcsWorldConfig config = null, short worldID = -1)
|
2023-04-20 20:03:26 +08:00
|
|
|
|
{
|
2024-02-03 01:12:53 +08:00
|
|
|
|
if (config == null)
|
|
|
|
|
{
|
2024-02-10 20:53:58 +08:00
|
|
|
|
config = EcsWorldConfig.Empty;
|
2024-02-03 01:12:53 +08:00
|
|
|
|
}
|
|
|
|
|
_config = config;
|
2023-04-23 17:19:52 +08:00
|
|
|
|
|
2024-02-15 00:53:20 +08:00
|
|
|
|
if (worldID < 0)
|
2023-05-07 00:50:02 +08:00
|
|
|
|
{
|
2024-02-15 00:53:20 +08:00
|
|
|
|
worldID = (short)_worldIdDispenser.UseFree();
|
|
|
|
|
if (worldID >= Worlds.Length)
|
2024-01-29 01:10:52 +08:00
|
|
|
|
{
|
2023-05-07 00:50:02 +08:00
|
|
|
|
Array.Resize(ref Worlds, Worlds.Length << 1);
|
2024-01-29 01:10:52 +08:00
|
|
|
|
}
|
2023-05-07 00:50:02 +08:00
|
|
|
|
}
|
2024-02-15 00:53:20 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (Worlds[worldID] != null)
|
|
|
|
|
{
|
|
|
|
|
Throw.UndefinedException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Worlds[worldID] = this;
|
2023-04-20 20:03:26 +08:00
|
|
|
|
|
2024-02-03 01:12:53 +08:00
|
|
|
|
_poolsMediator = new PoolsMediator(this);
|
|
|
|
|
|
2024-02-07 22:16:41 +08:00
|
|
|
|
int poolsCapacity = ArrayUtility.NormalizeSizeToPowerOfTwo(config.Get_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-02-07 22:16:41 +08:00
|
|
|
|
_entitesCapacity = ArrayUtility.NormalizeSizeToPowerOfTwo(config.Get_EntitiesCapacity());
|
2024-02-13 21:00:32 +08:00
|
|
|
|
_entityDispenser = new IdDispenser(_entitesCapacity, 0);
|
2023-04-23 17:19:52 +08:00
|
|
|
|
_gens = new short[_entitesCapacity];
|
2023-04-26 16:45:37 +08:00
|
|
|
|
_componentCounts = new short[_entitesCapacity];
|
|
|
|
|
|
2023-04-23 17:19:52 +08:00
|
|
|
|
ArrayUtility.Fill(_gens, DEATH_GEN_BIT);
|
2023-04-20 11:37:27 +08:00
|
|
|
|
_delEntBufferCount = 0;
|
2023-11-22 11:53:41 +08:00
|
|
|
|
_delEntBuffer = new int[_entitesCapacity];
|
2023-11-22 17:35:03 +08:00
|
|
|
|
_entitiesComponentMasks = new int[_entitesCapacity][];
|
2024-02-03 01:12:53 +08:00
|
|
|
|
|
|
|
|
|
int maskLength = _pools.Length / 32 + 1;
|
2023-11-22 17:35:03 +08:00
|
|
|
|
for (int i = 0; i < _entitesCapacity; i++)
|
2024-01-01 21:44:33 +08:00
|
|
|
|
{
|
2024-02-03 01:12:53 +08:00
|
|
|
|
_entitiesComponentMasks[i] = new int[maskLength];
|
2024-01-01 21:44:33 +08:00
|
|
|
|
}
|
2023-03-02 14:42:44 +08:00
|
|
|
|
}
|
2023-04-20 20:10:16 +08:00
|
|
|
|
public void Destroy()
|
2023-04-20 20:03:26 +08:00
|
|
|
|
{
|
2023-04-20 20:10:16 +08:00
|
|
|
|
_entityDispenser = null;
|
|
|
|
|
_gens = null;
|
2023-05-26 00:24:38 +08:00
|
|
|
|
_pools = null;
|
2023-04-20 20:10:16 +08:00
|
|
|
|
_nullPool = null;
|
2023-06-18 02:42:20 +08:00
|
|
|
|
Worlds[id] = null;
|
2023-06-21 15:03:33 +08:00
|
|
|
|
ReleaseData(id);
|
2023-06-18 02:42:20 +08:00
|
|
|
|
_worldIdDispenser.Release(id);
|
2023-06-29 00:56:26 +08:00
|
|
|
|
_isDestroyed = true;
|
2024-02-14 21:13:00 +08:00
|
|
|
|
_poolTypeCode_2_CmpTypeIDs = null;
|
|
|
|
|
_componentTypeCode_2_CmpTypeIDs = null;
|
2023-04-20 20:03:26 +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)]
|
2023-06-22 14:31:13 +08:00
|
|
|
|
public TAspect GetAspect<TAspect>() where TAspect : EcsAspect
|
2023-05-07 00:50:02 +08:00
|
|
|
|
{
|
2023-06-27 05:30:45 +08:00
|
|
|
|
return Get<AspectCache<TAspect>>().instance;
|
2023-05-07 00:50:02 +08:00
|
|
|
|
}
|
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-05-07 00:50:02 +08:00
|
|
|
|
{
|
2023-06-27 05:30:45 +08:00
|
|
|
|
return Get<ExcecutorCache<TExecutor>>().instance;
|
2023-05-30 15:20:27 +08:00
|
|
|
|
}
|
2023-06-27 01:23:01 +08:00
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-12-31 13:07:53 +08:00
|
|
|
|
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)]
|
2023-12-31 13:07:53 +08:00
|
|
|
|
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)]
|
2023-12-31 13:07:53 +08:00
|
|
|
|
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)]
|
2023-12-31 13:07:53 +08:00
|
|
|
|
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
|
|
|
|
|
2024-02-14 01:04:22 +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-11 01:16:47 +08:00
|
|
|
|
unchecked { _version++; }
|
2024-02-13 21:00:32 +08:00
|
|
|
|
int entityID = _entityDispenser.UseFree();
|
2023-04-18 19:35:42 +08:00
|
|
|
|
_entitiesCount++;
|
2023-04-01 22:18:40 +08:00
|
|
|
|
|
2024-02-14 01:04:22 +08:00
|
|
|
|
if (_entitesCapacity <= entityID)
|
2024-02-03 01:12:53 +08:00
|
|
|
|
{
|
2024-02-07 22:16:41 +08:00
|
|
|
|
Upsize_Internal(_gens.Length << 1);
|
2024-02-03 01:12:53 +08:00
|
|
|
|
}
|
2023-05-23 01:47:28 +08:00
|
|
|
|
|
2024-02-14 01:04:22 +08:00
|
|
|
|
_gens[entityID] &= GEN_MASK;
|
2023-06-10 18:58:43 +08:00
|
|
|
|
_entityListeners.InvokeOnNewEntity(entityID);
|
2023-05-07 00:50:02 +08:00
|
|
|
|
return entityID;
|
|
|
|
|
}
|
2024-02-14 01:04:22 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public void NewEntity(int entityID)
|
|
|
|
|
{
|
|
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
|
|
|
|
if (IsUsed(entityID))
|
|
|
|
|
{
|
|
|
|
|
Throw.World_EntityIsAlreadyСontained(entityID);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
unchecked { _version++; }
|
|
|
|
|
_entityDispenser.Use(entityID);
|
|
|
|
|
_entitiesCount++;
|
|
|
|
|
|
|
|
|
|
if (_entitesCapacity <= entityID)
|
|
|
|
|
{
|
|
|
|
|
Upsize_Internal(_gens.Length << 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_gens[entityID] &= GEN_MASK;
|
|
|
|
|
_entityListeners.InvokeOnNewEntity(entityID);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-20 19:06:17 +08:00
|
|
|
|
public entlong NewEntityLong()
|
2023-05-07 00:50:02 +08:00
|
|
|
|
{
|
2023-12-20 19:06:17 +08:00
|
|
|
|
int e = NewEntity();
|
2023-05-07 00:50:02 +08:00
|
|
|
|
return GetEntityLong(e);
|
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)]
|
2023-04-26 16:45:37 +08:00
|
|
|
|
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-11 01:29:18 +08:00
|
|
|
|
if (IsUsed(entityID) == false)
|
2024-02-07 22:16:41 +08:00
|
|
|
|
{
|
2024-02-11 22:10:05 +08:00
|
|
|
|
Throw.World_EntityIsNotContained(entityID);
|
2024-02-07 22:16:41 +08:00
|
|
|
|
}
|
|
|
|
|
#endif
|
2023-04-26 16:45:37 +08:00
|
|
|
|
_delEntBuffer[_delEntBufferCount++] = entityID;
|
|
|
|
|
_gens[entityID] |= DEATH_GEN_BIT;
|
2023-04-01 22:18:40 +08:00
|
|
|
|
_entitiesCount--;
|
2023-06-10 18:58:43 +08:00
|
|
|
|
_entityListeners.InvokeOnDelEntity(entityID);
|
2023-04-20 11:37:27 +08:00
|
|
|
|
}
|
2024-02-14 01:04:22 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Other
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public EcsSpan ToSpan()
|
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
2023-10-31 03:03:13 +08:00
|
|
|
|
long x = (long)id << 48 | (long)_gens[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-11 22:10:05 +08:00
|
|
|
|
public bool IsAlive(int entityID, short gen) { return _gens[entityID] == gen; }
|
|
|
|
|
|
2023-04-24 16:48:18 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-02-11 22:10:05 +08:00
|
|
|
|
public bool IsUsed(int entityID) { return _gens[entityID] >= 0; }
|
2023-05-30 04:32:09 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-02-11 22:10:05 +08:00
|
|
|
|
public short GetGen(int entityID) { return _gens[entityID]; }
|
2023-05-30 04:32:09 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-02-11 22:10:05 +08:00
|
|
|
|
public short GetComponentsCount(int entityID) { return _componentCounts[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-02-11 22:10:05 +08:00
|
|
|
|
if (mask.worldID != id) { Throw.World_MaskDoesntBelongWorld(); }
|
2023-11-22 20:28:11 +08:00
|
|
|
|
#endif
|
|
|
|
|
for (int i = 0, iMax = mask.incChunckMasks.Length; i < iMax; i++)
|
|
|
|
|
{
|
|
|
|
|
if (!_pools[mask.inc[i]].Has(entityID))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0, iMax = mask.excChunckMasks.Length; i < iMax; i++)
|
|
|
|
|
{
|
|
|
|
|
if (_pools[mask.exc[i]].Has(entityID))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2023-05-30 04:32:09 +08:00
|
|
|
|
}
|
2023-12-31 13:07:53 +08:00
|
|
|
|
|
2023-04-26 16:45:37 +08:00
|
|
|
|
public void DeleteEmptyEntites()
|
|
|
|
|
{
|
2024-02-13 21:00:32 +08:00
|
|
|
|
foreach (var e in Entities)
|
2023-04-26 16:45:37 +08:00
|
|
|
|
{
|
2023-12-31 13:07:53 +08:00
|
|
|
|
if (_componentCounts[e] <= 0)
|
|
|
|
|
{
|
|
|
|
|
DelEntity(e);
|
|
|
|
|
}
|
2023-04-26 16:45:37 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-14 01:04:22 +08:00
|
|
|
|
//public void Densify()
|
|
|
|
|
//{
|
|
|
|
|
// _entityDispenser.Sort();
|
|
|
|
|
//}
|
|
|
|
|
#endregion
|
2023-04-26 16:45:37 +08:00
|
|
|
|
|
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))
|
2023-06-29 00:56:26 +08:00
|
|
|
|
pool.Copy(fromEntityID, toEntityID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public void CopyEntity(int fromEntityID, EcsWorld toWorld, int toEntityID)
|
|
|
|
|
{
|
|
|
|
|
foreach (var pool in _pools)
|
|
|
|
|
{
|
|
|
|
|
if (pool.Has(fromEntityID))
|
|
|
|
|
pool.Copy(fromEntityID, toWorld, toEntityID);
|
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;
|
|
|
|
|
}
|
2023-06-29 00:56:26 +08:00
|
|
|
|
public int CloneEntity(int fromEntityID, EcsWorld toWorld)
|
|
|
|
|
{
|
2023-12-20 19:06:17 +08:00
|
|
|
|
int newEntity = NewEntity();
|
2023-06-29 00:56:26 +08:00
|
|
|
|
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))
|
|
|
|
|
pool.Del(toEntityID);
|
2023-05-23 01:47:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-29 00:56:26 +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
|
|
|
|
|
|
2023-12-31 13:07:53 +08:00
|
|
|
|
#region DelEntBuffer
|
2023-12-31 22:41:48 +08:00
|
|
|
|
public AutoReleaseDelEntBufferLonkUnloker DisableAutoReleaseDelEntBuffer()
|
|
|
|
|
{
|
2024-01-29 01:10:52 +08:00
|
|
|
|
_isEnableAutoReleaseDelEntBuffer = false;
|
2023-12-31 22:41:48 +08:00
|
|
|
|
return new AutoReleaseDelEntBufferLonkUnloker(this);
|
|
|
|
|
}
|
|
|
|
|
public void EnableAutoReleaseDelEntBuffer()
|
|
|
|
|
{
|
2024-01-29 01:10:52 +08:00
|
|
|
|
_isEnableAutoReleaseDelEntBuffer = true;
|
2023-12-31 22:41:48 +08:00
|
|
|
|
}
|
|
|
|
|
public readonly struct AutoReleaseDelEntBufferLonkUnloker : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private readonly EcsWorld _source;
|
|
|
|
|
public AutoReleaseDelEntBufferLonkUnloker(EcsWorld source)
|
|
|
|
|
{
|
|
|
|
|
_source = source;
|
|
|
|
|
}
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_source.EnableAutoReleaseDelEntBuffer();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-31 13:07:53 +08:00
|
|
|
|
public void ReleaseDelEntityBufferAll()
|
|
|
|
|
{
|
2024-02-10 20:54:09 +08:00
|
|
|
|
ReleaseDelEntityBuffer(_delEntBufferCount);
|
2023-12-31 13:07:53 +08:00
|
|
|
|
}
|
|
|
|
|
public void ReleaseDelEntityBuffer(int count)
|
|
|
|
|
{
|
|
|
|
|
if (_delEntBufferCount <= 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-02-11 01:16:47 +08:00
|
|
|
|
unchecked { _version++; }
|
2024-02-10 20:54:09 +08:00
|
|
|
|
count = Math.Clamp(count, 0, _delEntBufferCount);
|
2023-12-31 13:07:53 +08:00
|
|
|
|
_delEntBufferCount -= count;
|
|
|
|
|
ReadOnlySpan<int> buffser = new ReadOnlySpan<int>(_delEntBuffer, _delEntBufferCount, count);
|
|
|
|
|
for (int i = 0; i < _poolsCount; i++)
|
|
|
|
|
{
|
|
|
|
|
_pools[i].OnReleaseDelEntityBuffer(buffser);
|
|
|
|
|
}
|
|
|
|
|
_listeners.InvokeOnReleaseDelEntityBuffer(buffser);
|
|
|
|
|
for (int i = 0; i < buffser.Length; i++)
|
|
|
|
|
{
|
2024-02-11 19:59:56 +08:00
|
|
|
|
int e = buffser[i];
|
|
|
|
|
_entityDispenser.Release(e);
|
2024-02-11 20:23:11 +08:00
|
|
|
|
unchecked { _gens[e]++; }//up gen
|
|
|
|
|
_gens[e] |= DEATH_GEN_BIT;
|
2023-12-31 13:07:53 +08:00
|
|
|
|
}
|
2024-02-14 17:05:41 +08:00
|
|
|
|
Densify();
|
|
|
|
|
}
|
|
|
|
|
private void Densify() //уплотнение свободных айдишников
|
|
|
|
|
{
|
|
|
|
|
_entityDispenser.Sort();
|
2023-12-31 13:07:53 +08:00
|
|
|
|
}
|
|
|
|
|
#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-07 22:16:41 +08:00
|
|
|
|
Upsize_Internal(ArrayUtility.NormalizeSizeToPowerOfTwo(minSize));
|
|
|
|
|
}
|
|
|
|
|
[MethodImpl(MethodImplOptions.NoInlining)]
|
|
|
|
|
private void Upsize_Internal(int newSize)
|
|
|
|
|
{
|
2024-02-03 01:12:53 +08:00
|
|
|
|
Array.Resize(ref _gens, newSize);
|
|
|
|
|
Array.Resize(ref _componentCounts, newSize);
|
|
|
|
|
Array.Resize(ref _delEntBuffer, newSize);
|
|
|
|
|
Array.Resize(ref _entitiesComponentMasks, newSize);
|
|
|
|
|
for (int i = _entitesCapacity; i < newSize; i++)
|
2023-11-22 20:28:11 +08:00
|
|
|
|
_entitiesComponentMasks[i] = new int[_pools.Length / 32 + 1];
|
|
|
|
|
|
|
|
|
|
ArrayUtility.Fill(_gens, DEATH_GEN_BIT, _entitesCapacity);
|
2024-02-03 01:12:53 +08:00
|
|
|
|
_entitesCapacity = newSize;
|
2023-11-22 20:28:11 +08:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < _groups.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (_groups[i].TryGetTarget(out EcsGroup group))
|
|
|
|
|
{
|
2024-02-03 01:12:53 +08:00
|
|
|
|
group.OnWorldResize(newSize);
|
2023-11-22 20:28:11 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int last = _groups.Count - 1;
|
|
|
|
|
_groups[i--] = _groups[last];
|
|
|
|
|
_groups.RemoveAt(last);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
foreach (var item in _pools)
|
2024-02-03 01:12:53 +08:00
|
|
|
|
{
|
|
|
|
|
item.OnWorldResize(newSize);
|
|
|
|
|
}
|
2023-11-22 20:28:11 +08:00
|
|
|
|
|
2024-02-03 01:12:53 +08:00
|
|
|
|
_listeners.InvokeOnWorldResize(newSize);
|
2023-11-22 20:28:11 +08:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2023-05-28 06:29:04 +08:00
|
|
|
|
#region Groups Pool
|
2023-04-20 20:03:26 +08:00
|
|
|
|
internal void RegisterGroup(EcsGroup group)
|
2023-04-07 05:08:48 +08:00
|
|
|
|
{
|
2023-04-17 22:58:52 +08:00
|
|
|
|
_groups.Add(new WeakReference<EcsGroup>(group));
|
2023-04-07 05:08:48 +08:00
|
|
|
|
}
|
2023-06-01 20:14:34 +08:00
|
|
|
|
internal EcsGroup GetFreeGroup()
|
2023-04-18 19:35:42 +08:00
|
|
|
|
{
|
2023-05-28 06:29:04 +08:00
|
|
|
|
EcsGroup result = _groupsPool.Count <= 0 ? new EcsGroup(this) : _groupsPool.Pop();
|
|
|
|
|
result._isReleased = false;
|
|
|
|
|
return result;
|
2023-04-18 19:35:42 +08:00
|
|
|
|
}
|
2023-04-20 20:03:26 +08:00
|
|
|
|
internal void ReleaseGroup(EcsGroup group)
|
2023-04-18 19:35:42 +08:00
|
|
|
|
{
|
2023-05-26 05:13:11 +08:00
|
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
2023-06-26 01:57:50 +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);
|
|
|
|
|
}
|
2023-04-07 05:08:48 +08:00
|
|
|
|
#endregion
|
2023-05-07 00:50:02 +08:00
|
|
|
|
|
2023-06-08 04:04:39 +08:00
|
|
|
|
#region Listeners
|
2023-06-19 02:10:25 +08:00
|
|
|
|
public void AddListener(IEcsWorldEventListener worldEventListener)
|
2023-06-08 04:04:39 +08:00
|
|
|
|
{
|
2023-06-19 02:10:25 +08:00
|
|
|
|
_listeners.Add(worldEventListener);
|
2023-06-08 04:04:39 +08:00
|
|
|
|
}
|
2023-06-19 02:10:25 +08:00
|
|
|
|
public void RemoveListener(IEcsWorldEventListener worldEventListener)
|
2023-06-08 04:04:39 +08:00
|
|
|
|
{
|
2023-06-19 02:10:25 +08:00
|
|
|
|
_listeners.Remove(worldEventListener);
|
2023-06-08 04:04:39 +08:00
|
|
|
|
}
|
2023-06-19 02:10:25 +08:00
|
|
|
|
public void AddListener(IEcsEntityEventListener entityEventListener)
|
2023-06-10 18:58:43 +08:00
|
|
|
|
{
|
2023-06-19 02:10:25 +08:00
|
|
|
|
_entityListeners.Add(entityEventListener);
|
2023-06-10 18:58:43 +08:00
|
|
|
|
}
|
2023-06-19 02:10:25 +08:00
|
|
|
|
public void RemoveListener(IEcsEntityEventListener entityEventListener)
|
2023-06-10 18:58:43 +08:00
|
|
|
|
{
|
2023-06-19 02:10:25 +08:00
|
|
|
|
_entityListeners.Remove(entityEventListener);
|
2023-06-10 18:58:43 +08:00
|
|
|
|
}
|
2023-06-08 04:04:39 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
2023-05-07 00:50:02 +08:00
|
|
|
|
#region Debug
|
2023-05-23 01:47:28 +08:00
|
|
|
|
public void GetComponents(int entityID, List<object> list)
|
|
|
|
|
{
|
|
|
|
|
list.Clear();
|
|
|
|
|
var itemsCount = GetComponentsCount(entityID);
|
2023-12-31 21:03:00 +08:00
|
|
|
|
for (var i = 0; i < _poolsCount; i++)
|
2023-05-23 01:47:28 +08:00
|
|
|
|
{
|
2023-05-26 00:24:38 +08:00
|
|
|
|
if (_pools[i].Has(entityID))
|
2023-12-31 21:03:00 +08:00
|
|
|
|
{
|
|
|
|
|
itemsCount--;
|
2023-05-30 18:30:10 +08:00
|
|
|
|
list.Add(_pools[i].GetRaw(entityID));
|
2023-12-31 21:03:00 +08:00
|
|
|
|
if (itemsCount <= 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public void GetComponentTypes(int entityID, HashSet<Type> typeSet)
|
|
|
|
|
{
|
|
|
|
|
typeSet.Clear();
|
|
|
|
|
var itemsCount = GetComponentsCount(entityID);
|
|
|
|
|
for (var i = 0; i < _poolsCount; i++)
|
|
|
|
|
{
|
|
|
|
|
if (_pools[i].Has(entityID))
|
|
|
|
|
{
|
|
|
|
|
itemsCount--;
|
|
|
|
|
typeSet.Add(_pools[i].ComponentType);
|
|
|
|
|
if (itemsCount <= 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-05-23 01:47:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-07 00:50:02 +08:00
|
|
|
|
#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
|
2023-05-23 15:58:31 +08:00
|
|
|
|
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
|
|
|
|
|
{
|
2023-05-23 15:58:31 +08:00
|
|
|
|
void OnNewEntity(int entityID);
|
|
|
|
|
void OnDelEntity(int entityID);
|
|
|
|
|
}
|
2023-05-26 00:24:38 +08:00
|
|
|
|
internal static class WorldEventListExtensions
|
2023-05-23 15:58:31 +08:00
|
|
|
|
{
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static void InvokeOnWorldResize(this List<IEcsWorldEventListener> self, int newSize)
|
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
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)
|
2023-05-23 15:58:31 +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)
|
2023-05-23 15:58:31 +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)]
|
|
|
|
|
public static entlong ToEntityLong(this int self, EcsWorld world) => world.GetEntityLong(self);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2024-02-03 01:12:53 +08:00
|
|
|
|
}
|