DragonECS/src/EcsPool.cs

202 lines
6.8 KiB
C#
Raw Normal View History

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-02-08 17:07:39 +08:00
public interface IEcsPool
{
2023-04-17 22:58:52 +08:00
#region Properties
2023-04-06 23:40:47 +08:00
public Type ComponentType { get; }
public int ComponentID { get; }
2023-03-02 14:42:44 +08:00
public IEcsWorld 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 void Add(int entityID);
2023-04-01 21:16:08 +08:00
public void Write(int entityID);
2023-04-20 11:23:54 +08:00
public bool Has(int entityID);
2023-04-01 21:16:08 +08:00
public void Del(int entityID);
2023-04-01 20:45:37 +08:00
internal void OnWorldResize(int newSize);
2023-04-17 22:58:52 +08:00
#endregion
2023-02-08 17:07:39 +08:00
}
2023-04-06 23:40:47 +08:00
public interface IEcsPool<T> : IEcsPool where T : struct
2023-03-02 14:42:44 +08:00
{
2023-04-20 11:23:54 +08:00
public new ref T Add(int entityID);
public ref T Read(int entityID);
public new ref T Write(int entityID);
2023-03-02 14:42:44 +08:00
}
2023-03-13 04:32:24 +08:00
2023-04-06 23:40:47 +08:00
public struct NullComponent { }
2023-04-09 02:52:39 +08:00
public sealed class EcsNullPool : EcsPool, IEcsPool<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-10 22:22:17 +08:00
private IEcsWorld _source;
2023-04-06 23:40:47 +08:00
private NullComponent fakeComponent;
2023-04-17 22:58:52 +08:00
private EcsNullPool(IEcsWorld source) => _source = source;
#region Properties
2023-04-06 23:40:47 +08:00
public Type ComponentType => typeof(NullComponent);
public int ComponentID => -1;
2023-03-13 04:32:24 +08:00
public IEcsWorld World => _source;
2023-04-06 23:40:47 +08:00
public int Count => 0;
public int Capacity => 1;
2023-04-17 22:58:52 +08:00
#endregion
#region Methods
2023-04-20 11:23:54 +08:00
public ref NullComponent Add(int entity) => ref fakeComponent;
2023-04-09 02:52:39 +08:00
public override bool Has(int index) => false;
2023-04-10 22:22:17 +08:00
public ref NullComponent Read(int entity) => ref fakeComponent;
2023-04-06 23:40:47 +08:00
public ref NullComponent Write(int entity) => ref fakeComponent;
2023-04-20 11:23:54 +08:00
public void Del(int index) { }
void IEcsPool.Write(int entityID) { }
void IEcsPool.Add(int entityID) { }
2023-04-01 20:45:37 +08:00
void IEcsPool.OnWorldResize(int newSize) { }
2023-04-09 02:52:39 +08:00
internal override void OnWorldResize(int newSize) { }
2023-04-17 22:58:52 +08:00
#endregion
2023-03-13 04:32:24 +08:00
}
2023-04-09 02:52:39 +08:00
public abstract class EcsPool
{
public abstract bool Has(int entityID);
internal abstract void OnWorldResize(int newSize);
}
public sealed class EcsPool<T> : EcsPool, IEcsPool<T>
2023-02-09 02:26:43 +08:00
where T : struct
2023-02-05 19:59:45 +08:00
{
2023-04-06 23:40:47 +08:00
private readonly int _componentID;
2023-03-02 14:42:44 +08:00
private readonly IEcsWorld _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-01 20:45:37 +08:00
private PoolRunnres _poolRunnres;
2023-04-17 22:58:52 +08:00
2023-02-07 17:11:56 +08:00
#region Properites
2023-04-06 23:40:47 +08:00
public int Count => _itemsCount;
2023-04-01 20:45:37 +08:00
public int Capacity => _items.Length;
2023-03-02 14:42:44 +08:00
public IEcsWorld World => _source;
2023-04-06 23:40:47 +08:00
public Type ComponentType => typeof(T);
public int ComponentID => _componentID;
2023-02-07 17:11:56 +08:00
#endregion
#region Constructors
2023-04-01 20:45:37 +08:00
internal EcsPool(IEcsWorld source, int id, int capacity, PoolRunnres poolRunnres)
2023-02-06 01:27:32 +08:00
{
2023-02-07 17:11:56 +08:00
_source = source;
2023-04-06 23:40:47 +08:00
_componentID = id;
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-01 22:46:42 +08:00
_componentResetHandler = IEcsComponentReset<T>.Handler;
2023-04-01 20:45:37 +08:00
_poolRunnres = poolRunnres;
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-09 02:52:39 +08:00
public ref T Add(int entityID)
{
2023-04-10 22:22:17 +08:00
// using (_addMark.Auto())
// {
2023-04-09 02:52:39 +08:00
ref int itemIndex = ref _mapping[entityID];
if (itemIndex <= 0)
2023-04-01 20:45:37 +08:00
{
2023-04-09 02:52:39 +08:00
if (_recycledItemsCount > 0)
{
itemIndex = _recycledItems[--_recycledItemsCount];
_itemsCount++;
}
else
{
itemIndex = ++_itemsCount;
if (itemIndex >= _items.Length)
Array.Resize(ref _items, _items.Length << 1);
}
_mapping[entityID] = itemIndex;
_poolRunnres.add.OnComponentAdd<T>(entityID);
2023-04-17 22:58:52 +08:00
}
2023-04-09 02:52:39 +08:00
_poolRunnres.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-09 02:52:39 +08:00
public ref T Write(int entityID)
{
2023-04-10 22:22:17 +08:00
// using (_writeMark.Auto())
2023-04-20 10:59:55 +08:00
_poolRunnres.write.OnComponentWrite<T>(entityID);
2023-04-09 02:52:39 +08:00
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-10 22:22:17 +08:00
public ref T Read(int entityID)
2023-02-05 19:59:45 +08:00
{
2023-04-10 22:22:17 +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-09 02:52:39 +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
}
public 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--;
_poolRunnres.del.OnComponentDel<T>(entityID);
2023-04-10 22:22:17 +08:00
// }
2023-04-20 11:23:54 +08:00
}
2023-02-07 17:11:56 +08:00
#endregion
2023-03-13 04:32:24 +08:00
#region IEcsPool
2023-04-01 21:16:08 +08:00
void IEcsPool.Write(int entityID)
2023-02-05 19:59:45 +08:00
{
2023-04-01 21:16:08 +08:00
Write(entityID);
2023-02-05 19:59:45 +08:00
}
2023-04-20 11:23:54 +08:00
void IEcsPool.Add(int entityID)
{
Add(entityID);
}
2023-02-05 19:59:45 +08:00
#endregion
2023-02-08 17:57:06 +08:00
2023-04-06 23:40:47 +08:00
#region Object
public override bool Equals(object obj) => base.Equals(obj);
2023-04-15 00:23:46 +08:00
public override int GetHashCode() => _source.GetHashCode() ^ ~ComponentID;
2023-02-08 17:57:06 +08:00
#endregion
2023-03-30 16:43:22 +08:00
2023-04-06 23:40:47 +08:00
#region Internal
2023-04-09 02:52:39 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-04-01 20:45:37 +08:00
void IEcsPool.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-09 02:52:39 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal sealed override void OnWorldResize(int newSize)
{
Array.Resize(ref _mapping, newSize);
}
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
}