DragonECS/src/EcsWorld.cs

481 lines
17 KiB
C#
Raw Normal View History

2023-03-02 14:42:44 +08:00
using System;
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
{
public interface IEcsWorld
{
#region Properties
2023-03-23 01:15:34 +08:00
//private float _timeScale;//TODO реализовать собсвенныйтайм склей для разных миров
2023-03-02 14:42:44 +08:00
public bool IsEmpty { get; }
public Type ArchetypeType { get; }
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
#region GetterMethods
public ReadOnlySpan<IEcsPool> GetAllPools();
#endregion
2023-03-23 01:15:34 +08:00
#region Methods
2023-03-02 14:42:44 +08:00
public EcsPool<T> GetPool<T>() where T : struct;
2023-04-01 20:45:37 +08:00
public EcsPool<T> UncheckedGetPool<T>() where T : struct;
2023-03-26 11:19:03 +08:00
public EcsFilter Filter<TInc>() where TInc : struct, IInc;
2023-03-30 02:04:33 +08:00
public EcsFilter Filter<TInc, TExc>() where TInc : struct, IInc where TExc : struct, IExc;
2023-03-02 14:42:44 +08:00
public ent NewEntity();
2023-04-01 20:45:37 +08:00
public void DelEntity(ent entity);
2023-03-26 11:19:03 +08:00
public bool EntityIsAlive(int entityID, short gen);
public ent GetEntity(int entityID);
2023-03-02 14:42:44 +08:00
public void Destroy();
2023-04-01 22:19:36 +08:00
public bool IsMaskCompatible<TInc>(int entity) where TInc : struct, IInc;
public bool IsMaskCompatible<TInc, TExc>(int entity) where TInc : struct, IInc where TExc : struct, IExc;
2023-03-13 01:39:04 +08:00
public bool IsMaskCompatible(EcsMask mask, int entity);
public bool IsMaskCompatibleWithout(EcsMask mask, int entity, int otherPoolID);
2023-03-02 14:42:44 +08:00
internal void OnEntityComponentAdded(int entityID, int changedPoolID);
internal void OnEntityComponentRemoved(int entityID, int changedPoolID);
2023-04-01 20:45:37 +08:00
public int GetComponentID<T>();
internal void RegisterGroup(EcsGroup group);
2023-03-23 01:15:34 +08:00
#endregion
2023-03-02 14:42:44 +08:00
}
public abstract class EcsWorld
2023-03-02 14:42:44 +08:00
{
internal static IEcsWorld[] Worlds = new IEcsWorld[8];
2023-03-16 01:49:14 +08:00
private static IntDispenser _worldIdDispenser = new IntDispenser(1);
public readonly short id;
2023-03-02 14:42:44 +08:00
public EcsWorld()
{
id = (short)_worldIdDispenser.GetFree();
2023-04-01 20:45:37 +08:00
if(id >= Worlds.Length)
Array.Resize(ref Worlds, Worlds.Length << 1);
Worlds[id] = (IEcsWorld)this;
}
2023-03-30 02:29:11 +08:00
protected void Realeze()
{
Worlds[id] = null;
_worldIdDispenser.Release(id);
}
}
2023-03-30 01:14:43 +08:00
public abstract class EcsWorld<TArchetype> : EcsWorld, IEcsWorld
where TArchetype : EcsWorld<TArchetype>
{
private IntDispenser _entityDispenser;
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
private List<EcsFilter>[] _filtersByIncludedComponents;
private List<EcsFilter>[] _filtersByExcludedComponents;
private EcsFilter[] _filters;
2023-04-01 20:45:37 +08:00
private EcsPipeline _pipeline;
private List<EcsGroup> _groups;
#region RunnersCache
private PoolRunnres _poolRunnres;
private IEcsEntityCreate _entityCreate;
private IEcsEntityDestroy _entityDestry;
#endregion
#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;
#endregion
2023-03-02 14:42:44 +08:00
#region Properties
public bool IsEmpty => _entitiesCount < 0;
2023-03-02 14:42:44 +08:00
public Type ArchetypeType => typeof(TArchetype);
public int ID => id;
2023-04-01 20:45:37 +08:00
public EcsPipeline Pipeline => _pipeline;
public int EntitesCount => _entitiesCount;
public int EntitesCapacity => _denseEntities.Length;
2023-03-02 14:42:44 +08:00
#endregion
#region Constructors
2023-04-01 20:45:37 +08:00
public EcsWorld(EcsPipeline pipline = null)
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-03-30 10:46:57 +08:00
_entityDispenser = new IntDispenser(1);
2023-03-13 04:32:24 +08:00
_nullPool = new EcsNullPool(this);
2023-03-02 14:42:44 +08:00
_pools = new IEcsPool[512];
2023-03-26 11:19:03 +08:00
FillArray(_pools, _nullPool);
2023-04-01 20:45:37 +08:00
2023-03-12 20:45:18 +08:00
_gens = new short[512];
_filters = new EcsFilter[64];
2023-04-01 20:45:37 +08:00
_groups = new List<EcsGroup>(128);
_denseEntities = new int[512];
2023-04-01 20:45:37 +08:00
2023-03-12 20:45:18 +08:00
_filtersByIncludedComponents = new List<EcsFilter>[16];
_filtersByExcludedComponents = new List<EcsFilter>[16];
2023-04-01 20:45:37 +08:00
_poolRunnres = new PoolRunnres(_pipeline);
_entityCreate = _pipeline.GetRunner<IEcsEntityCreate>();
_entityDestry = _pipeline.GetRunner<IEcsEntityDestroy>();
_pipeline.GetRunner<IEcsInject<TArchetype>>().Inject((TArchetype)this);
_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-03-26 11:19:03 +08:00
FillArray(_pools, _nullPool, oldCapacity, oldCapacity - _pools.Length);
//Array.Fill(_pools, _nullPool, oldCapacity, oldCapacity - _pools.Length); //TODO Fix it
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-01 20:45:37 +08:00
public EcsPool<T> UncheckedGetPool<T>() where T : struct
{
return (EcsPool<T>)_pools[ComponentType<T>.uniqueID];
}
2023-03-02 14:42:44 +08:00
#endregion
#region GetFilter
2023-03-30 02:04:33 +08:00
public EcsFilter Filter<TInc>() where TInc : struct, IInc => Filter<TInc, Exc>();
public EcsFilter Filter<TInc, TExc>() where TInc : struct, IInc where TExc : struct, IExc
2023-03-12 20:45:18 +08:00
{
2023-03-30 02:04:33 +08:00
var mask = EcsMaskMap<TArchetype>.GetMask<TInc, TExc>();
2023-03-02 14:42:44 +08:00
2023-03-13 01:39:04 +08:00
if (_filters.Length <= EcsMaskMap<TArchetype>.Capacity)
2023-03-02 14:42:44 +08:00
{
2023-03-13 01:39:04 +08:00
Array.Resize(ref _filters, EcsMaskMap<TArchetype>.Capacity);
2023-03-02 14:42:44 +08:00
}
2023-03-13 01:39:04 +08:00
if (_filters[mask.UniqueID] == null)
2023-03-02 14:42:44 +08:00
{
2023-03-13 01:39:04 +08:00
_filters[mask.UniqueID] = NewFilter(mask);
2023-03-02 14:42:44 +08:00
}
2023-03-13 01:39:04 +08:00
return _filters[mask.UniqueID];
2023-03-02 14:42:44 +08:00
}
2023-03-12 20:45:18 +08:00
2023-03-13 01:39:04 +08:00
private EcsFilter NewFilter(EcsMask mask, int capacirty = 512)
2023-03-12 20:45:18 +08:00
{
2023-03-13 04:32:24 +08:00
var filter = new EcsFilter(this, mask, capacirty);
2023-03-12 20:45:18 +08:00
for (int i = 0; i < mask.IncCount; i++)
{
2023-03-13 04:32:24 +08:00
int componentID = mask.Inc[i];
var list = _filtersByIncludedComponents[componentID];
2023-03-12 20:45:18 +08:00
if (list == null)
{
list = new List<EcsFilter>(8);
2023-03-13 04:32:24 +08:00
_filtersByIncludedComponents[componentID] = list;
2023-03-12 20:45:18 +08:00
}
2023-03-13 04:32:24 +08:00
list.Add(filter);
2023-03-12 20:45:18 +08:00
}
for (int i = 0; i < mask.ExcCount; i++)
{
2023-03-13 04:32:24 +08:00
int componentID = mask.Exc[i];
var list = _filtersByExcludedComponents[componentID];
2023-03-12 20:45:18 +08:00
if (list == null)
{
list = new List<EcsFilter>(8);
2023-03-13 04:32:24 +08:00
_filtersByExcludedComponents[componentID] = list;
2023-03-12 20:45:18 +08:00
}
2023-03-13 04:32:24 +08:00
list.Add(filter);
2023-03-12 20:45:18 +08:00
}
2023-03-13 04:32:24 +08:00
// scan exist entities for compatibility with new filter.
for (int i = 0; i < _entitiesCount && _entitiesCount <= _denseEntities.Length; i++)
2023-03-13 04:32:24 +08:00
{
int entity = _denseEntities[i];
if (IsMaskCompatible(mask, entity))
filter.Add(entity);
2023-03-13 04:32:24 +08:00
}
return filter;
2023-03-12 20:45:18 +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
{
return IsMaskCompatible(EcsMaskMap<TArchetype>.GetMask<TInc, Exc>(), entityID);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsMaskCompatible<TInc, TExc>(int entityID) where TInc : struct, IInc where TExc : struct, IExc
{
return IsMaskCompatible(EcsMaskMap<TArchetype>.GetMask<TInc, TExc>(), entityID);
}
2023-03-13 01:39:04 +08:00
public bool IsMaskCompatible(EcsMask mask, int entity)
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-03-13 01:39:04 +08:00
if (mask.WorldArchetypeType != typeof(TArchetype))
throw new EcsFrameworkException("mask.WorldArchetypeType != typeof(TArchetype)");
#endif
for (int i = 0, iMax = mask.IncCount; i < iMax; i++)
2023-03-02 14:42:44 +08:00
{
2023-03-13 01:39:04 +08:00
if (!_pools[mask.Inc[i]].Has(entity))
2023-03-02 14:42:44 +08:00
return false;
}
2023-03-13 01:39:04 +08:00
for (int i = 0, iMax = mask.ExcCount; i < iMax; i++)
2023-03-02 14:42:44 +08:00
{
2023-03-13 01:39:04 +08:00
if (_pools[mask.Exc[i]].Has(entity))
2023-03-02 14:42:44 +08:00
return false;
}
return true;
}
2023-03-13 04:32:24 +08:00
public bool IsMaskCompatibleWithout(EcsMask 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-03-13 01:39:04 +08:00
if (mask.WorldArchetypeType != typeof(TArchetype))
throw new EcsFrameworkException("mask.WorldArchetypeType != typeof(TArchetype)");
#endif
for (int i = 0, iMax = mask.IncCount; 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-03-13 01:39:04 +08:00
for (int i = 0, iMax = mask.ExcCount; 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-03-13 04:32:24 +08:00
void IEcsWorld.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
if (includeList != null)
{
foreach (var filter in includeList)
{
if (IsMaskCompatible(filter.Mask, entityID))
{
filter.entities.UncheckedAdd(entityID);
}
}
}
if (excludeList != null)
{
foreach (var filter in excludeList)
{
if (IsMaskCompatibleWithout(filter.Mask, entityID, componentID))
{
filter.entities.UncheckedRemove(entityID);
}
}
}
//TODO провести стресс тест для варианта выши и закоментированного ниже
// if (includeList != null) foreach (var filter in includeList) filter.entities.Add(entityID);
// if (excludeList != null) foreach (var filter in excludeList) filter.entities.Remove(entityID);
2023-03-02 14:42:44 +08:00
}
2023-04-01 20:45:37 +08:00
void IEcsWorld.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
if (includeList != null)
{
foreach (var filter in includeList)
{
if (IsMaskCompatible(filter.Mask, entityID))
{
filter.entities.UncheckedRemove(entityID);
}
}
}
if (excludeList != null)
{
foreach (var filter in excludeList)
{
if (IsMaskCompatibleWithout(filter.Mask, entityID, componentID))
{
filter.entities.UncheckedAdd(entityID);
}
}
}
//TODO провести стресс тест для варианта выши и закоментированного ниже
// if (includeList != null) foreach (var filter in includeList) filter.entities.Remove(entityID);
// if (excludeList != null) foreach (var filter in excludeList) filter.entities.Add(entityID);
2023-03-02 14:42:44 +08:00
}
#endregion
2023-03-26 11:19:03 +08:00
#region Entity
public ent NewEntity()
2023-03-02 14:42:44 +08:00
{
2023-03-26 11:19:03 +08:00
int entityID = _entityDispenser.GetFree();
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);
}
_gens[entityID] |= short.MinValue;
2023-04-01 20:45:37 +08:00
ent entity = new ent(entityID, _gens[entityID]++, id);
_entityCreate.OnEntityCreate(entity);
return entity;
2023-03-26 11:19:03 +08:00
}
2023-04-01 20:45:37 +08:00
public void DelEntity(ent entity)
2023-03-26 11:19:03 +08:00
{
2023-04-01 20:45:37 +08:00
_entityDispenser.Release(entity.id);
_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)]
public ent GetEntity(int entityID)
2023-03-13 04:32:24 +08:00
{
2023-04-01 20:45:37 +08:00
return new ent(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)
{
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;
_denseEntities = null;
2023-03-30 02:29:11 +08:00
_gens = null;
_pools = null;
_nullPool = null;
_filtersByIncludedComponents = null;
_filtersByExcludedComponents = null;
_filters = null;
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
#region Utils
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
{
throw new EcsFrameworkException($"No more room for new component for this {typeof(TArchetype).FullName} IWorldArchetype");
}
#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
}
}
2023-03-26 11:19:03 +08:00
private void FillArray<T>(T[] array, T value, int startIndex = 0, int length = -1)
{
if (length < 0)
{
length = array.Length;
}
else
{
length = startIndex + length;
}
for (int i = startIndex; i < length; i++)
{
array[i] = value;
}
}
2023-03-02 14:42:44 +08:00
#endregion
2023-03-30 01:14:43 +08:00
2023-04-01 20:45:37 +08:00
#region Other
void IEcsWorld.RegisterGroup(EcsGroup group)
{
_groups.Add(group);
}
#endregion
}
[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
}