DragonECS/src/EcsPool.cs

178 lines
5.3 KiB
C#
Raw Normal View History

2023-02-08 17:57:06 +08:00
using System;
2023-02-09 02:26:43 +08:00
using System.Linq;
2023-03-30 16:45:43 +08:00
using System.Runtime.CompilerServices;
2023-04-01 20:45:37 +08:00
using System.Runtime.InteropServices;
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
{
public int EntitiesCount { get; }
public int Capacity { get; }
2023-03-02 14:42:44 +08:00
public IEcsWorld World { get; }
public Type DataType { get; }
2023-02-08 17:07:39 +08:00
public int ID { 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-03-02 14:42:44 +08:00
public interface IEcsPool<T> : IEcsPool
where T : struct
{
public ref readonly T Read(int entity);
public new ref T Write(int entity);
}
2023-03-13 04:32:24 +08:00
public class EcsNullPool : IEcsPool
{
private readonly IEcsWorld _source;
public EcsNullPool(IEcsWorld source)
{
_source = source;
}
public IEcsWorld World => _source;
public Type DataType => typeof(void);
2023-03-13 04:32:24 +08:00
public int ID => -1;
public int EntitiesCount => 0;
public int Capacity => 1;
2023-03-13 04:32:24 +08:00
public void Del(int index) { }
public bool Has(int index) => false;
public void Write(int index) { }
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-03-02 14:42:44 +08:00
public class EcsPool<T> : IEcsPool<T>
2023-02-09 02:26:43 +08:00
where T : struct
2023-02-05 19:59:45 +08:00
{
private readonly int _id;
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-01 20:45:37 +08:00
public int EntitiesCount => _itemsCount;
public int Capacity => _items.Length;
2023-03-02 14:42:44 +08:00
public IEcsWorld World => _source;
public Type DataType => typeof(T);
public int ID => _id;
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-03-13 04:32:24 +08:00
_id = 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
_componentResetHandler = ComponentResetHandler.New<T>();
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]);
_source.OnEntityComponentAdded(entityID, _id);
_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--;
_source.OnEntityComponentRemoved(entityID, _id);
_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
#region Equals/GetHashCode
public override bool Equals(object obj)
{
return base.Equals(obj);
}
public override int GetHashCode() => _source.GetHashCode() + ID;
2023-02-08 17:57:06 +08:00
#endregion
2023-03-30 16:43:22 +08:00
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-02-05 19:59:45 +08:00
}