mirror of
https://github.com/DCFApixels/DragonECS.git
synced 2025-09-18 09:54:35 +08:00
simple refactoring
This commit is contained in:
parent
5fe36063f2
commit
29dc825685
@ -179,10 +179,10 @@ namespace DCFApixels.DragonECS
|
|||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
private struct Combined
|
private readonly struct Combined
|
||||||
{
|
{
|
||||||
public EcsAspect aspect;
|
public readonly EcsAspect aspect;
|
||||||
public int order;
|
public readonly int order;
|
||||||
public Combined(EcsAspect aspect, int order)
|
public Combined(EcsAspect aspect, int order)
|
||||||
{
|
{
|
||||||
this.aspect = aspect;
|
this.aspect = aspect;
|
||||||
|
@ -7,11 +7,11 @@ using static DCFApixels.DragonECS.EcsPoolThrowHalper;
|
|||||||
namespace DCFApixels.DragonECS
|
namespace DCFApixels.DragonECS
|
||||||
{
|
{
|
||||||
/// <summary>Pool for IEcsComponent components</summary>
|
/// <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
|
where T : struct, IEcsComponent
|
||||||
{
|
{
|
||||||
private EcsWorld _source;
|
private EcsWorld _source;
|
||||||
private int _id;
|
private int _componentID;
|
||||||
|
|
||||||
private int[] _mapping;// index = entityID / value = itemIndex;/ value = 0 = no entityID
|
private int[] _mapping;// index = entityID / value = itemIndex;/ value = 0 = no entityID
|
||||||
private T[] _items; //dense
|
private T[] _items; //dense
|
||||||
@ -19,15 +19,15 @@ namespace DCFApixels.DragonECS
|
|||||||
private int[] _recycledItems;
|
private int[] _recycledItems;
|
||||||
private int _recycledItemsCount;
|
private int _recycledItemsCount;
|
||||||
|
|
||||||
private IEcsComponentReset<T> _componentResetHandler;
|
private IEcsComponentReset<T> _componentResetHandler = EcsComponentResetHandler<T>.instance;
|
||||||
private IEcsComponentCopy<T> _componentCopyHandler;
|
private IEcsComponentCopy<T> _componentCopyHandler = EcsComponentCopyHandler<T>.instance;
|
||||||
|
|
||||||
private List<IEcsPoolEventListener> _listeners;
|
private List<IEcsPoolEventListener> _listeners = new List<IEcsPoolEventListener>();
|
||||||
|
|
||||||
#region Properites
|
#region Properites
|
||||||
public int Count => _itemsCount;
|
public int Count => _itemsCount;
|
||||||
public int Capacity => _items.Length;
|
public int Capacity => _items.Length;
|
||||||
public int ComponentID => _id;
|
public int ComponentID => _componentID;
|
||||||
public Type ComponentType => typeof(T);
|
public Type ComponentType => typeof(T);
|
||||||
public EcsWorld World => _source;
|
public EcsWorld World => _source;
|
||||||
#endregion
|
#endregion
|
||||||
@ -36,7 +36,7 @@ namespace DCFApixels.DragonECS
|
|||||||
void IEcsPoolImplementation.OnInit(EcsWorld world, int componentID)
|
void IEcsPoolImplementation.OnInit(EcsWorld world, int componentID)
|
||||||
{
|
{
|
||||||
_source = world;
|
_source = world;
|
||||||
_id = componentID;
|
_componentID = componentID;
|
||||||
|
|
||||||
const int capacity = 512;
|
const int capacity = 512;
|
||||||
|
|
||||||
@ -45,11 +45,6 @@ namespace DCFApixels.DragonECS
|
|||||||
_recycledItemsCount = 0;
|
_recycledItemsCount = 0;
|
||||||
_items = new T[capacity];
|
_items = new T[capacity];
|
||||||
_itemsCount = 0;
|
_itemsCount = 0;
|
||||||
|
|
||||||
_listeners = new List<IEcsPoolEventListener>();
|
|
||||||
|
|
||||||
_componentResetHandler = EcsComponentResetHandler<T>.instance;
|
|
||||||
_componentCopyHandler = EcsComponentCopyHandler<T>.instance;
|
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@ -171,8 +166,8 @@ namespace DCFApixels.DragonECS
|
|||||||
void IEcsPool.AddRaw(int entityID, object dataRaw) => Add(entityID) = (T)dataRaw;
|
void IEcsPool.AddRaw(int entityID, object dataRaw) => Add(entityID) = (T)dataRaw;
|
||||||
object IEcsPool.GetRaw(int entityID) => Read(entityID);
|
object IEcsPool.GetRaw(int entityID) => Read(entityID);
|
||||||
void IEcsPool.SetRaw(int entityID, object dataRaw) => Get(entityID) = (T)dataRaw;
|
void IEcsPool.SetRaw(int entityID, object dataRaw) => Get(entityID) = (T)dataRaw;
|
||||||
ref readonly T IEcsStructsPool<T>.Read(int entityID) => ref Read(entityID);
|
ref readonly T IEcsStructPool<T>.Read(int entityID) => ref Read(entityID);
|
||||||
ref T IEcsStructsPool<T>.Get(int entityID) => ref Get(entityID);
|
ref T IEcsStructPool<T>.Get(int entityID) => ref Get(entityID);
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Listeners
|
#region Listeners
|
||||||
|
@ -30,12 +30,17 @@ namespace DCFApixels.DragonECS
|
|||||||
void RemoveListener(IEcsPoolEventListener listener);
|
void RemoveListener(IEcsPoolEventListener listener);
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
public interface IEcsStructsPool<T>
|
public interface IEcsStructPool<T> : IEcsPool
|
||||||
{
|
{
|
||||||
ref T Add(int entityID);
|
ref T Add(int entityID);
|
||||||
ref readonly T Read(int entityID);
|
ref readonly T Read(int entityID);
|
||||||
ref T Get(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>
|
/// <summary>Only used to implement a custom pool. In other contexts use IEcsPool or IEcsPool<T>.</summary>
|
||||||
public interface IEcsPoolImplementation : IEcsPool
|
public interface IEcsPoolImplementation : IEcsPool
|
||||||
{
|
{
|
||||||
|
@ -6,23 +6,23 @@ using static DCFApixels.DragonECS.EcsPoolThrowHalper;
|
|||||||
|
|
||||||
namespace DCFApixels.DragonECS
|
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
|
where T : struct, IEcsTagComponent
|
||||||
{
|
{
|
||||||
private EcsWorld _source;
|
private EcsWorld _source;
|
||||||
private int _id;
|
private int _componentID;
|
||||||
|
|
||||||
private bool[] _mapping;// index = entityID / value = itemIndex;/ value = 0 = no entityID
|
private bool[] _mapping;// index = entityID / value = itemIndex;/ value = 0 = no entityID
|
||||||
private int _count;
|
private int _count;
|
||||||
|
|
||||||
private List<IEcsPoolEventListener> _listeners;
|
private List<IEcsPoolEventListener> _listeners = new List<IEcsPoolEventListener>();
|
||||||
|
|
||||||
private T _fakeComponent;
|
private T _fakeComponent;
|
||||||
|
|
||||||
#region Properites
|
#region Properites
|
||||||
public int Count => _count;
|
public int Count => _count;
|
||||||
int IEcsPool.Capacity => -1;
|
int IEcsPool.Capacity => -1;
|
||||||
public int ComponentID => _id;
|
public int ComponentID => _componentID;
|
||||||
public Type ComponentType => typeof(T);
|
public Type ComponentType => typeof(T);
|
||||||
public EcsWorld World => _source;
|
public EcsWorld World => _source;
|
||||||
#endregion
|
#endregion
|
||||||
@ -31,12 +31,10 @@ namespace DCFApixels.DragonECS
|
|||||||
void IEcsPoolImplementation.OnInit(EcsWorld world, int componentID)
|
void IEcsPoolImplementation.OnInit(EcsWorld world, int componentID)
|
||||||
{
|
{
|
||||||
_source = world;
|
_source = world;
|
||||||
_id = componentID;
|
_componentID = componentID;
|
||||||
|
|
||||||
_mapping = new bool[world.Capacity];
|
_mapping = new bool[world.Capacity];
|
||||||
_count = 0;
|
_count = 0;
|
||||||
|
|
||||||
_listeners = new List<IEcsPoolEventListener>();
|
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@ -131,19 +129,19 @@ namespace DCFApixels.DragonECS
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Other
|
#region Other
|
||||||
ref T IEcsStructsPool<T>.Add(int entityID)
|
ref T IEcsStructPool<T>.Add(int entityID)
|
||||||
{
|
{
|
||||||
Add(entityID);
|
Add(entityID);
|
||||||
return ref _fakeComponent;
|
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 (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
||||||
if (!Has(entityID)) ThrowNotHaveComponent<T>(entityID);
|
if (!Has(entityID)) ThrowNotHaveComponent<T>(entityID);
|
||||||
#endif
|
#endif
|
||||||
return ref _fakeComponent;
|
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 (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
||||||
if (!Has(entityID)) ThrowNotHaveComponent<T>(entityID);
|
if (!Has(entityID)) ThrowNotHaveComponent<T>(entityID);
|
||||||
|
@ -33,7 +33,7 @@ namespace DCFApixels.DragonECS
|
|||||||
public bool IsNull
|
public bool IsNull
|
||||||
{
|
{
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
get => this == NULL;
|
get => full == 0l;
|
||||||
}
|
}
|
||||||
public int ID
|
public int ID
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user