DragonECS/src/EcsPool.cs

166 lines
5.6 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-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-01 21:16:08 +08:00
public bool Has(int entityID);
public void Write(int entityID);
public void Del(int entityID);
2023-04-01 20:45:37 +08:00
internal void OnWorldResize(int newSize);
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
{
public ref readonly T Read(int entity);
public new ref T Write(int entity);
}
2023-03-13 04:32:24 +08:00
2023-04-06 23:40:47 +08:00
public struct NullComponent { }
public sealed class EcsNullPool : 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-03-13 04:32:24 +08:00
private readonly IEcsWorld _source;
2023-04-06 23:40:47 +08:00
private EcsNullPool(IEcsWorld source) => _source = source;
private NullComponent fakeComponent;
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-03-13 04:32:24 +08:00
public void Del(int index) { }
public bool Has(int index) => false;
2023-04-06 23:40:47 +08:00
void IEcsPool.Write(int entityID) { }
public ref readonly NullComponent Read(int entity) => ref fakeComponent;
public ref NullComponent Write(int entity) => ref fakeComponent;
2023-04-01 20:45:37 +08:00
void IEcsPool.OnWorldResize(int newSize) { }
2023-03-13 04:32:24 +08:00
}
2023-04-01 21:16:08 +08:00
2023-04-06 23:40:47 +08:00
public sealed class EcsPool<T> : 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
private int[] _mapping;// index = entity / value = itemIndex;/ value = 0 = no entity
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-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-01 20:45:37 +08:00
_mapping = new int[source.EntitesCapacity];
_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
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");
public ref T Write(int entityID)
{
2023-04-01 21:16:08 +08:00
// using (_writeMark.Auto())
// {
2023-04-01 20:45:37 +08:00
ref int itemIndex = ref _mapping[entityID];
if (itemIndex <= 0) //<2F><><EFBFBD><EFBFBD> 0 <20><> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2023-02-27 18:16:23 +08:00
{
2023-04-01 20:45:37 +08:00
if (_recycledItemsCount > 0)
{
itemIndex = _recycledItems[--_recycledItemsCount];
_itemsCount++;
}
else
{
itemIndex = _itemsCount++;
2023-04-01 21:16:08 +08:00
if (itemIndex >= _items.Length)
2023-04-01 20:45:37 +08:00
Array.Resize(ref _items, _items.Length << 1);
}
_mapping[entityID] = itemIndex;
_componentResetHandler.Reset(ref _items[itemIndex]);
2023-04-06 23:40:47 +08:00
_source.OnEntityComponentAdded(entityID, _componentID);
2023-04-01 20:45:37 +08:00
_poolRunnres.add.OnComponentAdd<T>(entityID);
2023-02-27 18:16:23 +08:00
}
2023-04-01 20:45:37 +08:00
_poolRunnres.write.OnComponentWrite<T>(entityID);
return ref _items[itemIndex];
// }
}
2023-02-05 19:59:45 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-04-01 20:45:37 +08:00
public ref readonly T Read(int entityID)
2023-02-05 19:59:45 +08:00
{
2023-04-01 21:16:08 +08:00
// using (_readMark.Auto())
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-01 20:45:37 +08:00
public bool Has(int entityID)
{
2023-04-01 21:16:08 +08:00
// using (_hasMark.Auto())
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-01 20:45:37 +08:00
//using (_delMark.Auto())
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);
_recycledItems[_recycledItemsCount++] = _mapping[entityID];
_mapping[entityID] = 0;
_itemsCount--;
2023-04-06 23:40:47 +08:00
_source.OnEntityComponentRemoved(entityID, _componentID);
2023-04-01 20:45:37 +08:00
_poolRunnres.del.OnComponentDel<T>(entityID);
2023-04-01 21:16:08 +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
}
#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);
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-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-06 23:40:47 +08:00
#endregion
2023-03-30 16:43:22 +08:00
}
2023-02-05 19:59:45 +08:00
}