2023-02-08 17:57:06 +08:00
|
|
|
using System;
|
2023-03-30 16:45:43 +08:00
|
|
|
using System.Runtime.CompilerServices;
|
2023-04-01 20:45:37 +08:00
|
|
|
using Unity.Profiling;
|
2023-02-05 19:59:45 +08:00
|
|
|
|
|
|
|
namespace DCFApixels.DragonECS
|
|
|
|
{
|
2023-04-21 01:05:06 +08:00
|
|
|
public interface IEcsPoolBase
|
2023-02-08 17:07:39 +08:00
|
|
|
{
|
2023-04-17 22:58:52 +08:00
|
|
|
#region Properties
|
2023-04-06 23:40:47 +08:00
|
|
|
public Type ComponentType { get; }
|
2023-04-20 20:03:26 +08:00
|
|
|
public EcsWorld World { get; }
|
2023-04-06 23:40:47 +08:00
|
|
|
public int Count { get; }
|
|
|
|
public int Capacity { get; }
|
2023-04-17 22:58:52 +08:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Methods
|
2023-04-20 11:23:54 +08:00
|
|
|
public bool Has(int entityID);
|
2023-04-20 18:23:23 +08:00
|
|
|
#endregion
|
2023-04-21 01:05:06 +08:00
|
|
|
}
|
|
|
|
public interface IEcsReadonlyPool : IEcsPoolBase
|
|
|
|
{
|
|
|
|
#region Methods
|
|
|
|
public object Get(int entityID);
|
2023-04-17 22:58:52 +08:00
|
|
|
#endregion
|
2023-02-08 17:07:39 +08:00
|
|
|
}
|
2023-04-21 01:05:06 +08:00
|
|
|
public interface IEcsPool : IEcsReadonlyPool
|
2023-03-02 14:42:44 +08:00
|
|
|
{
|
2023-04-21 01:05:06 +08:00
|
|
|
#region Methods
|
|
|
|
public void AddOrWrite(int entityID, object data);
|
|
|
|
public void Del(int entityID);
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
public interface IEcsReadonlyPool<T> : IEcsReadonlyPool where T : struct
|
|
|
|
{
|
|
|
|
public ref readonly T Read(int entityID);
|
|
|
|
}
|
|
|
|
public interface IEcsPool<T> : IEcsPool, IEcsReadonlyPool<T> where T : struct
|
|
|
|
{
|
|
|
|
public ref T Add(int entityID);
|
2023-04-20 19:23:58 +08:00
|
|
|
public ref T Write(int entityID);
|
2023-03-13 04:32:24 +08:00
|
|
|
|
2023-04-20 19:23:58 +08:00
|
|
|
}
|
2023-04-21 01:05:06 +08:00
|
|
|
|
|
|
|
public abstract class EcsPoolBase<T> : IEcsPoolBase
|
|
|
|
where T : struct
|
|
|
|
{
|
|
|
|
#region Properties
|
|
|
|
public abstract Type ComponentType { get; }
|
|
|
|
public abstract EcsWorld World { get; }
|
|
|
|
public abstract int Count { get; }
|
|
|
|
public abstract int Capacity { get; }
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Methods
|
|
|
|
public abstract bool Has(int entityID);
|
|
|
|
|
|
|
|
protected abstract void OnWorldResize(int newSize);
|
|
|
|
protected abstract void OnDestroy();
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Internal
|
|
|
|
internal void InvokeOnWorldResize(int newSize) => OnWorldResize(newSize);
|
|
|
|
internal void InvokeOnDestroy() => OnDestroy();
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
|
2023-04-06 23:40:47 +08:00
|
|
|
public struct NullComponent { }
|
2023-04-21 01:05:06 +08:00
|
|
|
public sealed class EcsNullPool : EcsPoolBase<NullComponent>
|
2023-03-13 04:32:24 +08:00
|
|
|
{
|
2023-04-06 23:40:47 +08:00
|
|
|
public static EcsNullPool instance => new EcsNullPool(null);
|
2023-04-20 20:03:26 +08:00
|
|
|
private EcsWorld _source;
|
2023-04-06 23:40:47 +08:00
|
|
|
private NullComponent fakeComponent;
|
2023-04-20 20:03:26 +08:00
|
|
|
private EcsNullPool(EcsWorld source) => _source = source;
|
2023-04-17 22:58:52 +08:00
|
|
|
|
|
|
|
#region Properties
|
2023-04-21 01:05:06 +08:00
|
|
|
public sealed override Type ComponentType => typeof(NullComponent);
|
|
|
|
public sealed override EcsWorld World => _source;
|
|
|
|
public sealed override int Count => 0;
|
|
|
|
public sealed override int Capacity => 1;
|
2023-04-17 22:58:52 +08:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Methods
|
2023-04-21 01:05:06 +08:00
|
|
|
public sealed override ref NullComponent Add(int entity) => ref fakeComponent;
|
|
|
|
public sealed override bool Has(int index) => false;
|
|
|
|
public sealed override ref readonly NullComponent Read(int entity) => ref fakeComponent;
|
|
|
|
public sealed override ref NullComponent Write(int entity) => ref fakeComponent;
|
|
|
|
public sealed override void Del(int index) { }
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region WorldCallbacks
|
|
|
|
protected override void OnWorldResize(int newSize) { }
|
|
|
|
protected override void OnDestroy() { }
|
2023-04-17 22:58:52 +08:00
|
|
|
#endregion
|
2023-03-13 04:32:24 +08:00
|
|
|
}
|
2023-04-20 18:23:23 +08:00
|
|
|
|
2023-04-21 01:05:06 +08:00
|
|
|
public sealed class EcsPool<T> : EcsPoolBase<T>
|
2023-02-09 02:26:43 +08:00
|
|
|
where T : struct
|
2023-02-05 19:59:45 +08:00
|
|
|
{
|
2023-04-20 20:03:26 +08:00
|
|
|
public static EcsPool<T> Builder(EcsWorld source)
|
2023-04-20 18:23:23 +08:00
|
|
|
{
|
2023-04-21 01:05:06 +08:00
|
|
|
return new EcsPool<T>(source, 512, new PoolRunners(source.Pipeline));
|
2023-04-20 18:23:23 +08:00
|
|
|
}
|
|
|
|
|
2023-04-20 20:03:26 +08:00
|
|
|
private readonly EcsWorld _source;
|
2023-04-01 20:45:37 +08:00
|
|
|
|
2023-04-20 11:23:54 +08:00
|
|
|
private int[] _mapping;// index = entityID / value = itemIndex;/ value = 0 = no entityID
|
2023-04-01 20:45:37 +08:00
|
|
|
private T[] _items; //dense
|
|
|
|
private int _itemsCount;
|
|
|
|
private int[] _recycledItems;
|
|
|
|
private int _recycledItemsCount;
|
2023-02-05 19:59:45 +08:00
|
|
|
|
2023-03-30 16:43:22 +08:00
|
|
|
private IEcsComponentReset<T> _componentResetHandler;
|
2023-04-20 18:23:23 +08:00
|
|
|
private PoolRunners _poolRunners;
|
2023-04-17 22:58:52 +08:00
|
|
|
|
2023-02-07 17:11:56 +08:00
|
|
|
#region Properites
|
2023-04-21 01:05:06 +08:00
|
|
|
public sealed override int Count => _itemsCount;
|
|
|
|
public sealed override int Capacity => _items.Length;
|
|
|
|
public sealed override EcsWorld World => _source;
|
|
|
|
public sealed override Type ComponentType => typeof(T);
|
2023-02-07 17:11:56 +08:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Constructors
|
2023-04-21 01:05:06 +08:00
|
|
|
internal EcsPool(EcsWorld source, int capacity, PoolRunners poolRunners)
|
2023-02-06 01:27:32 +08:00
|
|
|
{
|
2023-02-07 17:11:56 +08:00
|
|
|
_source = source;
|
2023-02-09 02:26:43 +08:00
|
|
|
|
2023-04-18 19:35:42 +08:00
|
|
|
_mapping = new int[source.Capacity];
|
2023-04-01 20:45:37 +08:00
|
|
|
_recycledItems = new int[128];
|
|
|
|
_recycledItemsCount = 0;
|
2023-04-01 21:16:08 +08:00
|
|
|
_items = new T[capacity];
|
2023-04-01 20:45:37 +08:00
|
|
|
_itemsCount = 0;
|
2023-03-30 16:43:22 +08:00
|
|
|
|
2023-04-20 18:23:23 +08:00
|
|
|
_componentResetHandler = EcsComponentResetHandler<T>.instance;
|
2023-04-21 01:05:06 +08:00
|
|
|
_poolRunners = poolRunners;
|
2023-02-06 01:27:32 +08:00
|
|
|
}
|
2023-02-07 17:11:56 +08:00
|
|
|
#endregion
|
2023-02-06 01:27:32 +08:00
|
|
|
|
2023-04-01 20:45:37 +08:00
|
|
|
#region Write/Read/Has/Del
|
2023-04-09 02:52:39 +08:00
|
|
|
private ProfilerMarker _addMark = new ProfilerMarker("EcsPoo.Add");
|
2023-04-01 20:45:37 +08:00
|
|
|
private ProfilerMarker _writeMark = new ProfilerMarker("EcsPoo.Write");
|
|
|
|
private ProfilerMarker _readMark = new ProfilerMarker("EcsPoo.Read");
|
|
|
|
private ProfilerMarker _hasMark = new ProfilerMarker("EcsPoo.Has");
|
|
|
|
private ProfilerMarker _delMark = new ProfilerMarker("EcsPoo.Del");
|
2023-04-21 01:05:06 +08:00
|
|
|
public sealed override ref T Add(int entityID)
|
2023-02-13 21:11:54 +08:00
|
|
|
{
|
2023-04-20 18:23:23 +08:00
|
|
|
// using (_addMark.Auto())
|
|
|
|
// {
|
|
|
|
ref int itemIndex = ref _mapping[entityID];
|
|
|
|
if (itemIndex <= 0)
|
|
|
|
{
|
|
|
|
if (_recycledItemsCount > 0)
|
2023-04-01 20:45:37 +08:00
|
|
|
{
|
2023-04-20 18:23:23 +08:00
|
|
|
itemIndex = _recycledItems[--_recycledItemsCount];
|
|
|
|
_itemsCount++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
itemIndex = ++_itemsCount;
|
|
|
|
if (itemIndex >= _items.Length)
|
|
|
|
Array.Resize(ref _items, _items.Length << 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
_mapping[entityID] = itemIndex;
|
|
|
|
_poolRunners.add.OnComponentAdd<T>(entityID);
|
2023-04-17 22:58:52 +08:00
|
|
|
}
|
2023-04-20 18:23:23 +08:00
|
|
|
_poolRunners.write.OnComponentWrite<T>(entityID);
|
|
|
|
return ref _items[itemIndex];
|
2023-04-20 10:59:55 +08:00
|
|
|
// }
|
2023-04-09 02:52:39 +08:00
|
|
|
}
|
2023-04-17 22:58:52 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-04-21 01:05:06 +08:00
|
|
|
public sealed override ref T Write(int entityID)
|
2023-04-09 02:52:39 +08:00
|
|
|
{
|
2023-04-20 18:23:23 +08:00
|
|
|
// using (_writeMark.Auto())
|
|
|
|
_poolRunners.write.OnComponentWrite<T>(entityID);
|
|
|
|
return ref _items[_mapping[entityID]];
|
2023-04-01 20:45:37 +08:00
|
|
|
}
|
2023-02-05 19:59:45 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-04-21 01:05:06 +08:00
|
|
|
public sealed override ref readonly T Read(int entityID)
|
2023-02-05 19:59:45 +08:00
|
|
|
{
|
2023-04-20 18:23:23 +08:00
|
|
|
// using (_readMark.Auto())
|
2023-04-01 21:16:08 +08:00
|
|
|
return ref _items[_mapping[entityID]];
|
2023-02-05 19:59:45 +08:00
|
|
|
}
|
2023-02-07 17:11:56 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-04-21 01:05:06 +08:00
|
|
|
public sealed override bool Has(int entityID)
|
2023-04-01 20:45:37 +08:00
|
|
|
{
|
2023-04-17 22:58:52 +08:00
|
|
|
// using (_hasMark.Auto())
|
2023-04-01 21:16:08 +08:00
|
|
|
return _mapping[entityID] > 0;
|
2023-04-01 20:45:37 +08:00
|
|
|
}
|
2023-04-21 01:05:06 +08:00
|
|
|
public sealed override void Del(int entityID)
|
2023-02-07 17:11:56 +08:00
|
|
|
{
|
2023-04-20 11:23:54 +08:00
|
|
|
// using (_delMark.Auto())
|
|
|
|
// {
|
|
|
|
ref int itemIndex = ref _mapping[entityID];
|
|
|
|
_componentResetHandler.Reset(ref _items[itemIndex]);
|
2023-04-01 21:16:08 +08:00
|
|
|
if (_recycledItemsCount >= _recycledItems.Length)
|
2023-04-01 20:45:37 +08:00
|
|
|
Array.Resize(ref _recycledItems, _recycledItems.Length << 1);
|
2023-04-20 11:23:54 +08:00
|
|
|
_recycledItems[_recycledItemsCount++] = itemIndex;
|
|
|
|
itemIndex = 0;
|
2023-04-01 20:45:37 +08:00
|
|
|
_itemsCount--;
|
2023-04-20 18:23:23 +08:00
|
|
|
_poolRunners.del.OnComponentDel<T>(entityID);
|
|
|
|
// }
|
2023-04-20 11:23:54 +08:00
|
|
|
}
|
2023-02-07 17:11:56 +08:00
|
|
|
#endregion
|
|
|
|
|
2023-04-21 01:05:06 +08:00
|
|
|
#region WorldCallbacks
|
|
|
|
protected override void OnWorldResize(int newSize)
|
2023-03-30 16:43:22 +08:00
|
|
|
{
|
2023-04-01 20:45:37 +08:00
|
|
|
Array.Resize(ref _mapping, newSize);
|
2023-03-30 16:43:22 +08:00
|
|
|
}
|
2023-04-21 01:05:06 +08:00
|
|
|
protected override void OnDestroy() { }
|
2023-04-06 23:40:47 +08:00
|
|
|
#endregion
|
2023-03-30 16:43:22 +08:00
|
|
|
}
|
2023-02-05 19:59:45 +08:00
|
|
|
}
|