simple refactoring

This commit is contained in:
Mikhail 2023-06-25 23:13:51 +08:00
parent 5fe36063f2
commit 29dc825685
5 changed files with 27 additions and 29 deletions

View File

@ -179,10 +179,10 @@ namespace DCFApixels.DragonECS
}
#endregion
private struct Combined
private readonly struct Combined
{
public EcsAspect aspect;
public int order;
public readonly EcsAspect aspect;
public readonly int order;
public Combined(EcsAspect aspect, int order)
{
this.aspect = aspect;

View File

@ -7,11 +7,11 @@ using static DCFApixels.DragonECS.EcsPoolThrowHalper;
namespace DCFApixels.DragonECS
{
/// <summary>Pool for IEcsComponent components</summary>
public sealed class EcsPool<T> : IEcsPoolImplementation<T>, IEcsStructsPool<T>, IEnumerable<T> //IEnumerable<T> - IntelliSense hack
public sealed class EcsPool<T> : IEcsPoolImplementation<T>, IEcsStructPool<T>, IEnumerable<T> //IEnumerable<T> - IntelliSense hack
where T : struct, IEcsComponent
{
private EcsWorld _source;
private int _id;
private int _componentID;
private int[] _mapping;// index = entityID / value = itemIndex;/ value = 0 = no entityID
private T[] _items; //dense
@ -19,15 +19,15 @@ namespace DCFApixels.DragonECS
private int[] _recycledItems;
private int _recycledItemsCount;
private IEcsComponentReset<T> _componentResetHandler;
private IEcsComponentCopy<T> _componentCopyHandler;
private IEcsComponentReset<T> _componentResetHandler = EcsComponentResetHandler<T>.instance;
private IEcsComponentCopy<T> _componentCopyHandler = EcsComponentCopyHandler<T>.instance;
private List<IEcsPoolEventListener> _listeners;
private List<IEcsPoolEventListener> _listeners = new List<IEcsPoolEventListener>();
#region Properites
public int Count => _itemsCount;
public int Capacity => _items.Length;
public int ComponentID => _id;
public int ComponentID => _componentID;
public Type ComponentType => typeof(T);
public EcsWorld World => _source;
#endregion
@ -36,7 +36,7 @@ namespace DCFApixels.DragonECS
void IEcsPoolImplementation.OnInit(EcsWorld world, int componentID)
{
_source = world;
_id = componentID;
_componentID = componentID;
const int capacity = 512;
@ -45,11 +45,6 @@ namespace DCFApixels.DragonECS
_recycledItemsCount = 0;
_items = new T[capacity];
_itemsCount = 0;
_listeners = new List<IEcsPoolEventListener>();
_componentResetHandler = EcsComponentResetHandler<T>.instance;
_componentCopyHandler = EcsComponentCopyHandler<T>.instance;
}
#endregion
@ -171,8 +166,8 @@ namespace DCFApixels.DragonECS
void IEcsPool.AddRaw(int entityID, object dataRaw) => Add(entityID) = (T)dataRaw;
object IEcsPool.GetRaw(int entityID) => Read(entityID);
void IEcsPool.SetRaw(int entityID, object dataRaw) => Get(entityID) = (T)dataRaw;
ref readonly T IEcsStructsPool<T>.Read(int entityID) => ref Read(entityID);
ref T IEcsStructsPool<T>.Get(int entityID) => ref Get(entityID);
ref readonly T IEcsStructPool<T>.Read(int entityID) => ref Read(entityID);
ref T IEcsStructPool<T>.Get(int entityID) => ref Get(entityID);
#endregion
#region Listeners

View File

@ -30,12 +30,17 @@ namespace DCFApixels.DragonECS
void RemoveListener(IEcsPoolEventListener listener);
#endregion
}
public interface IEcsStructsPool<T>
public interface IEcsStructPool<T> : IEcsPool
{
ref T Add(int entityID);
ref readonly T Read(int entityID);
ref T Get(int entityID);
}
public interface IEcsClassPool<T> : IEcsPool
{
T Add(int entityID);
T Get(int entityID);
}
/// <summary>Only used to implement a custom pool. In other contexts use IEcsPool or IEcsPool<T>.</summary>
public interface IEcsPoolImplementation : IEcsPool
{

View File

@ -6,23 +6,23 @@ using static DCFApixels.DragonECS.EcsPoolThrowHalper;
namespace DCFApixels.DragonECS
{
public sealed class EcsTagPool<T> : IEcsPoolImplementation<T>, IEcsStructsPool<T>, IEnumerable<T> //IEnumerable<T> - IntelliSense hack
public sealed class EcsTagPool<T> : IEcsPoolImplementation<T>, IEcsStructPool<T>, IEnumerable<T> //IEnumerable<T> - IntelliSense hack
where T : struct, IEcsTagComponent
{
private EcsWorld _source;
private int _id;
private int _componentID;
private bool[] _mapping;// index = entityID / value = itemIndex;/ value = 0 = no entityID
private int _count;
private List<IEcsPoolEventListener> _listeners;
private List<IEcsPoolEventListener> _listeners = new List<IEcsPoolEventListener>();
private T _fakeComponent;
#region Properites
public int Count => _count;
int IEcsPool.Capacity => -1;
public int ComponentID => _id;
public int ComponentID => _componentID;
public Type ComponentType => typeof(T);
public EcsWorld World => _source;
#endregion
@ -31,12 +31,10 @@ namespace DCFApixels.DragonECS
void IEcsPoolImplementation.OnInit(EcsWorld world, int componentID)
{
_source = world;
_id = componentID;
_componentID = componentID;
_mapping = new bool[world.Capacity];
_count = 0;
_listeners = new List<IEcsPoolEventListener>();
}
#endregion
@ -131,19 +129,19 @@ namespace DCFApixels.DragonECS
#endregion
#region Other
ref T IEcsStructsPool<T>.Add(int entityID)
ref T IEcsStructPool<T>.Add(int entityID)
{
Add(entityID);
return ref _fakeComponent;
}
ref readonly T IEcsStructsPool<T>.Read(int entityID)
ref readonly T IEcsStructPool<T>.Read(int entityID)
{
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
if (!Has(entityID)) ThrowNotHaveComponent<T>(entityID);
#endif
return ref _fakeComponent;
}
ref T IEcsStructsPool<T>.Get(int entityID)
ref T IEcsStructPool<T>.Get(int entityID)
{
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
if (!Has(entityID)) ThrowNotHaveComponent<T>(entityID);

View File

@ -33,7 +33,7 @@ namespace DCFApixels.DragonECS
public bool IsNull
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => this == NULL;
get => full == 0l;
}
public int ID
{