expand functionality. redesign IEcsReadonlyGroup into EcsReadonlyGroup

This commit is contained in:
Mikhail 2023-03-30 20:47:39 +08:00
parent db3e633a05
commit f8d03cf949
4 changed files with 66 additions and 26 deletions

View File

@ -7,7 +7,7 @@ namespace DCFApixels.DragonECS
#region Properties #region Properties
public IEcsWorld World { get; } public IEcsWorld World { get; }
public EcsMask Mask { get; } public EcsMask Mask { get; }
public IEcsReadonlyGroup Entities { get; } public EcsReadonlyGroup Entities { get; }
public int EntitiesCount { get; } public int EntitiesCount { get; }
#endregion #endregion
} }
@ -29,10 +29,10 @@ namespace DCFApixels.DragonECS
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _mask; get => _mask;
} }
public IEcsReadonlyGroup Entities public EcsReadonlyGroup Entities
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _entities; get => _entities.Readonly;
} }
public int EntitiesCount public int EntitiesCount
{ {

View File

@ -1,26 +1,33 @@
using System; using System;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using delayedOp = System.Int32; using delayedOp = System.Int32;
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
public interface IEcsReadonlyGroup [StructLayout(LayoutKind.Sequential, Pack = 0, Size = 8)]
public readonly ref struct EcsReadonlyGroup
{ {
int Count { get; } private readonly EcsGroup _source;
bool Contains(int entityID);
EcsGroup.Enumerator GetEnumerator(); [MethodImpl(MethodImplOptions.AggressiveInlining)]
} public EcsReadonlyGroup(EcsGroup source) => _source = source;
public interface IEcsGroup : IEcsReadonlyGroup public int Count
{ {
void Add(int entityID); [MethodImpl(MethodImplOptions.AggressiveInlining)]
void Remove(int entityID); get => _source.Count;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(int entityID) => _source.Contains(entityID);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public EcsGroup.Enumerator GetEnumerator() => _source.GetEnumerator();
} }
// не может содержать значение 0 // не может содержать значение 0
// _delayedOps это int[] для отложенных операций, хранятся отложенные операции в виде int значения, если старший бит = 0 то это опреация добавленияб если = 1 то это операция вычитания // _delayedOps это int[] для отложенных операций, хранятся отложенные операции в виде int значения, если старший бит = 0 то это опреация добавленияб если = 1 то это операция вычитания
// this collection can only store numbers greater than 0 // this collection can only store numbers greater than 0
public class EcsGroup : IEcsGroup public class EcsGroup
{ {
public const int DEALAYED_ADD = 0; public const int DEALAYED_ADD = 0;
public const int DEALAYED_REMOVE = int.MinValue; public const int DEALAYED_REMOVE = int.MinValue;
@ -44,6 +51,11 @@ namespace DCFApixels.DragonECS
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _count; get => _count;
} }
public EcsReadonlyGroup Readonly
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new EcsReadonlyGroup(this);
}
#endregion #endregion
#region Constrcutors #region Constrcutors
@ -86,7 +98,7 @@ namespace DCFApixels.DragonECS
if(++_count >= _dense.Length) if(++_count >= _dense.Length)
Array.Resize(ref _dense, _dense.Length << 1); Array.Resize(ref _dense, _dense.Length << 1);
if (entityID > _sparse.Length) if (entityID >= _sparse.Length)
{ {
int neadedSpace = _sparse.Length; int neadedSpace = _sparse.Length;
while (entityID >= neadedSpace) while (entityID >= neadedSpace)
@ -128,20 +140,21 @@ namespace DCFApixels.DragonECS
//TODO добавить автосоритровку при каждом GetEnumerator //TODO добавить автосоритровку при каждом GetEnumerator
#region AddGroup/RemoveGroup #region AddGroup/RemoveGroup
public void AddGroup(IEcsReadonlyGroup group) public void AddGroup(EcsReadonlyGroup group)
{ {
foreach (var item in group) foreach (var item in group) Add(item.id);
{
Add(item.id);
}
} }
public void RemoveGroup(EcsReadonlyGroup group)
public void RemoveGroup(IEcsReadonlyGroup group)
{ {
foreach (var item in group) foreach (var item in group) Remove(item.id);
{ }
Remove(item.id); public void AddGroup(EcsGroup group)
} {
foreach (var item in group) Add(item.id);
}
public void RemoveGroup(EcsGroup group)
{
foreach (var item in group) Remove(item.id);
} }
#endregion #endregion
@ -179,7 +192,7 @@ namespace DCFApixels.DragonECS
} }
#endregion #endregion
#region Utils #region Enumerator
public struct Enumerator : IDisposable public struct Enumerator : IDisposable
{ {
private readonly EcsGroup _source; private readonly EcsGroup _source;

View File

@ -6,7 +6,11 @@ namespace DCFApixels.DragonECS
{ {
public interface IEcsPool public interface IEcsPool
{ {
public int EntitiesCount { get; }
public int Capacity { get; }
public IEcsWorld World { get; } public IEcsWorld World { get; }
public Type DataType { get; }
public int ID { get; } public int ID { get; }
public bool Has(int index); public bool Has(int index);
public void Write(int index); public void Write(int index);
@ -30,7 +34,12 @@ namespace DCFApixels.DragonECS
} }
public IEcsWorld World => _source; public IEcsWorld World => _source;
public Type DataType => typeof(void);
public int ID => -1; public int ID => -1;
public int EntitiesCount => 0;
public int Capacity => 1;
public void Del(int index) { } public void Del(int index) { }
public bool Has(int index) => false; public bool Has(int index) => false;
public void Write(int index) { } public void Write(int index) { }
@ -47,7 +56,11 @@ namespace DCFApixels.DragonECS
private IEcsComponentReset<T> _componentResetHandler; private IEcsComponentReset<T> _componentResetHandler;
#region Properites #region Properites
public int EntitiesCount => _sparseSet.Count;
public int Capacity => _sparseSet.CapacityDense;
public IEcsWorld World => _source; public IEcsWorld World => _source;
public Type DataType => typeof(T);
public int ID => _id; public int ID => _id;
#endregion #endregion
@ -112,6 +125,7 @@ namespace DCFApixels.DragonECS
#endregion #endregion
} }
#region ComponentResetHandler
internal static class ComponentResetHandler internal static class ComponentResetHandler
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -137,4 +151,5 @@ namespace DCFApixels.DragonECS
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Reset(ref T component) => _fakeInstnace.Reset(ref component); public void Reset(ref T component) => _fakeInstnace.Reset(ref component);
} }
#endregion
} }

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
@ -13,6 +14,12 @@ namespace DCFApixels.DragonECS
public int ID { get; } public int ID { get; }
#endregion #endregion
#region GetterMethods
public ReadOnlySpan<IEcsPool> GetAllPools();
#endregion
#region Methods #region Methods
public EcsPool<T> GetPool<T>() where T : struct; public EcsPool<T> GetPool<T>() where T : struct;
public EcsFilter Filter<TInc>() where TInc : struct, IInc; public EcsFilter Filter<TInc>() where TInc : struct, IInc;
@ -68,6 +75,11 @@ namespace DCFApixels.DragonECS
private EcsFilter[] _filters; private EcsFilter[] _filters;
#region GetterMethods
public ReadOnlySpan<IEcsPool> GetAllPools() => new ReadOnlySpan<IEcsPool>(_pools);
#endregion
#region Properties #region Properties
public bool IsEmpty => _entities.Count < 0; public bool IsEmpty => _entities.Count < 0;
public Type ArchetypeType => typeof(TArchetype); public Type ArchetypeType => typeof(TArchetype);