2023-10-31 03:03:13 +08:00
|
|
|
using DCFApixels.DragonECS.Utils;
|
2023-06-29 23:53:26 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
|
|
|
namespace DCFApixels.DragonECS
|
|
|
|
{
|
2023-10-31 03:03:13 +08:00
|
|
|
/// <summary>Pool for IEcsHybridComponent components</summary>
|
2023-06-29 23:53:26 +08:00
|
|
|
public sealed class EcsHybridPool<T> : IEcsPoolImplementation<T>, IEcsHybridPool<T>, IEnumerable<T> //IEnumerable<T> - IntelliSense hack
|
|
|
|
where T : IEcsHybridComponent
|
|
|
|
{
|
|
|
|
private EcsWorld _source;
|
|
|
|
private int _componentID;
|
|
|
|
|
|
|
|
private int[] _mapping;// index = entityID / value = itemIndex;/ value = 0 = no entityID
|
|
|
|
private T[] _items; //dense
|
2023-07-17 15:55:23 +08:00
|
|
|
private int[] _entities;
|
2023-06-29 23:53:26 +08:00
|
|
|
private int _itemsCount;
|
2023-07-17 15:55:23 +08:00
|
|
|
|
2023-06-29 23:53:26 +08:00
|
|
|
private int[] _recycledItems;
|
|
|
|
private int _recycledItemsCount;
|
|
|
|
|
|
|
|
private List<IEcsPoolEventListener> _listeners = new List<IEcsPoolEventListener>();
|
|
|
|
|
|
|
|
#region Properites
|
|
|
|
public int Count => _itemsCount;
|
|
|
|
public int Capacity => _items.Length;
|
|
|
|
public int ComponentID => _componentID;
|
|
|
|
public Type ComponentType => typeof(T);
|
|
|
|
public EcsWorld World => _source;
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Init
|
|
|
|
void IEcsPoolImplementation.OnInit(EcsWorld world, int componentID)
|
|
|
|
{
|
|
|
|
_source = world;
|
|
|
|
_componentID = componentID;
|
|
|
|
|
|
|
|
const int capacity = 512;
|
|
|
|
|
|
|
|
_mapping = new int[world.Capacity];
|
|
|
|
_recycledItems = new int[128];
|
|
|
|
_recycledItemsCount = 0;
|
|
|
|
_items = new T[capacity];
|
2023-07-17 15:55:23 +08:00
|
|
|
_entities = new int[capacity];
|
2023-06-29 23:53:26 +08:00
|
|
|
_itemsCount = 0;
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Methods
|
|
|
|
public void Add(int entityID, T component)
|
|
|
|
{
|
|
|
|
ref int itemIndex = ref _mapping[entityID];
|
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
|
|
|
if (itemIndex > 0) EcsPoolThrowHalper.ThrowAlreadyHasComponent<T>(entityID);
|
|
|
|
#endif
|
|
|
|
if (_recycledItemsCount > 0)
|
|
|
|
{
|
|
|
|
itemIndex = _recycledItems[--_recycledItemsCount];
|
|
|
|
_itemsCount++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
itemIndex = ++_itemsCount;
|
|
|
|
if (itemIndex >= _items.Length)
|
2023-07-17 15:55:23 +08:00
|
|
|
{
|
2023-06-29 23:53:26 +08:00
|
|
|
Array.Resize(ref _items, _items.Length << 1);
|
2023-07-17 15:55:23 +08:00
|
|
|
Array.Resize(ref _entities, _items.Length);
|
|
|
|
}
|
2023-06-29 23:53:26 +08:00
|
|
|
}
|
|
|
|
this.IncrementEntityComponentCount(entityID);
|
|
|
|
_listeners.InvokeOnAdd(entityID);
|
2023-06-30 01:01:19 +08:00
|
|
|
component.OnAddToPool(_source.GetEntityLong(entityID));
|
2023-06-29 23:53:26 +08:00
|
|
|
_items[itemIndex] = component;
|
2023-07-17 15:55:23 +08:00
|
|
|
_entities[itemIndex] = entityID;
|
2023-06-29 23:53:26 +08:00
|
|
|
}
|
|
|
|
public void Set(int entityID, T component)
|
|
|
|
{
|
|
|
|
ref int itemIndex = ref _mapping[entityID];
|
|
|
|
if(itemIndex <= 0)
|
|
|
|
{//null
|
|
|
|
if (_recycledItemsCount > 0)
|
|
|
|
{
|
|
|
|
itemIndex = _recycledItems[--_recycledItemsCount];
|
|
|
|
_itemsCount++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
itemIndex = ++_itemsCount;
|
|
|
|
if (itemIndex >= _items.Length)
|
2023-07-17 15:55:23 +08:00
|
|
|
{
|
2023-06-29 23:53:26 +08:00
|
|
|
Array.Resize(ref _items, _items.Length << 1);
|
2023-07-17 15:55:23 +08:00
|
|
|
Array.Resize(ref _entities, _items.Length);
|
|
|
|
}
|
2023-06-29 23:53:26 +08:00
|
|
|
}
|
|
|
|
this.IncrementEntityComponentCount(entityID);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{//not null
|
|
|
|
_listeners.InvokeOnDel(entityID);
|
2023-06-30 01:01:19 +08:00
|
|
|
_items[itemIndex].OnDelFromPool(_source.GetEntityLong(entityID));
|
2023-06-29 23:53:26 +08:00
|
|
|
}
|
|
|
|
_listeners.InvokeOnAdd(entityID);
|
2023-06-30 01:01:19 +08:00
|
|
|
component.OnAddToPool(_source.GetEntityLong(entityID));
|
2023-06-29 23:53:26 +08:00
|
|
|
_items[itemIndex] = component;
|
2023-07-17 15:55:23 +08:00
|
|
|
_entities[itemIndex] = entityID;
|
2023-06-29 23:53:26 +08:00
|
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public T Get(int entityID)
|
|
|
|
{
|
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
|
|
|
if (!Has(entityID)) EcsPoolThrowHalper.ThrowNotHaveComponent<T>(entityID);
|
|
|
|
#endif
|
|
|
|
_listeners.InvokeOnGet(entityID);
|
|
|
|
return _items[_mapping[entityID]];
|
|
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public ref readonly T Read(int entityID)
|
|
|
|
{
|
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
|
|
|
if (!Has(entityID)) EcsPoolThrowHalper.ThrowNotHaveComponent<T>(entityID);
|
|
|
|
#endif
|
|
|
|
return ref _items[_mapping[entityID]];
|
|
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public bool Has(int entityID)
|
|
|
|
{
|
|
|
|
return _mapping[entityID] > 0;
|
|
|
|
}
|
|
|
|
public void Del(int entityID)
|
|
|
|
{
|
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
|
|
|
if (!Has(entityID)) EcsPoolThrowHalper.ThrowNotHaveComponent<T>(entityID);
|
|
|
|
#endif
|
|
|
|
ref int itemIndex = ref _mapping[entityID];
|
|
|
|
T component = _items[itemIndex];
|
2023-06-30 01:01:19 +08:00
|
|
|
component.OnDelFromPool(_source.GetEntityLong(entityID));
|
2023-06-29 23:53:26 +08:00
|
|
|
if (_recycledItemsCount >= _recycledItems.Length)
|
|
|
|
Array.Resize(ref _recycledItems, _recycledItems.Length << 1);
|
|
|
|
_recycledItems[_recycledItemsCount++] = itemIndex;
|
|
|
|
_mapping[entityID] = 0;
|
2023-07-17 15:55:23 +08:00
|
|
|
_entities[itemIndex] = 0;
|
2023-06-29 23:53:26 +08:00
|
|
|
_itemsCount--;
|
|
|
|
this.DecrementEntityComponentCount(entityID);
|
|
|
|
_listeners.InvokeOnDel(entityID);
|
|
|
|
}
|
|
|
|
public void TryDel(int entityID)
|
|
|
|
{
|
|
|
|
if (Has(entityID)) Del(entityID);
|
|
|
|
}
|
|
|
|
public void Copy(int fromEntityID, int toEntityID)
|
|
|
|
{
|
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
|
|
|
if (!Has(fromEntityID)) EcsPoolThrowHalper.ThrowNotHaveComponent<T>(fromEntityID);
|
|
|
|
#endif
|
|
|
|
Set(toEntityID, Get(fromEntityID));
|
|
|
|
}
|
|
|
|
public void Copy(int fromEntityID, EcsWorld toWorld, int toEntityID)
|
|
|
|
{
|
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
|
|
|
if (!Has(fromEntityID)) EcsPoolThrowHalper.ThrowNotHaveComponent<T>(fromEntityID);
|
|
|
|
#endif
|
|
|
|
toWorld.GetPool<T>().Set(toEntityID, Get(fromEntityID));
|
|
|
|
}
|
2023-07-17 15:55:23 +08:00
|
|
|
|
|
|
|
public void ClearNotAliveComponents()
|
|
|
|
{
|
|
|
|
for (int i = _itemsCount - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if (!_items[i].IsAlive)
|
|
|
|
Del(_entities[i]);
|
|
|
|
}
|
|
|
|
}
|
2023-06-29 23:53:26 +08:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Callbacks
|
|
|
|
void IEcsPoolImplementation.OnWorldResize(int newSize)
|
|
|
|
{
|
|
|
|
Array.Resize(ref _mapping, newSize);
|
|
|
|
}
|
|
|
|
void IEcsPoolImplementation.OnWorldDestroy() { }
|
|
|
|
void IEcsPoolImplementation.OnReleaseDelEntityBuffer(ReadOnlySpan<int> buffer)
|
|
|
|
{
|
|
|
|
foreach (var entityID in buffer)
|
|
|
|
TryDel(entityID);
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Other
|
|
|
|
void IEcsPool.AddRaw(int entityID, object dataRaw) => Add(entityID, (T)dataRaw);
|
|
|
|
object IEcsPool.GetRaw(int entityID) => Read(entityID);
|
|
|
|
void IEcsPool.SetRaw(int entityID, object dataRaw) => Set(entityID, (T)dataRaw);
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Listeners
|
|
|
|
public void AddListener(IEcsPoolEventListener listener)
|
|
|
|
{
|
|
|
|
if (listener == null) { throw new ArgumentNullException("listener is null"); }
|
|
|
|
_listeners.Add(listener);
|
|
|
|
}
|
|
|
|
public void RemoveListener(IEcsPoolEventListener listener)
|
|
|
|
{
|
|
|
|
if (listener == null) { throw new ArgumentNullException("listener is null"); }
|
|
|
|
_listeners.Remove(listener);
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region IEnumerator - IntelliSense hack
|
|
|
|
IEnumerator<T> IEnumerable<T>.GetEnumerator() => throw new NotImplementedException();
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException();
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
/// <summary>Hybrid component</summary>
|
|
|
|
public interface IEcsHybridComponent
|
|
|
|
{
|
|
|
|
bool IsAlive { get; }
|
2023-06-30 01:01:19 +08:00
|
|
|
void OnAddToPool(entlong entity);
|
|
|
|
void OnDelFromPool(entlong entity);
|
2023-06-29 23:53:26 +08:00
|
|
|
}
|
2023-10-31 03:03:13 +08:00
|
|
|
public static class IEcsHybridComponentExtensions
|
|
|
|
{
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public static bool IsNullOrNotAlive(this IEcsHybridComponent self) => self == null || self.IsAlive;
|
|
|
|
}
|
2023-06-29 23:53:26 +08:00
|
|
|
public static class EcsHybridPoolExt
|
|
|
|
{
|
2023-07-03 02:50:16 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-06-29 23:53:26 +08:00
|
|
|
public static EcsHybridPool<T> GetPool<T>(this EcsWorld self) where T : IEcsHybridComponent
|
|
|
|
{
|
|
|
|
return self.GetPool<EcsHybridPool<T>>();
|
|
|
|
}
|
2023-07-03 02:50:16 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public static EcsHybridPool<T> UncheckedGetPool<T>(this EcsWorld self) where T : IEcsHybridComponent
|
|
|
|
{
|
|
|
|
return self.UncheckedGetPool<EcsHybridPool<T>>();
|
|
|
|
}
|
2023-06-29 23:53:26 +08:00
|
|
|
|
2023-07-03 02:50:16 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-06-29 23:53:26 +08:00
|
|
|
public static EcsHybridPool<T> Include<T>(this EcsAspectBuilderBase self) where T : IEcsHybridComponent
|
|
|
|
{
|
|
|
|
return self.Include<EcsHybridPool<T>>();
|
|
|
|
}
|
2023-07-03 02:50:16 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-06-29 23:53:26 +08:00
|
|
|
public static EcsHybridPool<T> Exclude<T>(this EcsAspectBuilderBase self) where T : IEcsHybridComponent
|
|
|
|
{
|
|
|
|
return self.Exclude<EcsHybridPool<T>>();
|
|
|
|
}
|
2023-07-03 02:50:16 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-06-29 23:53:26 +08:00
|
|
|
public static EcsHybridPool<T> Optional<T>(this EcsAspectBuilderBase self) where T : IEcsHybridComponent
|
|
|
|
{
|
|
|
|
return self.Optional<EcsHybridPool<T>>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|