DragonECS/src/Pools/EcsPoolBase.cs

225 lines
8.2 KiB
C#
Raw Normal View History

2023-05-23 01:47:28 +08:00
using DCFApixels.DragonECS.Internal;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace DCFApixels.DragonECS
{
2024-02-25 00:55:30 +08:00
public interface IEcsReadonlyPool
{
#region Properties
2024-02-25 00:55:30 +08:00
int ComponentTypeID { get; }
2023-06-12 20:46:51 +08:00
Type ComponentType { get; }
EcsWorld World { get; }
int Count { get; }
2024-02-25 17:27:08 +08:00
bool IsReadOnly { get; }
#endregion
#region Methods
bool Has(int entityID);
object GetRaw(int entityID);
void Copy(int fromEntityID, int toEntityID);
2023-05-23 01:47:28 +08:00
void Copy(int fromEntityID, EcsWorld toWorld, int toEntityID);
#endregion
#region Add/Remove Listeners
void AddListener(IEcsPoolEventListener listener);
void RemoveListener(IEcsPoolEventListener listener);
#endregion
}
2024-02-25 00:55:30 +08:00
public interface IEcsPool : IEcsReadonlyPool
{
#region Methods
void AddRaw(int entityID, object dataRaw);
void SetRaw(int entityID, object dataRaw);
void Del(int entityID);
2024-03-13 17:41:33 +08:00
void ClearAll();
2024-02-25 00:55:30 +08:00
#endregion
}
2024-02-24 02:26:42 +08:00
/// <summary>A pool for struct components.</summary>
2024-02-26 11:58:19 +08:00
public interface IEcsStructPool<T> : IEcsPool where T : struct
{
ref T Add(int entityID);
ref readonly T Read(int entityID);
2023-05-30 16:01:16 +08:00
ref T Get(int entityID);
}
2024-02-24 02:26:42 +08:00
/// <summary>A pool for reference components of type T that instantiates components itself.</summary>
2024-02-26 11:58:19 +08:00
public interface IEcsClassPool<T> : IEcsPool where T : class
2023-06-25 23:13:51 +08:00
{
T Add(int entityID);
T Get(int entityID);
}
2024-02-24 02:26:42 +08:00
/// <summary>A pool for reference components of type T, which does not instantiate components itself but receives components from external sources..</summary>
2024-02-26 11:58:19 +08:00
public interface IEcsHybridPool<T> : IEcsPool where T : class
2023-06-29 23:53:26 +08:00
{
void Add(int entityID, T component);
T Get(int entityID);
}
/// <summary>Only used to implement a custom pool. In other contexts use IEcsPool or IEcsPool<T>.</summary>
public interface IEcsPoolImplementation : IEcsPool
{
void OnInit(EcsWorld world, EcsWorld.PoolsMediator mediator, int componentTypeID);
void OnWorldResize(int newSize);
void OnReleaseDelEntityBuffer(ReadOnlySpan<int> buffer);
void OnWorldDestroy();
}
/// <summary>Only used to implement a custom pool. In other contexts use IEcsPool or IEcsPool<T>.</summary>
/// <typeparam name="T">Component type</typeparam>
public interface IEcsPoolImplementation<T> : IEcsPoolImplementation { }
public static class EcsPoolThrowHalper
{
public static void ThrowAlreadyHasComponent<T>(int entityID)
{
2023-06-26 02:53:55 +08:00
throw new EcsFrameworkException($"Entity({entityID}) already has component {EcsDebugUtility.GetGenericTypeName<T>()}.");
}
public static void ThrowNotHaveComponent<T>(int entityID)
{
2023-06-26 02:53:55 +08:00
throw new EcsFrameworkException($"Entity({entityID}) has no component {EcsDebugUtility.GetGenericTypeName<T>()}.");
}
2024-02-24 02:26:42 +08:00
public static void ThrowAlreadyHasComponent(Type type, int entityID)
{
throw new EcsFrameworkException($"Entity({entityID}) already has component {EcsDebugUtility.GetGenericTypeName(type)}.");
}
public static void ThrowNotHaveComponent(Type type, int entityID)
{
throw new EcsFrameworkException($"Entity({entityID}) has no component {EcsDebugUtility.GetGenericTypeName(type)}.");
}
}
public static class IEcsPoolImplementationExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNullOrDummy(this IEcsPool self)
{
2023-05-23 01:47:28 +08:00
return self == null || self == EcsNullPool.instance;
}
}
2023-05-28 05:53:08 +08:00
#region Dummy EcsNullPool
namespace Internal
{
public struct NullComponent { }
public sealed class EcsNullPool : IEcsPoolImplementation<NullComponent>
{
2024-01-01 21:44:33 +08:00
public static readonly EcsNullPool instance = new EcsNullPool();
#region Properties
2024-03-13 17:41:33 +08:00
int IEcsReadonlyPool.ComponentTypeID { get { return 0; } }//TODO Првоерить что NullComponent всегда имеет id 0
Type IEcsReadonlyPool.ComponentType { get { return typeof(NullComponent); } }
EcsWorld IEcsReadonlyPool.World
{
get
{
#if (DEBUG && !DISABLE_DEBUG)
throw new NullInstanceException();
#endif
}
}
public int Count { get { return 0; } }
2024-02-25 17:27:08 +08:00
public bool IsReadOnly { get { return true; } }
#endregion
#region Methods
2024-03-13 17:41:33 +08:00
bool IEcsReadonlyPool.Has(int index)
{
return false;
}
void IEcsPool.Del(int entityID)
{
#if (DEBUG && !DISABLE_DEBUG)
throw new NullInstanceException();
#endif
}
void IEcsPool.AddRaw(int entityID, object dataRaw)
{
#if (DEBUG && !DISABLE_DEBUG)
throw new NullInstanceException();
#endif
}
object IEcsReadonlyPool.GetRaw(int entityID)
{
#if (DEBUG && !DISABLE_DEBUG)
throw new NullInstanceException();
#endif
}
void IEcsPool.SetRaw(int entity, object dataRaw)
{
#if (DEBUG && !DISABLE_DEBUG)
throw new NullInstanceException();
#endif
}
void IEcsReadonlyPool.Copy(int fromEntityID, int toEntityID)
{
#if (DEBUG && !DISABLE_DEBUG)
throw new NullInstanceException();
#endif
}
void IEcsReadonlyPool.Copy(int fromEntityID, EcsWorld toWorld, int toEntityID)
{
#if (DEBUG && !DISABLE_DEBUG)
throw new NullInstanceException();
#endif
}
void IEcsPool.ClearAll()
{
#if (DEBUG && !DISABLE_DEBUG)
throw new NullInstanceException();
#endif
}
#endregion
#region Callbacks
void IEcsPoolImplementation.OnInit(EcsWorld world, EcsWorld.PoolsMediator mediator, int componentTypeID) { }
void IEcsPoolImplementation.OnWorldDestroy() { }
void IEcsPoolImplementation.OnWorldResize(int newSize) { }
void IEcsPoolImplementation.OnReleaseDelEntityBuffer(ReadOnlySpan<int> buffer) { }
#endregion
#region Listeners
2024-02-25 00:55:30 +08:00
void IEcsReadonlyPool.AddListener(IEcsPoolEventListener listener) { }
void IEcsReadonlyPool.RemoveListener(IEcsPoolEventListener listener) { }
#endregion
}
}
#endregion
2023-05-28 05:53:08 +08:00
#region Callbacks Interface
public interface IEcsPoolEventListener
{
2023-05-30 16:07:45 +08:00
/// <summary>Called after adding an entity to the pool, but before changing values</summary>
2023-05-28 05:53:08 +08:00
void OnAdd(int entityID);
2023-05-30 16:07:45 +08:00
/// <summary>Is called when EcsPool.Get or EcsPool.Add is called, but before changing values</summary>
2023-05-30 16:01:16 +08:00
void OnGet(int entityID);
2023-05-28 05:53:08 +08:00
/// <summary>Called after deleting an entity from the pool</summary>
void OnDel(int entityID);
}
public static class PoolEventListExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void InvokeOnAdd(this List<IEcsPoolEventListener> self, int entityID)
{
for (int i = 0, iMax = self.Count; i < iMax; i++) self[i].OnAdd(entityID);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-05-30 16:01:16 +08:00
public static void InvokeOnAddAndGet(this List<IEcsPoolEventListener> self, int entityID)
{
for (int i = 0, iMax = self.Count; i < iMax; i++)
{
self[i].OnAdd(entityID);
2023-05-30 16:01:16 +08:00
self[i].OnGet(entityID);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-05-30 16:01:16 +08:00
public static void InvokeOnGet(this List<IEcsPoolEventListener> self, int entityID)
{
2023-05-30 16:01:16 +08:00
for (int i = 0, iMax = self.Count; i < iMax; i++) self[i].OnGet(entityID);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void InvokeOnDel(this List<IEcsPoolEventListener> self, int entityID)
{
for (int i = 0, iMax = self.Count; i < iMax; i++) self[i].OnDel(entityID);
}
}
#endregion
}