refactoring

This commit is contained in:
Mikhail 2023-04-08 00:47:35 +08:00
parent 53b7fae2c4
commit 1ed1782e4a
6 changed files with 100 additions and 200 deletions

View File

@ -269,22 +269,13 @@ namespace DCFApixels.DragonECS
get => _source.World.GetEntity(_dense[_index]);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
{
// <= потму что отсчет начинается с индекса 1
return ++_index <= _count && _count < _dense.Length; //_count < _dense.Length дает среде понять что проверки на выход за границы не нужны
}
public bool MoveNext() => ++_index <= _count && _count<_dense.Length; // <= потму что отсчет начинается с индекса 1 //_count < _dense.Length дает среде понять что проверки на выход за границы не нужны
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose()
{
_source.Unlock();
}
public void Dispose() => _source.Unlock();
}
#endregion
}
public static class EcsGroupExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]

View File

@ -5,27 +5,14 @@ using System.Runtime.CompilerServices;
namespace DCFApixels.DragonECS
{
public interface IEcsQuery
{
internal void AddEntity(int entityID);
internal void RemoveEntity(int entityID);
public EcsQueryMask Mask { get; }
}
public abstract class EcsQueryBase : IEcsQuery
public abstract class EcsQueryBase
{
internal EcsGroup group;
internal EcsQueryMask mask;
public EcsQueryMask Mask => mask;
public void AddEntity(int entityID) => group.Add(entityID);
public void RemoveEntity(int entityID) => group.Remove(entityID);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IEcsQuery.AddEntity(int entityID) => group.Add(entityID);
internal void AddEntity(int entityID) => group.Add(entityID);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IEcsQuery.RemoveEntity(int entityID) => group.Remove(entityID);
internal void RemoveEntity(int entityID) => group.Remove(entityID);
}
public abstract class EcsQuery<TWorldArchetype> : EcsQueryBase
@ -51,7 +38,7 @@ namespace DCFApixels.DragonECS
private List<int> _inc;
private List<int> _exc;
internal static TQuery Build<TQuery>(IEcsWorld world) where TQuery : IEcsQuery
internal static TQuery Build<TQuery>(IEcsWorld world) where TQuery : EcsQueryBase
{
Builder builder = new Builder(world);

View File

@ -5,13 +5,13 @@ using System.Collections.Generic;
namespace DCFApixels.DragonECS
{
/* public interface IEcsRealationTable : IEcsReadonlyTable
public interface IEcsRealationTable
{
public EcsFilter Relations<TComponent>() where TComponent : struct;
rr
}
public sealed class EcsRelationTable<TWorldArchetype> : IEcsRealationTable
where TWorldArchetype : EcsRelationTableArchetypeBase
internal class EcsRelationWorld<TRelationTableArhetype> : EcsWorld<EcsRelationWorld<TRelationTableArhetype>>
where TRelationTableArhetype : EcsRelationTableArchetypeBase { }
public sealed class EcsRelationTable<TTableArhetype> : IEcsRealationTable
where TTableArhetype : EcsRelationTableArchetypeBase
{
public readonly IEcsWorld leftWorld;
public readonly IEcsWorld rightWorld;
@ -20,118 +20,6 @@ namespace DCFApixels.DragonECS
private int[] _leftMapping;
private int[] _rgihtMapping;
private int _relationsCount;
private IEcsPool[] _pools;
private EcsNullPool _nullPool;
#region Properties
public Type ArchetypeType => typeof(TWorldArchetype);
public int EntitesCount => _relationsCount;
public int EntitesCapacity => _relations.Length;
#endregion
#region Constructors
internal EcsRelationTable(IEcsWorld leftWorld, IEcsWorld rightWorld)
{
this.leftWorld = leftWorld;
this.rightWorld = rightWorld;
_relations = new int[512];
_leftMapping = new int[512];
_rgihtMapping = new int[512];
_relationsCount = 0;
}
#endregion
#region RealtionControls
public void AddRelation(int leftEnttiyID, int rightEntityID)
{
}
public void RemoveRelationLeft(int entityID)
{
}
public void RemoveRelationRight(int entityID)
{
}
#endregion
public ReadOnlySpan<IEcsPool> GetAllPools()
{
throw new NotImplementedException();
}
public int GetComponentID<T>()
{
throw new NotImplementedException();
}
public EcsPool<T> GetPool<T>() where T : struct
{
throw new NotImplementedException();
}
public EcsPool<T> UncheckedGetPool<T>() where T : struct
{
throw new NotImplementedException();
}
public EcsFilter Query<TComponent>() where TComponent : struct
{
throw new NotImplementedException();
}
public EcsFilter Filter<TInc>() where TInc : struct, IInc
{
throw new NotImplementedException();
}
public EcsFilter Filter<TInc, TExc>()
where TInc : struct, IInc
where TExc : struct, IExc
{
throw new NotImplementedException();
}
public bool IsMaskCompatible<TInc>(int entity) where TInc : struct, IInc
{
throw new NotImplementedException();
}
public bool IsMaskCompatible<TInc, TExc>(int entity)
where TInc : struct, IInc
where TExc : struct, IExc
{
throw new NotImplementedException();
}
public bool IsMaskCompatible(EcsMask mask, int entity)
{
throw new NotImplementedException();
}
public bool IsMaskCompatibleWithout(EcsMask mask, int entity, int otherPoolID)
{
throw new NotImplementedException();
}
void IEcsReadonlyTable.OnEntityComponentAdded(int entityID, int changedPoolID)
{
throw new NotImplementedException();
}
void IEcsReadonlyTable.OnEntityComponentRemoved(int entityID, int changedPoolID)
{
throw new NotImplementedException();
}
void IEcsReadonlyTable.RegisterGroup(EcsGroup group)
{
throw new NotImplementedException();
}
}*/
private EcsRelationWorld<TTableArhetype> _relationWorld;
}
}

View File

@ -3,6 +3,7 @@ using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Transactions;
namespace DCFApixels.DragonECS
{
@ -17,8 +18,6 @@ namespace DCFApixels.DragonECS
#endregion
#region Entities
public TQuery Query<TQuery>(out TQuery entities) where TQuery : IEcsQuery;
public ent NewEntity();
public void DelEntity(ent entity);
public bool EntityIsAlive(int entityID, short gen);
@ -71,12 +70,33 @@ namespace DCFApixels.DragonECS
private List<EcsQueryBase>[] _filtersByIncludedComponents;
private List<EcsQueryBase>[] _filtersByExcludedComponents;
private IEcsQuery[] _queries;
private EcsQueryBase[] _queries;
private EcsPipeline _pipeline;
private List<EcsGroup> _groups;
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
#region RunnersCache
private PoolRunnres _poolRunnres;
@ -112,10 +132,10 @@ namespace DCFApixels.DragonECS
_entityDispenser = new IntDispenser(0);
_nullPool = EcsNullPool.instance;
_pools = new IEcsPool[512];
FillArray(_pools, _nullPool);
ArrayUtility.Fill(_pools, _nullPool);
_gens = new short[512];
_queries = new EcsQuery<TWorldArchetype>[EntityArhetype.capacity];
_queries = new EcsQuery<TWorldArchetype>[QueryType.capacity];
_groups = new List<EcsGroup>(128);
_denseEntities = new int[512];
@ -141,7 +161,7 @@ namespace DCFApixels.DragonECS
{
int oldCapacity = _pools.Length;
Array.Resize(ref _pools, ComponentType.Capacity);
FillArray(_pools, _nullPool, oldCapacity, oldCapacity - _pools.Length);
ArrayUtility.Fill(_pools, _nullPool, oldCapacity, oldCapacity - _pools.Length);
Array.Resize(ref _filtersByIncludedComponents, ComponentType.Capacity);
Array.Resize(ref _filtersByExcludedComponents, ComponentType.Capacity);
@ -156,18 +176,18 @@ namespace DCFApixels.DragonECS
//public EcsPool<T> UncheckedGetPool<T>() where T : struct => (EcsPool<T>)_pools[ComponentType<T>.uniqueID];
#endregion
#region Entities
public TQuery Query<TQuery>(out TQuery entities) where TQuery : IEcsQuery
#region Query
public TQuery Query<TQuery>(out TQuery query) where TQuery : EcsQueryBase
{
int uniqueID = EntityArhetype<TQuery>.uniqueID;
if (_queries.Length < EntityArhetype.capacity)
Array.Resize(ref _queries, EntityArhetype.capacity);
int uniqueID = QueryType<TQuery>.uniqueID;
if (_queries.Length < QueryType.capacity)
Array.Resize(ref _queries, QueryType.capacity);
if (_queries[uniqueID] == null)
{
_queries[uniqueID] = EcsQuery<TWorldArchetype>.Builder.Build<TQuery>(this);
var mask = _queries[uniqueID].Mask;
var filter = (EcsQueryBase)_queries[uniqueID];
var mask = _queries[uniqueID].mask;
var filter = _queries[uniqueID];
for (int i = 0; i < mask.Inc.Length; i++)
{
@ -200,8 +220,8 @@ namespace DCFApixels.DragonECS
filter.AddEntity(entity);
}
}
entities = (TQuery)_queries[uniqueID];
return entities;
query = (TQuery)_queries[uniqueID];
return query;
}
#endregion
@ -217,20 +237,20 @@ namespace DCFApixels.DragonECS
return IsMaskCompatible(EcsMaskMap<TWorldArchetype>.GetMask<TInc, TExc>(), entityID);
}
public bool IsMaskCompatible(EcsComponentMask mask, int entity)
public bool IsMaskCompatible(EcsComponentMask mask, int entityID)
{
#if (DEBUG && !DISABLE_DRAGONECS_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS
if (mask.WorldArchetypeType != typeof(TWorldArchetype))
throw new EcsFrameworkException("mask.WorldArchetypeType != typeof(TWorldArchetype)");
throw new EcsFrameworkException("mask.WorldArchetypeType != typeof(TTableArhetype)");
#endif
for (int i = 0, iMax = mask.Inc.Length; i < iMax; i++)
{
if (!_pools[mask.Inc[i]].Has(entity))
if (!_pools[mask.Inc[i]].Has(entityID))
return false;
}
for (int i = 0, iMax = mask.Exc.Length; i < iMax; i++)
{
if (_pools[mask.Exc[i]].Has(entity))
if (_pools[mask.Exc[i]].Has(entityID))
return false;
}
return true;
@ -240,7 +260,7 @@ namespace DCFApixels.DragonECS
{
#if (DEBUG && !DISABLE_DRAGONECS_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS
if (mask.WorldArchetypeType != typeof(TWorldArchetype))
throw new EcsFrameworkException("mask.WorldArchetypeType != typeof(TWorldArchetype)");
throw new EcsFrameworkException("mask.WorldArchetypeType != typeof(TTableArhetype)");
#endif
for (int i = 0, iMax = mask.Inc.Length; i < iMax; i++)
{
@ -392,19 +412,19 @@ namespace DCFApixels.DragonECS
#endregion
#region Utils
internal static class EntityArhetype
internal static class QueryType
{
public static int increment = 0;
public static int capacity = 128;
}
internal static class EntityArhetype<TArhetype>
internal static class QueryType<TQuery>
{
public static int uniqueID;
static EntityArhetype()
static QueryType()
{
uniqueID = EntityArhetype.increment++;
if (EntityArhetype.increment > EntityArhetype.capacity)
EntityArhetype.capacity <<= 1;
uniqueID = QueryType.increment++;
if (QueryType.increment > QueryType.capacity)
QueryType.capacity <<= 1;
}
}
internal static class ComponentType
@ -436,22 +456,6 @@ namespace DCFApixels.DragonECS
ComponentType.types[uniqueID] = typeof(T);
}
}
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;
}
}
#endregion
}

