2024-02-07 22:16:41 +08:00
|
|
|
using DCFApixels.DragonECS.Internal;
|
2023-02-08 17:57:06 +08:00
|
|
|
using System;
|
2023-04-22 23:40:09 +08:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2023-03-30 16:45:43 +08:00
|
|
|
using System.Runtime.CompilerServices;
|
2023-02-05 19:59:45 +08:00
|
|
|
|
|
|
|
namespace DCFApixels.DragonECS
|
|
|
|
{
|
2023-05-07 00:50:02 +08:00
|
|
|
/// <summary>Pool for IEcsComponent components</summary>
|
2023-06-25 23:13:51 +08:00
|
|
|
public sealed class EcsPool<T> : IEcsPoolImplementation<T>, IEcsStructPool<T>, IEnumerable<T> //IEnumerable<T> - IntelliSense hack
|
2023-04-21 14:21:24 +08:00
|
|
|
where T : struct, IEcsComponent
|
2023-02-05 19:59:45 +08:00
|
|
|
{
|
2023-05-07 00:50:02 +08:00
|
|
|
private EcsWorld _source;
|
2023-12-06 18:58:06 +08:00
|
|
|
private int _componentTypeID;
|
2024-01-07 18:52:54 +08:00
|
|
|
private EcsMaskChunck _maskBit;
|
2023-04-22 23:40:09 +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-06-25 23:13:51 +08:00
|
|
|
private IEcsComponentReset<T> _componentResetHandler = EcsComponentResetHandler<T>.instance;
|
2024-02-09 22:20:55 +08:00
|
|
|
private bool _isHasComponentResetHandler = EcsComponentResetHandler<T>.isHasHandler;
|
2023-06-25 23:13:51 +08:00
|
|
|
private IEcsComponentCopy<T> _componentCopyHandler = EcsComponentCopyHandler<T>.instance;
|
2024-02-09 22:20:55 +08:00
|
|
|
private bool _isHasComponentCopyHandler = EcsComponentCopyHandler<T>.isHasHandler;
|
2023-05-23 15:58:31 +08:00
|
|
|
|
2023-06-25 23:13:51 +08:00
|
|
|
private List<IEcsPoolEventListener> _listeners = new List<IEcsPoolEventListener>();
|
2023-04-17 22:58:52 +08:00
|
|
|
|
2023-12-06 18:58:06 +08:00
|
|
|
private EcsWorld.PoolsMediator _mediator;
|
|
|
|
|
2023-02-07 17:11:56 +08:00
|
|
|
#region Properites
|
2024-02-24 02:26:42 +08:00
|
|
|
public int Count
|
|
|
|
{
|
|
|
|
get { return _itemsCount; }
|
|
|
|
}
|
|
|
|
public int Capacity
|
|
|
|
{
|
|
|
|
get { return _items.Length; }
|
|
|
|
}
|
2024-02-25 00:55:30 +08:00
|
|
|
public int ComponentTypeID
|
2024-02-24 02:26:42 +08:00
|
|
|
{
|
|
|
|
get { return _componentTypeID; }
|
|
|
|
}
|
|
|
|
public Type ComponentType
|
|
|
|
{
|
|
|
|
get { return typeof(T); }
|
|
|
|
}
|
|
|
|
public EcsWorld World
|
|
|
|
{
|
|
|
|
get { return _source; }
|
|
|
|
}
|
2024-02-25 17:27:08 +08:00
|
|
|
public bool IsReadOnly
|
2024-02-25 00:55:30 +08:00
|
|
|
{
|
|
|
|
get { return false; }
|
|
|
|
}
|
2023-02-07 17:11:56 +08:00
|
|
|
#endregion
|
|
|
|
|
2023-05-07 00:50:02 +08:00
|
|
|
#region Methods
|
2023-04-21 03:16:05 +08:00
|
|
|
public ref T Add(int entityID)
|
2023-02-13 21:11:54 +08:00
|
|
|
{
|
2023-04-20 18:23:23 +08:00
|
|
|
ref int itemIndex = ref _mapping[entityID];
|
2023-06-02 01:20:46 +08:00
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
2024-02-24 02:26:42 +08:00
|
|
|
if (itemIndex > 0) { EcsPoolThrowHalper.ThrowAlreadyHasComponent<T>(entityID); }
|
2023-05-07 00:50:02 +08:00
|
|
|
#endif
|
|
|
|
if (_recycledItemsCount > 0)
|
2023-04-20 18:23:23 +08:00
|
|
|
{
|
2023-05-07 00:50:02 +08:00
|
|
|
itemIndex = _recycledItems[--_recycledItemsCount];
|
|
|
|
_itemsCount++;
|
2023-04-17 22:58:52 +08:00
|
|
|
}
|
2023-05-07 00:50:02 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
itemIndex = ++_itemsCount;
|
|
|
|
if (itemIndex >= _items.Length)
|
2024-02-24 02:26:42 +08:00
|
|
|
{
|
2023-05-07 00:50:02 +08:00
|
|
|
Array.Resize(ref _items, _items.Length << 1);
|
2024-02-24 02:26:42 +08:00
|
|
|
}
|
2023-05-07 00:50:02 +08:00
|
|
|
}
|
2023-12-06 18:58:06 +08:00
|
|
|
_mediator.RegisterComponent(entityID, _componentTypeID, _maskBit);
|
2023-05-30 16:01:16 +08:00
|
|
|
_listeners.InvokeOnAddAndGet(entityID);
|
2024-02-09 22:20:55 +08:00
|
|
|
ResetComponent(ref _items[itemIndex]);
|
2023-04-20 18:23:23 +08:00
|
|
|
return ref _items[itemIndex];
|
2023-04-09 02:52:39 +08:00
|
|
|
}
|
2023-04-17 22:58:52 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-05-30 16:01:16 +08:00
|
|
|
public ref T Get(int entityID)
|
2023-04-09 02:52:39 +08:00
|
|
|
{
|
2023-06-02 01:20:46 +08:00
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
2024-02-24 02:26:42 +08:00
|
|
|
if (!Has(entityID)) { EcsPoolThrowHalper.ThrowNotHaveComponent<T>(entityID); }
|
2023-05-07 00:50:02 +08:00
|
|
|
#endif
|
2023-05-30 16:01:16 +08:00
|
|
|
_listeners.InvokeOnGet(entityID);
|
2023-04-20 18:23:23 +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-21 03:16:05 +08:00
|
|
|
public ref readonly T Read(int entityID)
|
2023-02-05 19:59:45 +08:00
|
|
|
{
|
2023-06-02 01:20:46 +08:00
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
2024-02-24 02:26:42 +08:00
|
|
|
if (!Has(entityID)) { EcsPoolThrowHalper.ThrowNotHaveComponent<T>(entityID); }
|
2023-05-07 00:50:02 +08:00
|
|
|
#endif
|
2023-04-01 21:16:08 +08:00
|
|
|
return ref _items[_mapping[entityID]];
|
2023-02-05 19:59:45 +08:00
|
|
|
}
|
2023-05-30 16:01:16 +08:00
|
|
|
public ref T TryAddOrGet(int entityID)
|
2023-05-07 00:50:02 +08:00
|
|
|
{
|
|
|
|
ref int itemIndex = ref _mapping[entityID];
|
|
|
|
if (itemIndex <= 0)
|
|
|
|
{
|
|
|
|
if (_recycledItemsCount > 0)
|
|
|
|
{
|
|
|
|
itemIndex = _recycledItems[--_recycledItemsCount];
|
|
|
|
_itemsCount++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
itemIndex = ++_itemsCount;
|
|
|
|
if (itemIndex >= _items.Length)
|
2024-02-24 02:26:42 +08:00
|
|
|
{
|
2023-05-07 00:50:02 +08:00
|
|
|
Array.Resize(ref _items, _items.Length << 1);
|
2024-02-24 02:26:42 +08:00
|
|
|
}
|
2023-05-07 00:50:02 +08:00
|
|
|
}
|
2023-12-06 18:58:06 +08:00
|
|
|
_mediator.RegisterComponent(entityID, _componentTypeID, _maskBit);
|
2023-05-23 15:58:31 +08:00
|
|
|
_listeners.InvokeOnAdd(entityID);
|
2023-05-07 00:50:02 +08:00
|
|
|
}
|
2023-05-30 16:01:16 +08:00
|
|
|
_listeners.InvokeOnGet(entityID);
|
2024-02-09 22:20:55 +08:00
|
|
|
ResetComponent(ref _items[itemIndex]);
|
2023-05-07 00:50:02 +08:00
|
|
|
return ref _items[itemIndex];
|
|
|
|
}
|
2023-02-07 17:11:56 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-05-07 00:50:02 +08:00
|
|
|
public bool Has(int entityID)
|
2023-04-01 20:45:37 +08:00
|
|
|
{
|
2023-04-01 21:16:08 +08:00
|
|
|
return _mapping[entityID] > 0;
|
2023-04-01 20:45:37 +08:00
|
|
|
}
|
2023-04-21 03:16:05 +08:00
|
|
|
public void Del(int entityID)
|
2023-02-07 17:11:56 +08:00
|
|
|
{
|
2024-02-24 02:26:42 +08:00
|
|
|
ref int itemIndex = ref _mapping[entityID];
|
2023-06-02 01:20:46 +08:00
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
2024-02-24 02:26:42 +08:00
|
|
|
if (itemIndex <= 0) { EcsPoolThrowHalper.ThrowNotHaveComponent<T>(entityID); }
|
2023-05-07 00:50:02 +08:00
|
|
|
#endif
|
2024-02-09 22:20:55 +08:00
|
|
|
ResetComponent(ref _items[itemIndex]);
|
2023-04-01 21:16:08 +08:00
|
|
|
if (_recycledItemsCount >= _recycledItems.Length)
|
2024-02-09 22:20:55 +08:00
|
|
|
{
|
2023-04-01 20:45:37 +08:00
|
|
|
Array.Resize(ref _recycledItems, _recycledItems.Length << 1);
|
2024-02-09 22:20:55 +08:00
|
|
|
}
|
2023-04-20 11:23:54 +08:00
|
|
|
_recycledItems[_recycledItemsCount++] = itemIndex;
|
2024-02-24 02:26:42 +08:00
|
|
|
itemIndex = 0;
|
2023-04-01 20:45:37 +08:00
|
|
|
_itemsCount--;
|
2023-12-06 18:58:06 +08:00
|
|
|
_mediator.UnregisterComponent(entityID, _componentTypeID, _maskBit);
|
2023-05-23 15:58:31 +08:00
|
|
|
_listeners.InvokeOnDel(entityID);
|
2023-05-07 00:50:02 +08:00
|
|
|
}
|
|
|
|
public void TryDel(int entityID)
|
|
|
|
{
|
2024-02-24 02:26:42 +08:00
|
|
|
if (Has(entityID))
|
|
|
|
{
|
|
|
|
Del(entityID);
|
|
|
|
}
|
2023-05-07 00:50:02 +08:00
|
|
|
}
|
|
|
|
public void Copy(int fromEntityID, int toEntityID)
|
|
|
|
{
|
2023-06-02 01:20:46 +08:00
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
2024-02-24 02:26:42 +08:00
|
|
|
if (!Has(fromEntityID)) { EcsPoolThrowHalper.ThrowNotHaveComponent<T>(fromEntityID); }
|
2023-05-07 00:50:02 +08:00
|
|
|
#endif
|
2024-02-09 22:20:55 +08:00
|
|
|
CopyComponent(ref Get(fromEntityID), ref TryAddOrGet(toEntityID));
|
2023-05-23 01:47:28 +08:00
|
|
|
}
|
|
|
|
public void Copy(int fromEntityID, EcsWorld toWorld, int toEntityID)
|
|
|
|
{
|
2023-06-02 01:20:46 +08:00
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
2024-02-24 02:26:42 +08:00
|
|
|
if (!Has(fromEntityID)) { EcsPoolThrowHalper.ThrowNotHaveComponent<T>(fromEntityID); }
|
2023-05-23 01:47:28 +08:00
|
|
|
#endif
|
2024-02-09 22:20:55 +08:00
|
|
|
CopyComponent(ref Get(fromEntityID), ref toWorld.GetPool<T>().TryAddOrGet(toEntityID));
|
2023-04-20 11:23:54 +08:00
|
|
|
}
|
2023-02-07 17:11:56 +08:00
|
|
|
#endregion
|
|
|
|
|
2023-05-07 00:50:02 +08:00
|
|
|
#region Callbacks
|
2023-12-06 18:58:06 +08:00
|
|
|
void IEcsPoolImplementation.OnInit(EcsWorld world, EcsWorld.PoolsMediator mediator, int componentTypeID)
|
2023-11-08 15:15:10 +08:00
|
|
|
{
|
|
|
|
_source = world;
|
2023-12-06 18:58:06 +08:00
|
|
|
_mediator = mediator;
|
|
|
|
_componentTypeID = componentTypeID;
|
2024-01-07 18:52:54 +08:00
|
|
|
_maskBit = EcsMaskChunck.FromID(componentTypeID);
|
2023-11-22 17:35:03 +08:00
|
|
|
|
2023-11-08 15:15:10 +08:00
|
|
|
_mapping = new int[world.Capacity];
|
2024-02-03 01:12:53 +08:00
|
|
|
_recycledItems = new int[world.Config.Get_PoolRecycledComponentsCapacity()];
|
2023-11-08 15:15:10 +08:00
|
|
|
_recycledItemsCount = 0;
|
2024-02-07 22:16:41 +08:00
|
|
|
_items = new T[ArrayUtility.NormalizeSizeToPowerOfTwo(world.Config.Get_PoolComponentsCapacity())];
|
2023-11-08 15:15:10 +08:00
|
|
|
_itemsCount = 0;
|
|
|
|
}
|
2023-05-07 00:50:02 +08:00
|
|
|
void IEcsPoolImplementation.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-05-07 00:50:02 +08:00
|
|
|
void IEcsPoolImplementation.OnWorldDestroy() { }
|
|
|
|
void IEcsPoolImplementation.OnReleaseDelEntityBuffer(ReadOnlySpan<int> buffer)
|
|
|
|
{
|
2024-02-25 23:05:11 +08:00
|
|
|
if (_itemsCount <= 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2023-05-07 00:50:02 +08:00
|
|
|
foreach (var entityID in buffer)
|
2024-02-07 22:16:41 +08:00
|
|
|
{
|
2023-05-07 00:50:02 +08:00
|
|
|
TryDel(entityID);
|
2024-02-07 22:16:41 +08:00
|
|
|
}
|
2023-05-07 00:50:02 +08:00
|
|
|
}
|
|
|
|
#endregion
|
2023-04-22 23:40:09 +08:00
|
|
|
|
2023-05-07 00:50:02 +08:00
|
|
|
#region Other
|
2024-02-24 02:26:42 +08:00
|
|
|
void IEcsPool.AddRaw(int entityID, object dataRaw) { Add(entityID) = (T)dataRaw; }
|
2024-02-25 00:55:30 +08:00
|
|
|
object IEcsReadonlyPool.GetRaw(int entityID) { return Get(entityID); }
|
2024-02-24 02:26:42 +08:00
|
|
|
void IEcsPool.SetRaw(int entityID, object dataRaw) { Get(entityID) = (T)dataRaw; }
|
2023-04-06 23:40:47 +08:00
|
|
|
#endregion
|
2023-04-21 03:16:05 +08:00
|
|
|
|
2023-05-23 15:58:31 +08:00
|
|
|
#region Listeners
|
|
|
|
public void AddListener(IEcsPoolEventListener listener)
|
|
|
|
{
|
2023-05-30 18:30:10 +08:00
|
|
|
if (listener == null) { throw new ArgumentNullException("listener is null"); }
|
2023-05-23 15:58:31 +08:00
|
|
|
_listeners.Add(listener);
|
|
|
|
}
|
|
|
|
public void RemoveListener(IEcsPoolEventListener listener)
|
|
|
|
{
|
2023-05-30 18:30:10 +08:00
|
|
|
if (listener == null) { throw new ArgumentNullException("listener is null"); }
|
2023-05-23 15:58:31 +08:00
|
|
|
_listeners.Remove(listener);
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
2024-02-09 22:20:55 +08:00
|
|
|
#region Reset/Copy
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
private void ResetComponent(ref T component)
|
|
|
|
{
|
|
|
|
if (_isHasComponentResetHandler)
|
|
|
|
{
|
|
|
|
_componentResetHandler.Reset(ref component);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
component = default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
private void CopyComponent(ref T from, ref T to)
|
|
|
|
{
|
|
|
|
if (_isHasComponentCopyHandler)
|
|
|
|
{
|
|
|
|
_componentCopyHandler.Copy(ref from, ref to);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
to = from;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
2023-05-07 00:50:02 +08:00
|
|
|
#region IEnumerator - IntelliSense hack
|
2024-02-24 02:26:42 +08:00
|
|
|
IEnumerator<T> IEnumerable<T>.GetEnumerator() { throw new NotImplementedException(); }
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); }
|
2023-05-07 00:50:02 +08:00
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
/// <summary>Standard component</summary>
|
2023-04-21 03:46:51 +08:00
|
|
|
public interface IEcsComponent { }
|
|
|
|
public static class EcsPoolExt
|
2023-04-21 03:16:05 +08:00
|
|
|
{
|
2023-07-03 02:50:16 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-04-21 16:03:50 +08:00
|
|
|
public static EcsPool<TComponent> GetPool<TComponent>(this EcsWorld self) where TComponent : struct, IEcsComponent
|
2023-04-21 03:16:05 +08:00
|
|
|
{
|
2024-03-07 03:30:18 +08:00
|
|
|
return self.GetPoolInstance<EcsPool<TComponent>>();
|
2023-04-21 16:03:50 +08:00
|
|
|
}
|
2023-07-03 02:50:16 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-11-21 10:41:41 +08:00
|
|
|
public static EcsPool<TComponent> GetPoolUnchecked<TComponent>(this EcsWorld self) where TComponent : struct, IEcsComponent
|
2023-07-03 02:50:16 +08:00
|
|
|
{
|
2024-03-07 03:30:18 +08:00
|
|
|
return self.GetPoolInstanceUnchecked<EcsPool<TComponent>>();
|
2023-07-03 02:50:16 +08:00
|
|
|
}
|
2023-04-21 16:03:50 +08:00
|
|
|
|
2023-07-03 02:50:16 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-02-11 01:28:18 +08:00
|
|
|
public static EcsPool<TComponent> Include<TComponent>(this EcsAspect.Builder self) where TComponent : struct, IEcsComponent
|
2023-04-21 16:03:50 +08:00
|
|
|
{
|
2024-03-07 03:40:06 +08:00
|
|
|
return self.IncludePool<EcsPool<TComponent>>();
|
2023-04-21 16:03:50 +08:00
|
|
|
}
|
2023-07-03 02:50:16 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-02-11 01:28:18 +08:00
|
|
|
public static EcsPool<TComponent> Exclude<TComponent>(this EcsAspect.Builder self) where TComponent : struct, IEcsComponent
|
2023-04-21 16:03:50 +08:00
|
|
|
{
|
2024-03-07 03:40:06 +08:00
|
|
|
return self.ExcludePool<EcsPool<TComponent>>();
|
2023-04-21 16:03:50 +08:00
|
|
|
}
|
2023-07-03 02:50:16 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-02-11 01:28:18 +08:00
|
|
|
public static EcsPool<TComponent> Optional<TComponent>(this EcsAspect.Builder self) where TComponent : struct, IEcsComponent
|
2023-04-21 16:03:50 +08:00
|
|
|
{
|
2024-03-07 03:40:06 +08:00
|
|
|
return self.OptionalPool<EcsPool<TComponent>>();
|
2023-04-21 16:03:50 +08:00
|
|
|
}
|
2023-04-21 03:16:05 +08:00
|
|
|
}
|
2023-02-05 19:59:45 +08:00
|
|
|
}
|