2023-04-20 10:59:55 +08:00
|
|
|
|
using System;
|
2023-03-02 14:42:44 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
2023-05-26 03:45:35 +08:00
|
|
|
|
using DCFApixels.DragonECS.Internal;
|
2023-03-02 14:42:44 +08:00
|
|
|
|
|
|
|
|
|
namespace DCFApixels.DragonECS
|
|
|
|
|
{
|
2023-04-24 00:31:03 +08:00
|
|
|
|
public abstract class EcsWorld
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-04-23 17:19:52 +08:00
|
|
|
|
private const short GEN_BITS = 0x7fff;
|
|
|
|
|
private const short DEATH_GEN_BIT = short.MinValue;
|
2023-04-24 16:48:18 +08:00
|
|
|
|
private const int DEL_ENT_BUFFER_SIZE_OFFSET = 2;
|
2023-04-23 17:19:52 +08:00
|
|
|
|
|
2023-04-20 20:03:26 +08:00
|
|
|
|
public static EcsWorld[] Worlds = new EcsWorld[8];
|
2023-04-07 15:11:48 +08:00
|
|
|
|
private static IntDispenser _worldIdDispenser = new IntDispenser(0);
|
2023-05-26 03:45:35 +08:00
|
|
|
|
|
2023-04-18 19:35:42 +08:00
|
|
|
|
public readonly short uniqueID;
|
2023-04-20 20:03:26 +08:00
|
|
|
|
|
2023-05-07 00:50:02 +08:00
|
|
|
|
private int _worldTypeID;
|
2023-04-18 19:35:42 +08:00
|
|
|
|
|
2023-03-11 17:11:40 +08:00
|
|
|
|
private IntDispenser _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
|
|
|
|
private EcsGroup _allEntites;
|
|
|
|
|
|
2023-05-26 00:24:38 +08:00
|
|
|
|
//буфер удаления откладывает освобождение андишников сущностей.
|
|
|
|
|
//Нужен для того чтобы запускать некоторые процесыы связанные с удалением сущности не по одному при каждом удалении, а пачкой
|
2023-04-24 00:19:07 +08:00
|
|
|
|
//В теории такой подход частично улучшает ситуацию с переполнением поколений
|
2023-04-20 18:23:23 +08:00
|
|
|
|
private int[] _delEntBuffer;
|
2023-04-20 11:37:27 +08:00
|
|
|
|
private int _delEntBufferCount;
|
2023-04-18 19:35:42 +08:00
|
|
|
|
|
2023-05-26 00:24:38 +08:00
|
|
|
|
internal IEcsPoolImplementation[] _pools;
|
2023-03-13 04:32:24 +08:00
|
|
|
|
private EcsNullPool _nullPool;
|
2023-05-23 01:47:28 +08:00
|
|
|
|
private int _poolsCount = 0;
|
2023-03-02 14:42:44 +08:00
|
|
|
|
|
2023-05-07 00:50:02 +08:00
|
|
|
|
private EcsSubject[] _subjects;
|
|
|
|
|
private EcsQueryExecutor[] _executors;
|
2023-03-02 14:42:44 +08:00
|
|
|
|
|
2023-04-17 22:58:52 +08:00
|
|
|
|
private List<WeakReference<EcsGroup>> _groups;
|
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-05-23 15:58:31 +08:00
|
|
|
|
private List<IEcsWorldEventListener> _listeners;
|
2023-04-01 20:45:37 +08:00
|
|
|
|
|
2023-03-02 14:42:44 +08:00
|
|
|
|
#region Properties
|
2023-04-20 20:03:26 +08:00
|
|
|
|
public abstract Type Archetype { get; }
|
2023-04-18 19:35:42 +08:00
|
|
|
|
public int UniqueID => uniqueID;
|
|
|
|
|
public int Count => _entitiesCount;
|
|
|
|
|
public int Capacity => _entitesCapacity; //_denseEntities.Length;
|
2023-04-09 02:52:39 +08:00
|
|
|
|
public EcsReadonlyGroup Entities => _allEntites.Readonly;
|
2023-05-26 00:24:38 +08:00
|
|
|
|
public ReadOnlySpan<IEcsPoolImplementation> AllPools => _pools;// new ReadOnlySpan<IEcsPoolImplementation>(pools, 0, _poolsCount);
|
2023-05-23 01:47:28 +08:00
|
|
|
|
public int PoolsCount => _poolsCount;
|
2023-04-23 22:55:13 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
2023-04-20 20:10:16 +08:00
|
|
|
|
#region Constructors/Destroy
|
2023-05-07 00:50:02 +08:00
|
|
|
|
static EcsWorld()
|
|
|
|
|
{
|
|
|
|
|
EcsNullWorld nullWorld = new EcsNullWorld();
|
|
|
|
|
Worlds[0] = nullWorld;
|
|
|
|
|
}
|
2023-05-23 15:58:31 +08:00
|
|
|
|
public EcsWorld() : this(true) { }
|
|
|
|
|
internal EcsWorld(bool isIndexable)
|
2023-04-20 20:03:26 +08:00
|
|
|
|
{
|
2023-04-23 17:19:52 +08:00
|
|
|
|
_entitesCapacity = 512;
|
|
|
|
|
|
2023-05-23 15:58:31 +08:00
|
|
|
|
_listeners = new List<IEcsWorldEventListener>();
|
|
|
|
|
|
2023-05-07 00:50:02 +08:00
|
|
|
|
if (isIndexable)
|
|
|
|
|
{
|
|
|
|
|
uniqueID = (short)_worldIdDispenser.GetFree();
|
|
|
|
|
if (uniqueID >= Worlds.Length)
|
|
|
|
|
Array.Resize(ref Worlds, Worlds.Length << 1);
|
|
|
|
|
Worlds[uniqueID] = this;
|
|
|
|
|
}
|
2023-04-20 20:03:26 +08:00
|
|
|
|
|
2023-05-07 00:50:02 +08:00
|
|
|
|
_worldTypeID = WorldMetaStorage.GetWorldId(Archetype);
|
2023-04-20 20:03:26 +08:00
|
|
|
|
|
2023-04-07 15:11:48 +08:00
|
|
|
|
_entityDispenser = new IntDispenser(0);
|
2023-04-06 23:40:47 +08:00
|
|
|
|
_nullPool = EcsNullPool.instance;
|
2023-05-26 00:24:38 +08:00
|
|
|
|
_pools = new IEcsPoolImplementation[512];
|
|
|
|
|
ArrayUtility.Fill(_pools, _nullPool);
|
2023-04-01 20:45:37 +08:00
|
|
|
|
|
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-04-23 17:19:52 +08:00
|
|
|
|
_delEntBuffer = new int[_entitesCapacity >> DEL_ENT_BUFFER_SIZE_OFFSET];
|
2023-04-18 19:35:42 +08:00
|
|
|
|
|
2023-04-17 22:58:52 +08:00
|
|
|
|
_groups = new List<WeakReference<EcsGroup>>();
|
2023-04-20 10:59:55 +08:00
|
|
|
|
_allEntites = GetGroupFromPool();
|
|
|
|
|
|
2023-05-07 00:50:02 +08:00
|
|
|
|
_subjects = new EcsSubject[128];
|
|
|
|
|
_executors = new EcsQueryExecutor[128];
|
2023-03-02 14:42:44 +08:00
|
|
|
|
}
|
2023-05-07 00:50:02 +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;
|
|
|
|
|
//_denseEntities = null;
|
|
|
|
|
_gens = null;
|
2023-05-26 00:24:38 +08:00
|
|
|
|
_pools = null;
|
2023-04-20 20:10:16 +08:00
|
|
|
|
_nullPool = null;
|
2023-05-07 00:50:02 +08:00
|
|
|
|
_subjects = null;
|
|
|
|
|
_executors = null;
|
2023-04-20 20:10:16 +08:00
|
|
|
|
|
2023-04-20 20:03:26 +08:00
|
|
|
|
Worlds[uniqueID] = null;
|
|
|
|
|
_worldIdDispenser.Release(uniqueID);
|
|
|
|
|
}
|
2023-04-20 20:10:16 +08:00
|
|
|
|
public void DestryWithPipeline()
|
|
|
|
|
{
|
|
|
|
|
Destroy();
|
|
|
|
|
}
|
2023-03-02 14:42:44 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
2023-04-24 16:48:18 +08:00
|
|
|
|
#region GetComponentID
|
2023-05-07 00:50:02 +08:00
|
|
|
|
public int GetComponentID<T>() => WorldMetaStorage.GetComponentId<T>(_worldTypeID);////ComponentType<TWorldArchetype>.uniqueID;
|
2023-04-24 16:48:18 +08:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2023-03-02 14:42:44 +08:00
|
|
|
|
#region GetPool
|
2023-05-07 00:50:02 +08:00
|
|
|
|
public TPool GetPool<TComponent, TPool>() where TComponent : struct where TPool : IEcsPoolImplementation<TComponent>, new()
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-05-07 00:50:02 +08:00
|
|
|
|
int uniqueID = WorldMetaStorage.GetComponentId<TComponent>(_worldTypeID);
|
2023-03-02 14:42:44 +08:00
|
|
|
|
|
2023-05-26 00:24:38 +08:00
|
|
|
|
if (uniqueID >= _pools.Length)
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-05-26 00:24:38 +08:00
|
|
|
|
int oldCapacity = _pools.Length;
|
|
|
|
|
Array.Resize(ref _pools, _pools.Length << 1);
|
|
|
|
|
ArrayUtility.Fill(_pools, _nullPool, oldCapacity, oldCapacity - _pools.Length);
|
2023-03-02 14:42:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-26 00:24:38 +08:00
|
|
|
|
if (_pools[uniqueID] == _nullPool)
|
2023-04-21 03:16:05 +08:00
|
|
|
|
{
|
|
|
|
|
var pool = new TPool();
|
2023-05-26 00:24:38 +08:00
|
|
|
|
_pools[uniqueID] = pool;
|
2023-05-07 00:50:02 +08:00
|
|
|
|
pool.OnInit(this, uniqueID);
|
2023-05-23 01:47:28 +08:00
|
|
|
|
_poolsCount++;
|
2023-04-21 03:16:05 +08:00
|
|
|
|
//EcsDebug.Print(pool.GetType().FullName);
|
|
|
|
|
}
|
2023-04-20 18:23:23 +08:00
|
|
|
|
|
2023-05-26 00:24:38 +08:00
|
|
|
|
return (TPool)_pools[uniqueID];
|
2023-03-02 14:42:44 +08:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2023-04-20 11:37:27 +08:00
|
|
|
|
#region Queries
|
2023-05-07 00:50:02 +08:00
|
|
|
|
public TSubject GetSubject<TSubject>() where TSubject : EcsSubject
|
|
|
|
|
{
|
|
|
|
|
int uniqueID = WorldMetaStorage.GetSubjectId<TSubject>(_worldTypeID);
|
|
|
|
|
if (uniqueID >= _subjects.Length)
|
|
|
|
|
Array.Resize(ref _subjects, _subjects.Length << 1);
|
|
|
|
|
if (_subjects[uniqueID] == null)
|
|
|
|
|
_subjects[uniqueID] = EcsSubject.Builder.Build<TSubject>(this);
|
|
|
|
|
return (TSubject)_subjects[uniqueID];
|
|
|
|
|
}
|
|
|
|
|
#region Iterate
|
|
|
|
|
public EcsSubjectIterator<TSubject> IterateFor<TSubject>(EcsReadonlyGroup sourceGroup, out TSubject subject) where TSubject : EcsSubject
|
2023-03-12 20:45:18 +08:00
|
|
|
|
{
|
2023-05-07 00:50:02 +08:00
|
|
|
|
|
|
|
|
|
subject = GetSubject<TSubject>();
|
|
|
|
|
return subject.GetIteratorFor(sourceGroup);
|
|
|
|
|
}
|
|
|
|
|
public EcsSubjectIterator<TSubject> IterateFor<TSubject>(EcsReadonlyGroup sourceGroup) where TSubject : EcsSubject
|
|
|
|
|
{
|
|
|
|
|
return GetSubject<TSubject>().GetIteratorFor(sourceGroup);
|
2023-04-07 05:08:48 +08:00
|
|
|
|
}
|
2023-05-07 00:50:02 +08:00
|
|
|
|
public EcsSubjectIterator<TSubject> Iterate<TSubject>(out TSubject subject) where TSubject : EcsSubject
|
2023-04-24 16:48:18 +08:00
|
|
|
|
{
|
2023-05-07 00:50:02 +08:00
|
|
|
|
subject = GetSubject<TSubject>();
|
|
|
|
|
return subject.GetIterator();
|
2023-04-24 16:48:18 +08:00
|
|
|
|
}
|
2023-05-07 00:50:02 +08:00
|
|
|
|
public EcsSubjectIterator<TSubject> Iterate<TSubject>() where TSubject : EcsSubject
|
2023-04-24 16:48:18 +08:00
|
|
|
|
{
|
2023-05-07 00:50:02 +08:00
|
|
|
|
return GetSubject<TSubject>().GetIterator();
|
2023-04-24 16:48:18 +08:00
|
|
|
|
}
|
2023-05-07 00:50:02 +08:00
|
|
|
|
#endregion
|
2023-04-24 16:48:18 +08:00
|
|
|
|
|
2023-05-07 00:50:02 +08:00
|
|
|
|
#region Where
|
|
|
|
|
private EcsWhereExecutor<TSubject> GetWhereExecutor<TSubject>() where TSubject : EcsSubject
|
|
|
|
|
{
|
|
|
|
|
int id = WorldMetaStorage.GetExecutorId<EcsWhereExecutor<TSubject>>(_worldTypeID);
|
|
|
|
|
if (id >= _executors.Length)
|
|
|
|
|
Array.Resize(ref _executors, _executors.Length << 1);
|
|
|
|
|
if (_executors[id] == null)
|
|
|
|
|
_executors[id] = new EcsWhereExecutor<TSubject>(GetSubject<TSubject>());
|
|
|
|
|
return (EcsWhereExecutor<TSubject>)_executors[id];
|
|
|
|
|
}
|
|
|
|
|
public EcsWhereResult<TSubject> WhereFor<TSubject>(EcsReadonlyGroup sourceGroup, out TSubject subject) where TSubject : EcsSubject
|
|
|
|
|
{
|
|
|
|
|
var executor = GetWhereExecutor<TSubject>();
|
|
|
|
|
subject = executor.Subject;
|
|
|
|
|
return executor.ExecuteFor(sourceGroup);
|
|
|
|
|
}
|
|
|
|
|
public EcsWhereResult<TSubject> WhereFor<TSubject>(EcsReadonlyGroup sourceGroup) where TSubject : EcsSubject
|
|
|
|
|
{
|
|
|
|
|
return GetWhereExecutor<TSubject>().ExecuteFor(sourceGroup);
|
|
|
|
|
}
|
|
|
|
|
public EcsWhereResult<TSubject> Where<TSubject>(out TSubject subject) where TSubject : EcsSubject
|
|
|
|
|
{
|
|
|
|
|
var executor = GetWhereExecutor<TSubject>();
|
|
|
|
|
subject = executor.Subject;
|
|
|
|
|
return executor.Execute();
|
|
|
|
|
}
|
|
|
|
|
public EcsWhereResult<TSubject> Where<TSubject>() where TSubject : EcsSubject
|
|
|
|
|
{
|
|
|
|
|
return GetWhereExecutor<TSubject>().Execute();
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Join
|
|
|
|
|
private EcsJoinAttachExecutor<TSubject, TAttachComponent> GetJoinAttachExecutor<TSubject, TAttachComponent>()
|
|
|
|
|
where TSubject : EcsSubject
|
|
|
|
|
where TAttachComponent : struct, IEcsAttachComponent
|
|
|
|
|
{
|
|
|
|
|
int id = WorldMetaStorage.GetExecutorId<EcsJoinAttachExecutor<TSubject, TAttachComponent>>(_worldTypeID);
|
|
|
|
|
if (id >= _executors.Length)
|
|
|
|
|
Array.Resize(ref _executors, _executors.Length << 1);
|
|
|
|
|
if (_executors[id] == null)
|
|
|
|
|
_executors[id] = new EcsJoinAttachExecutor<TSubject, TAttachComponent>(GetSubject<TSubject>());
|
|
|
|
|
return (EcsJoinAttachExecutor<TSubject, TAttachComponent>)_executors[id];
|
|
|
|
|
}
|
|
|
|
|
public EcsJoinAttachResult<TSubject, TAttachComponent> JoinFor<TSubject, TAttachComponent>(EcsReadonlyGroup sourceGroup, out TSubject subject)
|
|
|
|
|
where TSubject : EcsSubject
|
|
|
|
|
where TAttachComponent : struct, IEcsAttachComponent
|
|
|
|
|
{
|
|
|
|
|
var executor = GetJoinAttachExecutor<TSubject, TAttachComponent>();
|
|
|
|
|
subject = executor.Subject;
|
|
|
|
|
return executor.ExecuteFor(sourceGroup);
|
|
|
|
|
}
|
|
|
|
|
public EcsJoinAttachResult<TSubject, TAttachComponent> JoinFor<TSubject, TAttachComponent>(EcsReadonlyGroup sourceGroup)
|
|
|
|
|
where TSubject : EcsSubject
|
|
|
|
|
where TAttachComponent : struct, IEcsAttachComponent
|
|
|
|
|
{
|
|
|
|
|
return GetJoinAttachExecutor<TSubject, TAttachComponent>().ExecuteFor(sourceGroup);
|
|
|
|
|
}
|
|
|
|
|
public EcsJoinAttachResult<TSubject, TAttachComponent> Join<TSubject, TAttachComponent>(out TSubject subject)
|
|
|
|
|
where TSubject : EcsSubject
|
|
|
|
|
where TAttachComponent : struct, IEcsAttachComponent
|
2023-04-24 16:48:18 +08:00
|
|
|
|
{
|
2023-05-07 00:50:02 +08:00
|
|
|
|
var executor = GetJoinAttachExecutor<TSubject, TAttachComponent>();
|
|
|
|
|
subject = executor.Subject;
|
|
|
|
|
return executor.Execute();
|
2023-04-24 16:48:18 +08:00
|
|
|
|
}
|
2023-05-07 00:50:02 +08:00
|
|
|
|
public EcsJoinAttachResult<TSubject, TAttachComponent> Join<TSubject, TAttachComponent>()
|
|
|
|
|
where TSubject : EcsSubject
|
|
|
|
|
where TAttachComponent : struct, IEcsAttachComponent
|
2023-04-24 16:48:18 +08:00
|
|
|
|
{
|
2023-05-07 00:50:02 +08:00
|
|
|
|
return GetJoinAttachExecutor<TSubject, TAttachComponent>().Execute();
|
2023-04-24 16:48:18 +08:00
|
|
|
|
}
|
2023-03-02 14:42:44 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
2023-05-07 00:50:02 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
2023-05-26 00:24:38 +08:00
|
|
|
|
#region IsMatchesMask
|
|
|
|
|
public bool IsMatchesMask(EcsMask mask, int entityID)
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-05-07 00:50:02 +08:00
|
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS
|
2023-05-26 00:24:38 +08:00
|
|
|
|
if (mask._worldType != Archetype)
|
2023-04-08 00:47:35 +08:00
|
|
|
|
throw new EcsFrameworkException("mask.WorldArchetypeType != typeof(TTableArhetype)");
|
2023-03-13 01:39:04 +08:00
|
|
|
|
#endif
|
2023-05-26 00:24:38 +08:00
|
|
|
|
for (int i = 0, iMax = mask._inc.Length; i < iMax; i++)
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-05-26 00:24:38 +08:00
|
|
|
|
if (!_pools[mask._inc[i]].Has(entityID))
|
2023-03-02 14:42:44 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-05-26 00:24:38 +08:00
|
|
|
|
for (int i = 0, iMax = mask._exc.Length; i < iMax; i++)
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-05-26 00:24:38 +08:00
|
|
|
|
if (_pools[mask._exc[i]].Has(entityID))
|
2023-03-02 14:42:44 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2023-03-26 11:19:03 +08:00
|
|
|
|
#region Entity
|
2023-05-23 01:47:28 +08:00
|
|
|
|
public int NewEmptyEntity()
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-03-26 11:19:03 +08:00
|
|
|
|
int entityID = _entityDispenser.GetFree();
|
2023-04-18 19:35:42 +08:00
|
|
|
|
_entitiesCount++;
|
2023-04-01 22:18:40 +08:00
|
|
|
|
|
2023-04-01 20:45:37 +08:00
|
|
|
|
if (_gens.Length <= entityID)
|
|
|
|
|
{
|
2023-03-26 11:19:03 +08:00
|
|
|
|
Array.Resize(ref _gens, _gens.Length << 1);
|
2023-04-26 16:45:37 +08:00
|
|
|
|
Array.Resize(ref _componentCounts, _gens.Length);
|
2023-04-23 17:19:52 +08:00
|
|
|
|
ArrayUtility.Fill(_gens, DEATH_GEN_BIT, _entitesCapacity);
|
2023-04-18 19:35:42 +08:00
|
|
|
|
_entitesCapacity = _gens.Length;
|
2023-04-23 17:19:52 +08:00
|
|
|
|
|
2023-04-17 22:58:52 +08:00
|
|
|
|
for (int i = 0; i < _groups.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (_groups[i].TryGetTarget(out EcsGroup group))
|
|
|
|
|
{
|
|
|
|
|
group.OnWorldResize(_gens.Length);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int last = _groups.Count - 1;
|
|
|
|
|
_groups[i--] = _groups[last];
|
|
|
|
|
_groups.RemoveAt(last);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-26 00:24:38 +08:00
|
|
|
|
foreach (var item in _pools)
|
2023-05-07 00:50:02 +08:00
|
|
|
|
item.OnWorldResize(_gens.Length);
|
2023-05-23 01:47:28 +08:00
|
|
|
|
|
2023-05-23 15:58:31 +08:00
|
|
|
|
_listeners.InvokeOnWorldResize(_gens.Length);
|
2023-04-01 20:45:37 +08:00
|
|
|
|
}
|
2023-04-23 17:19:52 +08:00
|
|
|
|
_gens[entityID] &= GEN_BITS;
|
2023-04-09 02:52:39 +08:00
|
|
|
|
_allEntites.Add(entityID);
|
2023-05-23 15:58:31 +08:00
|
|
|
|
_listeners.InvokeOnNewEntity(entityID);
|
2023-05-07 00:50:02 +08:00
|
|
|
|
return entityID;
|
|
|
|
|
}
|
2023-05-23 01:47:28 +08:00
|
|
|
|
public entlong NewEmptyEntityLong()
|
2023-05-07 00:50:02 +08:00
|
|
|
|
{
|
2023-05-23 01:47:28 +08:00
|
|
|
|
int e = NewEmptyEntity();
|
2023-05-07 00:50:02 +08:00
|
|
|
|
return GetEntityLong(e);
|
2023-03-26 11:19:03 +08:00
|
|
|
|
}
|
2023-04-26 16:45:37 +08:00
|
|
|
|
|
|
|
|
|
public void DelEntity(int entityID)
|
2023-03-26 11:19:03 +08:00
|
|
|
|
{
|
2023-04-26 16:45:37 +08:00
|
|
|
|
_allEntites.Remove(entityID);
|
|
|
|
|
_delEntBuffer[_delEntBufferCount++] = entityID;
|
|
|
|
|
_gens[entityID] |= DEATH_GEN_BIT;
|
2023-04-01 22:18:40 +08:00
|
|
|
|
_entitiesCount--;
|
2023-05-23 15:58:31 +08:00
|
|
|
|
_listeners.InvokeOnDelEntity(entityID);
|
2023-04-20 11:37:27 +08:00
|
|
|
|
|
2023-04-20 20:03:26 +08:00
|
|
|
|
if (_delEntBufferCount >= _delEntBuffer.Length)
|
2023-04-24 16:48:18 +08:00
|
|
|
|
ReleaseDelEntityBuffer();
|
2023-04-20 11:37:27 +08:00
|
|
|
|
}
|
2023-04-24 16:48:18 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-05-07 00:50:02 +08:00
|
|
|
|
public entlong GetEntityLong(int entityID) => new entlong(entityID, _gens[entityID], uniqueID); //TODO придумать получше имя метода
|
2023-04-24 16:48:18 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public bool IsAlive(int entityID, short gen) => _gens[entityID] == gen;
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-05-07 00:50:02 +08:00
|
|
|
|
public bool IsUsed(int entityID) => _gens[entityID] >= 0;
|
2023-04-24 16:48:18 +08:00
|
|
|
|
public void ReleaseDelEntityBuffer()
|
2023-04-20 11:37:27 +08:00
|
|
|
|
{
|
2023-05-07 00:50:02 +08:00
|
|
|
|
ReadOnlySpan<int> buffser = new ReadOnlySpan<int>(_delEntBuffer, 0, _delEntBufferCount);
|
2023-05-26 00:24:38 +08:00
|
|
|
|
foreach (var pool in _pools)
|
2023-05-07 00:50:02 +08:00
|
|
|
|
pool.OnReleaseDelEntityBuffer(buffser);
|
2023-05-23 15:58:31 +08:00
|
|
|
|
_listeners.InvokeOnReleaseDelEntityBuffer(buffser);
|
2023-04-20 11:37:27 +08:00
|
|
|
|
for (int i = 0; i < _delEntBufferCount; i++)
|
|
|
|
|
_entityDispenser.Release(_delEntBuffer[i]);
|
|
|
|
|
_delEntBufferCount = 0;
|
2023-03-02 14:42:44 +08:00
|
|
|
|
}
|
2023-05-26 00:24:38 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-04-26 16:45:37 +08:00
|
|
|
|
public short GetGen(int entityID) => _gens[entityID];
|
2023-05-26 00:24:38 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-05-23 01:47:28 +08:00
|
|
|
|
public short GetComponentsCount(int entityID) => _componentCounts[entityID];
|
2023-04-26 16:45:37 +08:00
|
|
|
|
public void DeleteEmptyEntites()
|
|
|
|
|
{
|
|
|
|
|
foreach (var e in _allEntites)
|
|
|
|
|
{
|
2023-05-26 00:24:38 +08:00
|
|
|
|
if (_componentCounts[e] <= 0) DelEntity(e);
|
2023-04-26 16:45:37 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-05-26 00:24:38 +08:00
|
|
|
|
if(pool.Has(fromEntityID)) pool.Copy(fromEntityID, toEntityID);
|
2023-05-23 01:47:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public int CloneEntity(int fromEntityID)
|
|
|
|
|
{
|
|
|
|
|
int newEntity = NewEmptyEntity();
|
|
|
|
|
CopyEntity(fromEntityID, newEntity);
|
|
|
|
|
return newEntity;
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
if (!pool.Has(fromEntityID)&& pool.Has(toEntityID))
|
|
|
|
|
pool.Del(toEntityID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-26 16:45:37 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-05-26 00:24:38 +08:00
|
|
|
|
internal void IncrementEntityComponentCount(int entityID) => _componentCounts[entityID]++;
|
2023-04-26 16:45:37 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
internal void DecrementEntityComponentCount(int entityID)
|
|
|
|
|
{
|
|
|
|
|
var count = --_componentCounts[entityID];
|
2023-05-26 00:24:38 +08:00
|
|
|
|
if(count == 0 && _allEntites.Has(entityID)) DelEntity(entityID);
|
2023-05-07 00:50:02 +08:00
|
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS
|
2023-05-26 00:24:38 +08:00
|
|
|
|
if (count < 0) throw new EcsFrameworkException("нарушен баланс инкремента/декремента компонентов");
|
2023-04-26 16:45:37 +08:00
|
|
|
|
#endif
|
|
|
|
|
}
|
2023-03-02 14:42:44 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
2023-04-18 19:35:42 +08:00
|
|
|
|
#region Groups
|
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-04-18 19:35:42 +08:00
|
|
|
|
internal EcsGroup GetGroupFromPool()
|
|
|
|
|
{
|
2023-05-26 00:24:38 +08:00
|
|
|
|
if (_groupsPool.Count <= 0) return new EcsGroup(this);
|
2023-04-18 19:35:42 +08:00
|
|
|
|
return _groupsPool.Pop();
|
|
|
|
|
}
|
2023-04-20 20:03:26 +08:00
|
|
|
|
internal void ReleaseGroup(EcsGroup group)
|
2023-04-18 19:35:42 +08:00
|
|
|
|
{
|
|
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS
|
|
|
|
|
if (group.World != this)
|
2023-04-20 18:23:23 +08:00
|
|
|
|
throw new ArgumentException("groupFilter.WorldIndex != this");
|
2023-04-18 19:35:42 +08:00
|
|
|
|
#endif
|
|
|
|
|
group.Clear();
|
|
|
|
|
_groupsPool.Push(group);
|
|
|
|
|
}
|
2023-04-07 05:08:48 +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);
|
|
|
|
|
if (itemsCount == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2023-05-26 00:24:38 +08:00
|
|
|
|
for (var i = 0; i < _pools.Length; i++)
|
2023-05-23 01:47:28 +08:00
|
|
|
|
{
|
2023-05-26 00:24:38 +08:00
|
|
|
|
if (_pools[i].Has(entityID))
|
|
|
|
|
list.Add(_pools[i].GetRaw(entityID));
|
2023-05-23 01:47:28 +08:00
|
|
|
|
if (list.Count >= itemsCount)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// public int GetComponents(int entity, ref object[] list)
|
|
|
|
|
// {
|
|
|
|
|
// var entityOffset = GetRawEntityOffset(entity);
|
|
|
|
|
// var itemsCount = _entities[entityOffset + RawEntityOffsets.ComponentsCount];
|
|
|
|
|
// if (itemsCount == 0) { return 0; }
|
|
|
|
|
// if (list == null || list.Length < itemsCount)
|
|
|
|
|
// {
|
|
|
|
|
// list = new object[_pools.Length];
|
|
|
|
|
// }
|
|
|
|
|
// var dataOffset = entityOffset + RawEntityOffsets.Components;
|
|
|
|
|
// for (var i = 0; i < itemsCount; i++)
|
|
|
|
|
// {
|
|
|
|
|
// list[i] = _pools[_entities[dataOffset + i]].GetRaw(entity);
|
|
|
|
|
// }
|
|
|
|
|
// return itemsCount;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// public int GetComponentTypes(int entity, ref Type[] list)
|
|
|
|
|
// {
|
|
|
|
|
// var entityOffset = GetRawEntityOffset(entity);
|
|
|
|
|
// var itemsCount = _entities[entityOffset + RawEntityOffsets.ComponentsCount];
|
|
|
|
|
// if (itemsCount == 0) { return 0; }
|
|
|
|
|
// if (list == null || list.Length < itemsCount)
|
|
|
|
|
// {
|
|
|
|
|
// list = new Type[_pools.Length];
|
|
|
|
|
// }
|
|
|
|
|
// var dataOffset = entityOffset + RawEntityOffsets.Components;
|
|
|
|
|
// for (var i = 0; i < itemsCount; i++)
|
|
|
|
|
// {
|
|
|
|
|
// list[i] = _pools[_entities[dataOffset + i]].GetComponentType();
|
|
|
|
|
// }
|
|
|
|
|
// return itemsCount;
|
|
|
|
|
// }
|
2023-05-07 00:50:02 +08:00
|
|
|
|
#endregion
|
2023-04-01 20:45:37 +08:00
|
|
|
|
}
|
2023-04-08 21:29:18 +08:00
|
|
|
|
|
2023-04-20 20:03:26 +08:00
|
|
|
|
public abstract class EcsWorld<TWorldArchetype> : EcsWorld
|
|
|
|
|
where TWorldArchetype : EcsWorld<TWorldArchetype>
|
|
|
|
|
{
|
|
|
|
|
public override Type Archetype => typeof(TWorldArchetype);
|
2023-05-23 15:58:31 +08:00
|
|
|
|
public EcsWorld() : base() { }
|
|
|
|
|
internal EcsWorld(bool isIndexable) : base(isIndexable) { }
|
2023-04-20 20:03:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 19:35:42 +08:00
|
|
|
|
#region Utils
|
2023-04-20 18:23:23 +08:00
|
|
|
|
public static class WorldMetaStorage
|
2023-04-10 22:22:17 +08:00
|
|
|
|
{
|
2023-05-26 00:24:38 +08:00
|
|
|
|
private static List<Resizer> _resizer = new List<Resizer>();
|
|
|
|
|
private static int _tokenCount = 0;
|
|
|
|
|
private static int[] _componentCounts = new int[0];
|
|
|
|
|
private static int[] _subjectsCounts = new int[0];
|
2023-04-20 19:23:58 +08:00
|
|
|
|
private static Dictionary<Type, int> _worldIds = new Dictionary<Type, int>();
|
2023-04-20 18:23:23 +08:00
|
|
|
|
private static class WorldIndex<TWorldArchetype>
|
2023-04-10 22:22:17 +08:00
|
|
|
|
{
|
2023-04-20 19:23:58 +08:00
|
|
|
|
public static int id = GetWorldId(typeof(TWorldArchetype));
|
2023-04-10 22:22:17 +08:00
|
|
|
|
}
|
|
|
|
|
private static int GetToken()
|
|
|
|
|
{
|
2023-05-26 00:24:38 +08:00
|
|
|
|
_tokenCount++;
|
|
|
|
|
Array.Resize(ref _componentCounts, _tokenCount);
|
|
|
|
|
Array.Resize(ref _subjectsCounts, _tokenCount);
|
|
|
|
|
foreach (var item in _resizer)
|
|
|
|
|
item.Resize(_tokenCount);
|
|
|
|
|
return _tokenCount - 1;
|
2023-04-10 22:22:17 +08:00
|
|
|
|
}
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-04-20 19:23:58 +08:00
|
|
|
|
public static int GetWorldId(Type archetype)
|
|
|
|
|
{
|
|
|
|
|
if(_worldIds.TryGetValue(archetype, out int id) == false)
|
|
|
|
|
{
|
|
|
|
|
id = GetToken();
|
|
|
|
|
_worldIds.Add(archetype, id);
|
|
|
|
|
}
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-04-20 18:23:23 +08:00
|
|
|
|
public static int GetWorldId<TWorldArchetype>() => WorldIndex<TWorldArchetype>.id;
|
2023-04-10 22:22:17 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-04-20 18:23:23 +08:00
|
|
|
|
public static int GetComponentId<T>(int worldID) => Component<T>.Get(worldID);
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-05-07 00:50:02 +08:00
|
|
|
|
public static int GetSubjectId<T>(int worldID) => Subject<T>.Get(worldID);
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static int GetExecutorId<T>(int worldID) => Executor<T>.Get(worldID);
|
2023-04-10 22:22:17 +08:00
|
|
|
|
private abstract class Resizer
|
|
|
|
|
{
|
|
|
|
|
public abstract void Resize(int size);
|
|
|
|
|
}
|
|
|
|
|
private sealed class Resizer<T> : Resizer
|
|
|
|
|
{
|
2023-04-20 18:23:23 +08:00
|
|
|
|
public override void Resize(int size)
|
|
|
|
|
{
|
|
|
|
|
Array.Resize(ref Component<T>.ids, size);
|
2023-05-07 00:50:02 +08:00
|
|
|
|
Array.Resize(ref Subject<T>.ids, size);
|
|
|
|
|
Array.Resize(ref Executor<T>.ids, size);
|
2023-04-20 18:23:23 +08:00
|
|
|
|
}
|
2023-04-10 22:22:17 +08:00
|
|
|
|
}
|
2023-04-20 18:23:23 +08:00
|
|
|
|
private static class Component<T>
|
2023-04-10 22:22:17 +08:00
|
|
|
|
{
|
|
|
|
|
public static int[] ids;
|
|
|
|
|
static Component()
|
|
|
|
|
{
|
2023-05-26 00:24:38 +08:00
|
|
|
|
ids = new int[_tokenCount];
|
2023-04-10 22:22:17 +08:00
|
|
|
|
for (int i = 0; i < ids.Length; i++)
|
|
|
|
|
ids[i] = -1;
|
2023-05-26 00:24:38 +08:00
|
|
|
|
_resizer.Add(new Resizer<T>());
|
2023-04-10 22:22:17 +08:00
|
|
|
|
}
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static int Get(int token)
|
|
|
|
|
{
|
|
|
|
|
ref int id = ref ids[token];
|
|
|
|
|
if (id < 0)
|
2023-05-26 00:24:38 +08:00
|
|
|
|
id = _componentCounts[token]++;
|
2023-04-10 22:22:17 +08:00
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-07 00:50:02 +08:00
|
|
|
|
private static class Subject<T>
|
|
|
|
|
{
|
|
|
|
|
public static int[] ids;
|
|
|
|
|
static Subject()
|
|
|
|
|
{
|
2023-05-26 00:24:38 +08:00
|
|
|
|
ids = new int[_tokenCount];
|
2023-05-07 00:50:02 +08:00
|
|
|
|
for (int i = 0; i < ids.Length; i++)
|
|
|
|
|
ids[i] = -1;
|
2023-05-26 00:24:38 +08:00
|
|
|
|
_resizer.Add(new Resizer<T>());
|
2023-05-07 00:50:02 +08:00
|
|
|
|
}
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static int Get(int token)
|
|
|
|
|
{
|
|
|
|
|
ref int id = ref ids[token];
|
|
|
|
|
if (id < 0)
|
2023-05-26 00:24:38 +08:00
|
|
|
|
id = _subjectsCounts[token]++;
|
2023-05-07 00:50:02 +08:00
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private static class Executor<T>
|
2023-04-20 18:23:23 +08:00
|
|
|
|
{
|
|
|
|
|
public static int[] ids;
|
2023-05-07 00:50:02 +08:00
|
|
|
|
static Executor()
|
2023-04-20 18:23:23 +08:00
|
|
|
|
{
|
2023-05-26 00:24:38 +08:00
|
|
|
|
ids = new int[_tokenCount];
|
2023-04-20 18:23:23 +08:00
|
|
|
|
for (int i = 0; i < ids.Length; i++)
|
|
|
|
|
ids[i] = -1;
|
2023-05-26 00:24:38 +08:00
|
|
|
|
_resizer.Add(new Resizer<T>());
|
2023-04-20 18:23:23 +08:00
|
|
|
|
}
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static int Get(int token)
|
|
|
|
|
{
|
|
|
|
|
ref int id = ref ids[token];
|
|
|
|
|
if (id < 0)
|
2023-05-26 00:24:38 +08:00
|
|
|
|
id = _subjectsCounts[token]++;
|
2023-04-20 18:23:23 +08:00
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-10 22:22:17 +08:00
|
|
|
|
}
|
2023-04-18 19:35:42 +08:00
|
|
|
|
#endregion
|
2023-05-23 01:47:28 +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();
|
|
|
|
|
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)]
|
|
|
|
|
public static void InvokeOnNewEntity(this List<IEcsWorldEventListener> self, int entityID)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0, iMax = self.Count; i < iMax; i++) self[i].OnNewEntity(entityID);
|
|
|
|
|
}
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static void InvokeOnDelEntity(this List<IEcsWorldEventListener> self, int entityID)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0, iMax = self.Count; i < iMax; i++) self[i].OnDelEntity(entityID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2023-03-02 14:42:44 +08:00
|
|
|
|
}
|