View File

@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;
namespace DCFApixels.DragonECS
{
@ -7,26 +8,34 @@ namespace DCFApixels.DragonECS
#region Properties
/// <summary>Table Archetype</summary>
public Type ArchetypeType { get; }
internal int Count { get; }
internal int Capacity { get; }
public int Count { get; }
public int Capacity { get; }
#endregion
#region Methods
public ReadOnlySpan<IEcsPool> GetAllPools();
public int GetComponentID<T>();
public EcsPool<T> GetPool<T>() where T : struct;
public ReadOnlySpan<IEcsPool> GetAllPools();
public TQuery Query<TQuery>(out TQuery query) where TQuery : EcsQueryBase;
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;
public bool IsMaskCompatible(EcsComponentMask mask, int entity);
public bool IsMaskCompatibleWithout(EcsComponentMask mask, int entity, int otherPoolID);
public int GetComponentID<T>();
public bool IsMaskCompatible<TInc, TExc>(int entityID) where TInc : struct, IInc where TExc : struct, IExc;
public bool IsMaskCompatible(EcsComponentMask mask, int entityID);
public bool IsMaskCompatibleWithout(EcsComponentMask mask, int entity, int componentID);
#endregion
#region Internal Methods
internal void OnEntityComponentAdded(int entityID, int changedPoolID);
internal void OnEntityComponentRemoved(int entityID, int changedPoolID);
internal void OnEntityComponentAdded(int entityID, int componentID);
internal void OnEntityComponentRemoved(int entityID, int componentID);
internal void RegisterGroup(EcsGroup group);
#endregion
}
public static class IEcsReadonlyTableExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsMaskCompatible<TInc>(this IEcsReadonlyTable self, int entityID) where TInc : struct, IInc
{
return self.IsMaskCompatible<TInc, Exc>(entityID);
}
}
}

21
src/Utils/ArrayUtility.cs Normal file
View File

@ -0,0 +1,21 @@
namespace DCFApixels.DragonECS
{
internal static class ArrayUtility
{
public static void Fill<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;
}
}
}
}