2023-04-06 23:40:47 +08:00
|
|
|
|
using DCFApixels.DragonECS.Internal;
|
|
|
|
|
using System;
|
2023-03-02 14:42:44 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
2023-04-01 20:45:37 +08:00
|
|
|
|
using System.Runtime.InteropServices;
|
2023-03-02 14:42:44 +08:00
|
|
|
|
|
|
|
|
|
namespace DCFApixels.DragonECS
|
|
|
|
|
{
|
2023-04-06 23:40:47 +08:00
|
|
|
|
public interface IEcsWorld : IEcsReadonlyTable
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
|
|
|
|
#region Properties
|
2023-03-23 01:15:34 +08:00
|
|
|
|
//private float _timeScale;//TODO реализовать собсвенныйтайм склей для разных миров
|
2023-03-11 17:11:40 +08:00
|
|
|
|
public int ID { get; }
|
2023-04-01 20:45:37 +08:00
|
|
|
|
public EcsPipeline Pipeline { get; }
|
|
|
|
|
public int EntitesCount { get; }
|
|
|
|
|
public int EntitesCapacity { get; }
|
2023-03-02 14:42:44 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
2023-04-06 23:40:47 +08:00
|
|
|
|
#region Entities
|
2023-04-08 05:50:44 +08:00
|
|
|
|
public EcsEntity NewEntity();
|
|
|
|
|
public void DelEntity(EcsEntity entity);
|
2023-03-26 11:19:03 +08:00
|
|
|
|
public bool EntityIsAlive(int entityID, short gen);
|
2023-04-08 05:50:44 +08:00
|
|
|
|
public EcsEntity GetEntity(int entityID);
|
2023-03-02 14:42:44 +08:00
|
|
|
|
public void Destroy();
|
2023-03-23 01:15:34 +08:00
|
|
|
|
#endregion
|
2023-04-08 21:29:18 +08:00
|
|
|
|
|
|
|
|
|
#region Group
|
|
|
|
|
internal EcsGroup GetGroupFromPool();
|
|
|
|
|
internal void ReleaseGroup(EcsGroup group);
|
|
|
|
|
#endregion
|
2023-03-02 14:42:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-11 17:11:40 +08:00
|
|
|
|
public abstract class EcsWorld
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-03-11 17:11:40 +08:00
|
|
|
|
internal static IEcsWorld[] Worlds = new IEcsWorld[8];
|
2023-04-07 15:11:48 +08:00
|
|
|
|
private static IntDispenser _worldIdDispenser = new IntDispenser(0);
|
2023-03-11 17:11:40 +08:00
|
|
|
|
|
|
|
|
|
public readonly short id;
|
2023-03-02 14:42:44 +08:00
|
|
|
|
|
2023-04-06 23:40:47 +08:00
|
|
|
|
protected EcsWorld(bool isIndexed)
|
2023-03-11 17:11:40 +08:00
|
|
|
|
{
|
2023-04-06 23:40:47 +08:00
|
|
|
|
if(isIndexed == true)
|
|
|
|
|
{
|
|
|
|
|
id = (short)_worldIdDispenser.GetFree();
|
|
|
|
|
if (id >= Worlds.Length)
|
|
|
|
|
Array.Resize(ref Worlds, Worlds.Length << 1);
|
|
|
|
|
Worlds[id] = (IEcsWorld)this;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
id = -1;
|
|
|
|
|
}
|
2023-03-11 17:11:40 +08:00
|
|
|
|
}
|
2023-03-30 02:29:11 +08:00
|
|
|
|
|
|
|
|
|
protected void Realeze()
|
|
|
|
|
{
|
|
|
|
|
Worlds[id] = null;
|
|
|
|
|
_worldIdDispenser.Release(id);
|
|
|
|
|
}
|
2023-03-11 17:11:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 05:08:48 +08:00
|
|
|
|
public abstract class EcsWorld<TWorldArchetype> : EcsWorld, IEcsWorld
|
|
|
|
|
where TWorldArchetype : EcsWorld<TWorldArchetype>
|
2023-03-11 17:11:40 +08:00
|
|
|
|
{
|
|
|
|
|
private IntDispenser _entityDispenser;
|
2023-04-01 22:18:40 +08:00
|
|
|
|
private int[] _denseEntities;
|
|
|
|
|
private int _entitiesCount;
|
|
|
|
|
private short[] _gens; //старший бит указывает на то жива ли сущьность.
|
2023-03-02 14:42:44 +08:00
|
|
|
|
|
2023-03-26 11:19:03 +08:00
|
|
|
|
//private short[] _componentCounts; //TODO
|
2023-03-02 14:42:44 +08:00
|
|
|
|
private IEcsPool[] _pools;
|
2023-03-13 04:32:24 +08:00
|
|
|
|
private EcsNullPool _nullPool;
|
2023-03-02 14:42:44 +08:00
|
|
|
|
|
2023-04-07 15:11:48 +08:00
|
|
|
|
private List<EcsQueryBase>[] _filtersByIncludedComponents;
|
|
|
|
|
private List<EcsQueryBase>[] _filtersByExcludedComponents;
|
2023-03-02 14:42:44 +08:00
|
|
|
|
|
2023-04-08 00:47:35 +08:00
|
|
|
|
private EcsQueryBase[] _queries;
|
2023-03-02 14:42:44 +08:00
|
|
|
|
|
2023-04-01 20:45:37 +08:00
|
|
|
|
private EcsPipeline _pipeline;
|
|
|
|
|
|
|
|
|
|
private List<EcsGroup> _groups;
|
|
|
|
|
|
2023-04-08 00:47:35 +08:00
|
|
|
|
public IEcsRealationTable[] _relationTables;
|
|
|
|
|
|
|
|
|
|
#region RelationTables
|
|
|
|
|
public IEcsRealationTable GetRelationTalbe<TWorldArhetype>(TWorldArhetype targetWorld)
|
|
|
|
|
where TWorldArhetype : EcsWorld<TWorldArhetype>
|
|
|
|
|
{
|
|
|
|
|
int targetID = targetWorld.ID;
|
|
|
|
|
if (targetID <= 0)
|
|
|
|
|
throw new ArgumentException("targetWorld.ID <= 0");
|
|
|
|
|
|
|
|
|
|
if(_relationTables.Length <= targetID)
|
|
|
|
|
Array.Resize(ref _relationTables, targetID + 8);
|
|
|
|
|
|
|
|
|
|
if (_relationTables[targetID] == null)
|
|
|
|
|
{
|
|
|
|
|
// _relationTables[targetID]= new EcsRelationTable
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2023-04-07 15:11:48 +08:00
|
|
|
|
|
2023-04-01 20:45:37 +08:00
|
|
|
|
#region RunnersCache
|
|
|
|
|
private PoolRunnres _poolRunnres;
|
|
|
|
|
private IEcsEntityCreate _entityCreate;
|
|
|
|
|
private IEcsEntityDestroy _entityDestry;
|
|
|
|
|
#endregion
|
|
|
|
|
|
2023-03-30 20:47:39 +08:00
|
|
|
|
#region GetterMethods
|
|
|
|
|
public ReadOnlySpan<IEcsPool> GetAllPools() => new ReadOnlySpan<IEcsPool>(_pools);
|
2023-04-01 20:45:37 +08:00
|
|
|
|
public int GetComponentID<T>() => ComponentType<T>.uniqueID;
|
2023-03-30 20:47:39 +08:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2023-04-06 23:40:47 +08:00
|
|
|
|
#region Internal Properties
|
|
|
|
|
int IEcsReadonlyTable.Count => _entitiesCount;
|
|
|
|
|
int IEcsReadonlyTable.Capacity => _denseEntities.Length;
|
|
|
|
|
#endregion
|
|
|
|
|
|
2023-03-02 14:42:44 +08:00
|
|
|
|
#region Properties
|
2023-04-07 05:08:48 +08:00
|
|
|
|
public Type ArchetypeType => typeof(TWorldArchetype);
|
2023-03-11 17:11:40 +08:00
|
|
|
|
public int ID => id;
|
2023-04-01 20:45:37 +08:00
|
|
|
|
public EcsPipeline Pipeline => _pipeline;
|
|
|
|
|
|
2023-04-01 22:18:40 +08:00
|
|
|
|
public int EntitesCount => _entitiesCount;
|
|
|
|
|
public int EntitesCapacity => _denseEntities.Length;
|
2023-03-02 14:42:44 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Constructors
|
2023-04-06 23:40:47 +08:00
|
|
|
|
public EcsWorld(EcsPipeline pipline = null) : base(true)
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-04-01 20:45:37 +08:00
|
|
|
|
_pipeline = pipline ?? EcsPipeline.Empty;
|
|
|
|
|
if (!_pipeline.IsInit) pipline.Init();
|
2023-04-07 15:11:48 +08:00
|
|
|
|
_entityDispenser = new IntDispenser(0);
|
2023-04-06 23:40:47 +08:00
|
|
|
|
_nullPool = EcsNullPool.instance;
|
2023-03-02 14:42:44 +08:00
|
|
|
|
_pools = new IEcsPool[512];
|
2023-04-08 00:47:35 +08:00
|
|
|
|
ArrayUtility.Fill(_pools, _nullPool);
|
2023-04-01 20:45:37 +08:00
|
|
|
|
|
2023-03-12 20:45:18 +08:00
|
|
|
|
_gens = new short[512];
|
2023-04-08 00:47:35 +08:00
|
|
|
|
_queries = new EcsQuery<TWorldArchetype>[QueryType.capacity];
|
2023-04-01 20:45:37 +08:00
|
|
|
|
_groups = new List<EcsGroup>(128);
|
|
|
|
|
|
2023-04-01 22:18:40 +08:00
|
|
|
|
_denseEntities = new int[512];
|
2023-04-01 20:45:37 +08:00
|
|
|
|
|
2023-04-07 15:11:48 +08:00
|
|
|
|
_filtersByIncludedComponents = new List<EcsQueryBase>[16];
|
|
|
|
|
_filtersByExcludedComponents = new List<EcsQueryBase>[16];
|
2023-04-01 20:45:37 +08:00
|
|
|
|
|
|
|
|
|
_poolRunnres = new PoolRunnres(_pipeline);
|
|
|
|
|
_entityCreate = _pipeline.GetRunner<IEcsEntityCreate>();
|
|
|
|
|
_entityDestry = _pipeline.GetRunner<IEcsEntityDestroy>();
|
2023-04-07 05:08:48 +08:00
|
|
|
|
_pipeline.GetRunner<IEcsInject<TWorldArchetype>>().Inject((TWorldArchetype)this);
|
2023-04-01 20:45:37 +08:00
|
|
|
|
_pipeline.GetRunner<IEcsInject<IEcsWorld>>().Inject(this);
|
|
|
|
|
_pipeline.GetRunner<IEcsWorldCreate>().OnWorldCreate(this);
|
2023-03-02 14:42:44 +08:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region GetPool
|
|
|
|
|
public EcsPool<T> GetPool<T>() where T : struct
|
|
|
|
|
{
|
|
|
|
|
int uniqueID = ComponentType<T>.uniqueID;
|
|
|
|
|
|
|
|
|
|
if (uniqueID >= _pools.Length)
|
|
|
|
|
{
|
2023-03-13 04:32:24 +08:00
|
|
|
|
int oldCapacity = _pools.Length;
|
2023-03-13 00:05:35 +08:00
|
|
|
|
Array.Resize(ref _pools, ComponentType.Capacity);
|
2023-04-08 00:47:35 +08:00
|
|
|
|
ArrayUtility.Fill(_pools, _nullPool, oldCapacity, oldCapacity - _pools.Length);
|
2023-03-13 04:32:24 +08:00
|
|
|
|
|
2023-03-13 00:05:35 +08:00
|
|
|
|
Array.Resize(ref _filtersByIncludedComponents, ComponentType.Capacity);
|
|
|
|
|
Array.Resize(ref _filtersByExcludedComponents, ComponentType.Capacity);
|
2023-03-02 14:42:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-13 04:32:24 +08:00
|
|
|
|
if (_pools[uniqueID] == _nullPool)
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-04-01 20:45:37 +08:00
|
|
|
|
_pools[uniqueID] = new EcsPool<T>(this, ComponentType<T>.uniqueID, 512, _poolRunnres);
|
2023-03-02 14:42:44 +08:00
|
|
|
|
}
|
|
|
|
|
return (EcsPool<T>)_pools[uniqueID];
|
|
|
|
|
}
|
2023-04-07 19:23:07 +08:00
|
|
|
|
//public EcsPool<T> UncheckedGetPool<T>() where T : struct => (EcsPool<T>)_pools[ComponentType<T>.uniqueID];
|
2023-03-02 14:42:44 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
2023-04-08 00:47:35 +08:00
|
|
|
|
#region Query
|
|
|
|
|
public TQuery Query<TQuery>(out TQuery query) where TQuery : EcsQueryBase
|
2023-03-12 20:45:18 +08:00
|
|
|
|
{
|
2023-04-08 00:47:35 +08:00
|
|
|
|
int uniqueID = QueryType<TQuery>.uniqueID;
|
|
|
|
|
if (_queries.Length < QueryType.capacity)
|
|
|
|
|
Array.Resize(ref _queries, QueryType.capacity);
|
2023-03-02 14:42:44 +08:00
|
|
|
|
|
2023-04-07 16:03:42 +08:00
|
|
|
|
if (_queries[uniqueID] == null)
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-04-07 16:03:42 +08:00
|
|
|
|
_queries[uniqueID] = EcsQuery<TWorldArchetype>.Builder.Build<TQuery>(this);
|
2023-04-08 00:47:35 +08:00
|
|
|
|
var mask = _queries[uniqueID].mask;
|
|
|
|
|
var filter = _queries[uniqueID];
|
2023-03-02 14:42:44 +08:00
|
|
|
|
|
2023-04-07 18:21:52 +08:00
|
|
|
|
for (int i = 0; i < mask.Inc.Length; i++)
|
2023-03-12 20:45:18 +08:00
|
|
|
|
{
|
2023-04-07 05:08:48 +08:00
|
|
|
|
int componentID = mask.Inc[i];
|
|
|
|
|
var list = _filtersByIncludedComponents[componentID];
|
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
2023-04-07 15:11:48 +08:00
|
|
|
|
list = new List<EcsQueryBase>(8);
|
2023-04-07 05:08:48 +08:00
|
|
|
|
_filtersByIncludedComponents[componentID] = list;
|
|
|
|
|
}
|
|
|
|
|
list.Add(filter);
|
2023-03-12 20:45:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 18:21:52 +08:00
|
|
|
|
for (int i = 0; i < mask.Exc.Length; i++)
|
2023-03-12 20:45:18 +08:00
|
|
|
|
{
|
2023-04-07 05:08:48 +08:00
|
|
|
|
int componentID = mask.Exc[i];
|
|
|
|
|
var list = _filtersByExcludedComponents[componentID];
|
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
2023-04-07 15:11:48 +08:00
|
|
|
|
list = new List<EcsQueryBase>(8);
|
2023-04-07 05:08:48 +08:00
|
|
|
|
_filtersByExcludedComponents[componentID] = list;
|
|
|
|
|
}
|
|
|
|
|
list.Add(filter);
|
|
|
|
|
}
|
|
|
|
|
// scan exist entities for compatibility with new filter.
|
|
|
|
|
for (int i = 0; i < _entitiesCount && _entitiesCount <= _denseEntities.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
int entity = _denseEntities[i];
|
2023-04-07 18:16:13 +08:00
|
|
|
|
if (IsMaskCompatible(mask, entity))
|
2023-04-07 15:11:48 +08:00
|
|
|
|
filter.AddEntity(entity);
|
2023-03-12 20:45:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-08 00:47:35 +08:00
|
|
|
|
query = (TQuery)_queries[uniqueID];
|
|
|
|
|
return query;
|
2023-04-07 05:08:48 +08:00
|
|
|
|
}
|
2023-03-02 14:42:44 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region IsMaskCompatible/IsMaskCompatibleWithout
|
2023-04-01 22:19:36 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public bool IsMaskCompatible<TInc>(int entityID) where TInc : struct, IInc
|
|
|
|
|
{
|
2023-04-07 05:08:48 +08:00
|
|
|
|
return IsMaskCompatible(EcsMaskMap<TWorldArchetype>.GetMask<TInc, Exc>(), entityID);
|
2023-04-01 22:19:36 +08:00
|
|
|
|
}
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public bool IsMaskCompatible<TInc, TExc>(int entityID) where TInc : struct, IInc where TExc : struct, IExc
|
|
|
|
|
{
|
2023-04-07 05:08:48 +08:00
|
|
|
|
return IsMaskCompatible(EcsMaskMap<TWorldArchetype>.GetMask<TInc, TExc>(), entityID);
|
2023-04-01 22:19:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 00:47:35 +08:00
|
|
|
|
public bool IsMaskCompatible(EcsComponentMask mask, int entityID)
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-04-01 20:45:37 +08:00
|
|
|
|
#if (DEBUG && !DISABLE_DRAGONECS_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS
|
2023-04-07 05:08:48 +08:00
|
|
|
|
if (mask.WorldArchetypeType != typeof(TWorldArchetype))
|
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-04-07 18:21:52 +08:00
|
|
|
|
for (int i = 0, iMax = mask.Inc.Length; i < iMax; i++)
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-04-08 00:47:35 +08:00
|
|
|
|
if (!_pools[mask.Inc[i]].Has(entityID))
|
2023-03-02 14:42:44 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-04-07 18:21:52 +08:00
|
|
|
|
for (int i = 0, iMax = mask.Exc.Length; i < iMax; i++)
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-04-08 00:47:35 +08:00
|
|
|
|
if (_pools[mask.Exc[i]].Has(entityID))
|
2023-03-02 14:42:44 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 18:16:13 +08:00
|
|
|
|
public bool IsMaskCompatibleWithout(EcsComponentMask mask, int entity, int otherComponentID)
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-04-01 20:45:37 +08:00
|
|
|
|
#if (DEBUG && !DISABLE_DRAGONECS_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS
|
2023-04-07 05:08:48 +08:00
|
|
|
|
if (mask.WorldArchetypeType != typeof(TWorldArchetype))
|
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-04-07 18:21:52 +08:00
|
|
|
|
for (int i = 0, iMax = mask.Inc.Length; i < iMax; i++)
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-03-13 04:32:24 +08:00
|
|
|
|
int componentID = mask.Inc[i];
|
|
|
|
|
if (componentID == otherComponentID || !_pools[componentID].Has(entity))
|
2023-03-02 14:42:44 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-04-07 18:21:52 +08:00
|
|
|
|
for (int i = 0, iMax = mask.Exc.Length; i < iMax; i++)
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-04-01 20:45:37 +08:00
|
|
|
|
int componentID = mask.Exc[i];
|
|
|
|
|
if (componentID != otherComponentID && _pools[componentID].Has(entity))
|
2023-03-02 14:42:44 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region EntityChangedReact
|
2023-04-01 22:18:40 +08:00
|
|
|
|
|
2023-04-06 23:40:47 +08:00
|
|
|
|
void IEcsReadonlyTable.OnEntityComponentAdded(int entityID, int componentID)
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-03-13 04:32:24 +08:00
|
|
|
|
var includeList = _filtersByIncludedComponents[componentID];
|
|
|
|
|
var excludeList = _filtersByExcludedComponents[componentID];
|
2023-03-02 14:42:44 +08:00
|
|
|
|
|
2023-04-07 15:11:48 +08:00
|
|
|
|
if (includeList != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var filter in includeList)
|
|
|
|
|
{
|
|
|
|
|
if (IsMaskCompatible(filter.mask, entityID))
|
|
|
|
|
{
|
|
|
|
|
filter.AddEntity(entityID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (excludeList != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var filter in excludeList)
|
|
|
|
|
{
|
|
|
|
|
if (IsMaskCompatibleWithout(filter.mask, entityID, componentID))
|
|
|
|
|
{
|
|
|
|
|
filter.RemoveEntity(entityID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-01 22:18:40 +08:00
|
|
|
|
//TODO провести стресс тест для варианта выши и закоментированного ниже
|
|
|
|
|
|
2023-04-07 15:11:48 +08:00
|
|
|
|
// if (includeList != null) foreach (var filter in includeList) filter.Add(entityID);
|
|
|
|
|
// if (excludeList != null) foreach (var filter in excludeList) filter.Remove(entityID);
|
2023-03-02 14:42:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 23:40:47 +08:00
|
|
|
|
void IEcsReadonlyTable.OnEntityComponentRemoved(int entityID, int componentID)
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-04-01 20:45:37 +08:00
|
|
|
|
var includeList = _filtersByIncludedComponents[componentID];
|
|
|
|
|
var excludeList = _filtersByExcludedComponents[componentID];
|
2023-03-02 14:42:44 +08:00
|
|
|
|
|
2023-04-07 15:11:48 +08:00
|
|
|
|
if (includeList != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var filter in includeList)
|
|
|
|
|
{
|
|
|
|
|
if (IsMaskCompatible(filter.mask, entityID))
|
|
|
|
|
{
|
|
|
|
|
filter.RemoveEntity(entityID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (excludeList != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var filter in excludeList)
|
|
|
|
|
{
|
|
|
|
|
if (IsMaskCompatibleWithout(filter.mask, entityID, componentID))
|
|
|
|
|
{
|
|
|
|
|
filter.AddEntity(entityID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-01 22:18:40 +08:00
|
|
|
|
//TODO провести стресс тест для варианта выши и закоментированного ниже
|
|
|
|
|
|
2023-04-07 15:11:48 +08:00
|
|
|
|
// if (includeList != null) foreach (var filter in includeList) filter.Remove(entityID);
|
|
|
|
|
// if (excludeList != null) foreach (var filter in excludeList) filter.Add(entityID);
|
2023-03-02 14:42:44 +08:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2023-03-26 11:19:03 +08:00
|
|
|
|
#region Entity
|
2023-04-08 05:50:44 +08:00
|
|
|
|
public EcsEntity NewEntity()
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-03-26 11:19:03 +08:00
|
|
|
|
int entityID = _entityDispenser.GetFree();
|
2023-04-01 22:18:40 +08:00
|
|
|
|
if (_entityDispenser.LastInt >= _denseEntities.Length)
|
|
|
|
|
Array.Resize(ref _denseEntities, _denseEntities.Length << 1);
|
|
|
|
|
_denseEntities[_entitiesCount++] = entityID;
|
|
|
|
|
|
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-01 20:45:37 +08:00
|
|
|
|
foreach (var item in _groups)
|
|
|
|
|
item.OnWorldResize(_gens.Length);
|
|
|
|
|
foreach (var item in _pools)
|
|
|
|
|
item.OnWorldResize(_gens.Length);
|
|
|
|
|
}
|
2023-04-01 22:18:40 +08:00
|
|
|
|
_gens[entityID] |= short.MinValue;
|
2023-04-08 05:50:44 +08:00
|
|
|
|
EcsEntity entity = new EcsEntity(entityID, _gens[entityID]++, id);
|
2023-04-01 20:45:37 +08:00
|
|
|
|
_entityCreate.OnEntityCreate(entity);
|
|
|
|
|
return entity;
|
2023-03-26 11:19:03 +08:00
|
|
|
|
}
|
2023-04-08 05:50:44 +08:00
|
|
|
|
public void DelEntity(EcsEntity entity)
|
2023-03-26 11:19:03 +08:00
|
|
|
|
{
|
2023-04-01 20:45:37 +08:00
|
|
|
|
_entityDispenser.Release(entity.id);
|
2023-04-01 22:18:40 +08:00
|
|
|
|
_gens[entity.id] |= short.MinValue;
|
|
|
|
|
_entitiesCount--;
|
2023-04-01 20:45:37 +08:00
|
|
|
|
_entityDestry.OnEntityDestroy(entity);
|
2023-03-02 14:42:44 +08:00
|
|
|
|
}
|
2023-04-01 20:45:37 +08:00
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-04-08 05:50:44 +08:00
|
|
|
|
public EcsEntity GetEntity(int entityID)
|
2023-03-13 04:32:24 +08:00
|
|
|
|
{
|
2023-04-08 05:50:44 +08:00
|
|
|
|
return new EcsEntity(entityID, _gens[entityID], id);
|
2023-03-13 04:32:24 +08:00
|
|
|
|
}
|
2023-03-26 11:19:03 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public bool EntityIsAlive(int entityID, short gen)
|
|
|
|
|
{
|
2023-04-01 22:18:40 +08:00
|
|
|
|
return _gens[entityID] == gen;
|
2023-03-26 11:19:03 +08:00
|
|
|
|
}
|
2023-03-02 14:42:44 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Destroy
|
|
|
|
|
public void Destroy()
|
|
|
|
|
{
|
2023-03-30 02:29:11 +08:00
|
|
|
|
_entityDispenser = null;
|
2023-04-01 22:18:40 +08:00
|
|
|
|
_denseEntities = null;
|
2023-03-30 02:29:11 +08:00
|
|
|
|
_gens = null;
|
|
|
|
|
_pools = null;
|
|
|
|
|
_nullPool = null;
|
|
|
|
|
_filtersByIncludedComponents = null;
|
|
|
|
|
_filtersByExcludedComponents = null;
|
2023-04-07 16:03:42 +08:00
|
|
|
|
_queries = null;
|
2023-03-30 02:29:11 +08:00
|
|
|
|
Realeze();
|
2023-03-02 14:42:44 +08:00
|
|
|
|
}
|
2023-04-01 20:45:37 +08:00
|
|
|
|
public void DestryWithPipeline()
|
|
|
|
|
{
|
|
|
|
|
Destroy();
|
|
|
|
|
_pipeline.Destroy();
|
|
|
|
|
}
|
2023-03-02 14:42:44 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
2023-04-07 05:08:48 +08:00
|
|
|
|
#region Other
|
|
|
|
|
void IEcsReadonlyTable.RegisterGroup(EcsGroup group)
|
|
|
|
|
{
|
|
|
|
|
_groups.Add(group);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2023-03-02 14:42:44 +08:00
|
|
|
|
#region Utils
|
2023-04-08 00:47:35 +08:00
|
|
|
|
internal static class QueryType
|
2023-04-07 05:08:48 +08:00
|
|
|
|
{
|
|
|
|
|
public static int increment = 0;
|
|
|
|
|
public static int capacity = 128;
|
|
|
|
|
}
|
2023-04-08 00:47:35 +08:00
|
|
|
|
internal static class QueryType<TQuery>
|
2023-04-07 05:08:48 +08:00
|
|
|
|
{
|
|
|
|
|
public static int uniqueID;
|
2023-04-08 00:47:35 +08:00
|
|
|
|
static QueryType()
|
2023-04-07 05:08:48 +08:00
|
|
|
|
{
|
2023-04-08 00:47:35 +08:00
|
|
|
|
uniqueID = QueryType.increment++;
|
|
|
|
|
if (QueryType.increment > QueryType.capacity)
|
|
|
|
|
QueryType.capacity <<= 1;
|
2023-04-07 05:08:48 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-13 01:39:04 +08:00
|
|
|
|
internal static class ComponentType
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
|
|
|
|
internal static int increment = 1;
|
2023-03-13 00:05:35 +08:00
|
|
|
|
internal static int Capacity
|
|
|
|
|
{
|
|
|
|
|
get => types.Length;
|
|
|
|
|
}
|
2023-03-13 01:39:04 +08:00
|
|
|
|
internal static Type[] types = new Type[64];
|
2023-03-02 14:42:44 +08:00
|
|
|
|
}
|
2023-03-13 01:39:04 +08:00
|
|
|
|
internal static class ComponentType<T>
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
|
|
|
|
internal static int uniqueID;
|
|
|
|
|
|
|
|
|
|
static ComponentType()
|
|
|
|
|
{
|
2023-03-13 01:39:04 +08:00
|
|
|
|
uniqueID = ComponentType.increment++;
|
2023-04-01 20:45:37 +08:00
|
|
|
|
#if (DEBUG && !DISABLE_DRAGONECS_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS
|
2023-03-13 01:39:04 +08:00
|
|
|
|
if (ComponentType.increment + 1 > ushort.MaxValue)
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-04-07 05:08:48 +08:00
|
|
|
|
throw new EcsFrameworkException($"No more room for new component for this {typeof(TWorldArchetype).FullName} IWorldArchetype");
|
2023-03-02 14:42:44 +08:00
|
|
|
|
}
|
|
|
|
|
#endif
|
2023-03-13 01:39:04 +08:00
|
|
|
|
if (uniqueID >= ComponentType.types.Length)
|
2023-03-02 14:42:44 +08:00
|
|
|
|
{
|
2023-03-13 01:39:04 +08:00
|
|
|
|
Array.Resize(ref ComponentType.types, ComponentType.types.Length << 1);
|
2023-03-02 14:42:44 +08:00
|
|
|
|
}
|
2023-03-13 01:39:04 +08:00
|
|
|
|
ComponentType.types[uniqueID] = typeof(T);
|
2023-03-02 14:42:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2023-04-08 21:29:18 +08:00
|
|
|
|
|
|
|
|
|
#region GroupsPool
|
|
|
|
|
private Stack<EcsGroup> _pool = new Stack<EcsGroup>(64);
|
|
|
|
|
EcsGroup IEcsWorld.GetGroupFromPool()
|
|
|
|
|
{
|
|
|
|
|
if (_pool.Count <= 0)
|
|
|
|
|
{
|
|
|
|
|
return new EcsGroup(this);
|
|
|
|
|
}
|
|
|
|
|
return _pool.Pop();
|
|
|
|
|
}
|
|
|
|
|
void IEcsWorld.ReleaseGroup(EcsGroup group)
|
|
|
|
|
{
|
|
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS
|
|
|
|
|
if (group.World != this)
|
|
|
|
|
throw new ArgumentException("group.World != this");
|
|
|
|
|
#endif
|
|
|
|
|
group.Clear();
|
|
|
|
|
_pool.Push(group);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2023-04-01 20:45:37 +08:00
|
|
|
|
}
|
2023-04-08 21:29:18 +08:00
|
|
|
|
|
2023-04-01 20:45:37 +08:00
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 8, Size = 24)]
|
|
|
|
|
internal readonly struct PoolRunnres
|
2023-03-30 01:14:43 +08:00
|
|
|
|
{
|
2023-04-01 20:45:37 +08:00
|
|
|
|
public readonly IEcsComponentAdd add;
|
|
|
|
|
public readonly IEcsComponentWrite write;
|
|
|
|
|
public readonly IEcsComponentDel del;
|
|
|
|
|
|
|
|
|
|
public PoolRunnres(EcsPipeline pipeline)
|
2023-03-30 01:14:43 +08:00
|
|
|
|
{
|
2023-04-01 20:45:37 +08:00
|
|
|
|
add = pipeline.GetRunner<IEcsComponentAdd>();
|
|
|
|
|
write = pipeline.GetRunner<IEcsComponentWrite>();
|
|
|
|
|
del = pipeline.GetRunner<IEcsComponentDel>();
|
2023-03-30 01:14:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-02 14:42:44 +08:00
|
|
|
|
}
|