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-05-30 04:32:09 +08:00
|
|
|
using static DCFApixels.DragonECS.EcsPoolThrowHalper;
|
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-05-26 06:37:15 +08:00
|
|
|
public sealed class EcsPool<T> : IEcsPoolImplementation<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;
|
|
|
|
private int _id;
|
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-03-30 16:43:22 +08:00
|
|
|
private IEcsComponentReset<T> _componentResetHandler;
|
2023-05-07 00:50:02 +08:00
|
|
|
private IEcsComponentCopy<T> _componentCopyHandler;
|
2023-05-23 15:58:31 +08:00
|
|
|
|
|
|
|
private List<IEcsPoolEventListener> _listeners;
|
2023-04-17 22:58:52 +08:00
|
|
|
|
2023-02-07 17:11:56 +08:00
|
|
|
#region Properites
|
2023-04-21 03:16:05 +08:00
|
|
|
public int Count => _itemsCount;
|
|
|
|
public int Capacity => _items.Length;
|
2023-05-07 00:50:02 +08:00
|
|
|
public int ComponentID => _id;
|
|
|
|
public Type ComponentType => typeof(T);
|
|
|
|
public EcsWorld World => _source;
|
2023-02-07 17:11:56 +08:00
|
|
|
#endregion
|
|
|
|
|
2023-04-21 03:16:05 +08:00
|
|
|
#region Init
|
2023-05-07 00:50:02 +08:00
|
|
|
void IEcsPoolImplementation.OnInit(EcsWorld world, int componentID)
|
2023-02-06 01:27:32 +08:00
|
|
|
{
|
2023-05-07 00:50:02 +08:00
|
|
|
_source = world;
|
|
|
|
_id = componentID;
|
|
|
|
|
2023-04-21 03:16:05 +08:00
|
|
|
const int capacity = 512;
|
2023-02-09 02:26:43 +08:00
|
|
|
|
2023-04-21 03:16:05 +08:00
|
|
|
_mapping = new int[world.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-05-23 15:58:31 +08:00
|
|
|
_listeners = new List<IEcsPoolEventListener>();
|
|
|
|
|
2023-04-20 18:23:23 +08:00
|
|
|
_componentResetHandler = EcsComponentResetHandler<T>.instance;
|
2023-05-07 00:50:02 +08:00
|
|
|
_componentCopyHandler = EcsComponentCopyHandler<T>.instance;
|
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-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
|
|
|
// using (_addMark.Auto())
|
|
|
|
// {
|
|
|
|
ref int itemIndex = ref _mapping[entityID];
|
2023-05-26 05:13:11 +08:00
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
2023-05-07 00:50:02 +08:00
|
|
|
if (itemIndex > 0) ThrowAlreadyHasComponent<T>(entityID);
|
|
|
|
#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)
|
|
|
|
Array.Resize(ref _items, _items.Length << 1);
|
|
|
|
}
|
|
|
|
this.IncrementEntityComponentCount(entityID);
|
2023-05-23 15:58:31 +08:00
|
|
|
_listeners.InvokeOnAddAndWrite(entityID);
|
2023-04-20 18:23:23 +08:00
|
|
|
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-21 03:16:05 +08:00
|
|
|
public ref T Write(int entityID)
|
2023-04-09 02:52:39 +08:00
|
|
|
{
|
2023-04-20 18:23:23 +08:00
|
|
|
// using (_writeMark.Auto())
|
2023-05-26 05:13:11 +08:00
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
2023-05-07 00:50:02 +08:00
|
|
|
if (!Has(entityID)) ThrowNotHaveComponent<T>(entityID);
|
|
|
|
#endif
|
2023-05-23 15:58:31 +08:00
|
|
|
_listeners.InvokeOnWrite(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-04-20 18:23:23 +08:00
|
|
|
// using (_readMark.Auto())
|
2023-05-26 05:13:11 +08:00
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
2023-05-07 00:50:02 +08:00
|
|
|
if (!Has(entityID)) ThrowNotHaveComponent<T>(entityID);
|
|
|
|
#endif
|
2023-04-01 21:16:08 +08:00
|
|
|
return ref _items[_mapping[entityID]];
|
2023-02-05 19:59:45 +08:00
|
|
|
}
|
2023-05-07 00:50:02 +08:00
|
|
|
public ref T TryAddOrWrite(int entityID)
|
|
|
|
{
|
|
|
|
ref int itemIndex = ref _mapping[entityID];
|
|
|
|
if (itemIndex <= 0)
|
|
|
|
{
|
|
|
|
if (_recycledItemsCount > 0)
|
|
|
|
{
|
|
|
|
itemIndex = _recycledItems[--_recycledItemsCount];
|
|
|
|
_itemsCount++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
itemIndex = ++_itemsCount;
|
|
|
|
if (itemIndex >= _items.Length)
|
|
|
|
Array.Resize(ref _items, _items.Length << 1);
|
|
|
|
}
|
|
|
|
this.IncrementEntityComponentCount(entityID);
|
2023-05-23 15:58:31 +08:00
|
|
|
_listeners.InvokeOnAdd(entityID);
|
2023-05-07 00:50:02 +08:00
|
|
|
}
|
2023-05-23 15:58:31 +08:00
|
|
|
_listeners.InvokeOnWrite(entityID);
|
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
|
|
|
{
|
2023-05-26 05:13:11 +08:00
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
2023-05-07 00:50:02 +08:00
|
|
|
if (!Has(entityID)) ThrowNotHaveComponent<T>(entityID);
|
|
|
|
#endif
|
2023-04-20 11:23:54 +08:00
|
|
|
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;
|
2023-05-07 00:50:02 +08:00
|
|
|
_mapping[entityID] = 0;
|
2023-04-01 20:45:37 +08:00
|
|
|
_itemsCount--;
|
2023-05-07 00:50:02 +08:00
|
|
|
this.DecrementEntityComponentCount(entityID);
|
2023-05-23 15:58:31 +08:00
|
|
|
_listeners.InvokeOnDel(entityID);
|
2023-05-07 00:50:02 +08:00
|
|
|
}
|
|
|
|
public void TryDel(int entityID)
|
|
|
|
{
|
|
|
|
if (Has(entityID)) Del(entityID);
|
|
|
|
}
|
|
|
|
public void Copy(int fromEntityID, int toEntityID)
|
|
|
|
{
|
2023-05-26 05:13:11 +08:00
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
2023-05-07 00:50:02 +08:00
|
|
|
if (!Has(fromEntityID)) ThrowNotHaveComponent<T>(fromEntityID);
|
|
|
|
#endif
|
2023-05-23 01:47:28 +08:00
|
|
|
_componentCopyHandler.Copy(ref Write(fromEntityID), ref TryAddOrWrite(toEntityID));
|
|
|
|
}
|
|
|
|
public void Copy(int fromEntityID, EcsWorld toWorld, int toEntityID)
|
|
|
|
{
|
2023-05-26 05:13:11 +08:00
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
|
2023-05-23 01:47:28 +08:00
|
|
|
if (!Has(fromEntityID)) ThrowNotHaveComponent<T>(fromEntityID);
|
|
|
|
#endif
|
|
|
|
_componentCopyHandler.Copy(ref Write(fromEntityID), ref toWorld.GetPool<T>().TryAddOrWrite(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
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
foreach (var entityID in buffer)
|
|
|
|
TryDel(entityID);
|
|
|
|
}
|
|
|
|
#endregion
|
2023-04-22 23:40:09 +08:00
|
|
|
|
2023-05-07 00:50:02 +08:00
|
|
|
#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) => Write(entityID) = (T)dataRaw;
|
|
|
|
ref readonly T IEcsPool<T>.Read(int entityID) => ref Read(entityID);
|
|
|
|
ref T IEcsPool<T>.Write(int entityID) => ref Write(entityID);
|
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)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
2023-05-07 00:50:02 +08:00
|
|
|
#region IEnumerator - IntelliSense hack
|
|
|
|
IEnumerator<T> IEnumerable<T>.GetEnumerator() => throw new NotImplementedException();
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException();
|
|
|
|
#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-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
|
|
|
{
|
|
|
|
return self.GetPool<TComponent, EcsPool<TComponent>>();
|
2023-04-21 16:03:50 +08:00
|
|
|
}
|
|
|
|
|
2023-05-07 00:50:02 +08:00
|
|
|
public static EcsPool<TComponent> Include<TComponent>(this EcsSubjectBuilderBase self) where TComponent : struct, IEcsComponent
|
2023-04-21 16:03:50 +08:00
|
|
|
{
|
|
|
|
return self.Include<TComponent, EcsPool<TComponent>>();
|
|
|
|
}
|
2023-05-07 00:50:02 +08:00
|
|
|
public static EcsPool<TComponent> Exclude<TComponent>(this EcsSubjectBuilderBase self) where TComponent : struct, IEcsComponent
|
2023-04-21 16:03:50 +08:00
|
|
|
{
|
|
|
|
return self.Exclude<TComponent, EcsPool<TComponent>>();
|
|
|
|
}
|
2023-05-07 00:50:02 +08:00
|
|
|
public static EcsPool<TComponent> Optional<TComponent>(this EcsSubjectBuilderBase self) where TComponent : struct, IEcsComponent
|
2023-04-21 16:03:50 +08:00
|
|
|
{
|
|
|
|
return self.Optional<TComponent, EcsPool<TComponent>>();
|
|
|
|
}
|
2023-04-21 03:16:05 +08:00
|
|
|
}
|
2023-02-05 19:59:45 +08:00
|
|
|
}
|