2025-03-14 16:53:25 +08:00
|
|
|
#if DISABLE_DEBUG
|
|
|
|
|
#undef DEBUG
|
|
|
|
|
#endif
|
2023-06-21 01:37:05 +08:00
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
|
2025-03-17 13:05:23 +08:00
|
|
|
namespace DCFApixels.DragonECS.Core
|
2023-06-21 01:37:05 +08:00
|
|
|
{
|
|
|
|
|
#region IEcsWorldComponent
|
|
|
|
|
public interface IEcsWorldComponent<T>
|
|
|
|
|
{
|
|
|
|
|
void Init(ref T component, EcsWorld world);
|
2023-06-21 15:03:33 +08:00
|
|
|
void OnDestroy(ref T component, EcsWorld world);
|
2023-06-21 01:37:05 +08:00
|
|
|
}
|
2026-03-15 21:56:48 +08:00
|
|
|
public static class EcsWorldComponent<T>
|
2023-06-21 01:37:05 +08:00
|
|
|
{
|
2026-03-15 17:28:11 +08:00
|
|
|
public static readonly IEcsWorldComponent<T> CustomHandler;
|
|
|
|
|
public static readonly bool IsCustom;
|
|
|
|
|
static EcsWorldComponent()
|
2023-06-21 01:37:05 +08:00
|
|
|
{
|
2026-03-15 17:28:11 +08:00
|
|
|
T raw = default;
|
|
|
|
|
if (raw is IEcsWorldComponent<T> handler)
|
2023-06-21 01:37:05 +08:00
|
|
|
{
|
2026-03-15 17:28:11 +08:00
|
|
|
IsCustom = true;
|
|
|
|
|
CustomHandler = handler;
|
2023-06-21 01:37:05 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2026-03-15 17:28:11 +08:00
|
|
|
IsCustom = false;
|
|
|
|
|
CustomHandler = new DummyHandler();
|
2023-06-21 01:37:05 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-15 17:28:11 +08:00
|
|
|
private sealed class DummyHandler : IEcsWorldComponent<T>
|
2023-06-21 01:37:05 +08:00
|
|
|
{
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public void Init(ref T component, EcsWorld world) { }
|
2026-03-15 17:28:11 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-06-21 15:03:33 +08:00
|
|
|
public void OnDestroy(ref T component, EcsWorld world) { }
|
2023-06-21 01:37:05 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2026-03-15 17:28:11 +08:00
|
|
|
#region IEcsComponentLifecycle
|
2024-03-10 19:22:47 +08:00
|
|
|
public interface IEcsComponentLifecycle<T>
|
2023-06-21 01:37:05 +08:00
|
|
|
{
|
2026-03-15 17:28:11 +08:00
|
|
|
void OnAdd(ref T component, short worldID, int entityID);
|
|
|
|
|
void OnDel(ref T component, short worldID, int entityID);
|
2023-06-21 01:37:05 +08:00
|
|
|
}
|
2026-03-15 17:28:11 +08:00
|
|
|
public static class EcsComponentLifecycle<T> where T : struct
|
2023-06-21 01:37:05 +08:00
|
|
|
{
|
2026-03-15 17:28:11 +08:00
|
|
|
public static readonly IEcsComponentLifecycle<T> CustomHandler;
|
|
|
|
|
public static readonly bool IsCustom;
|
|
|
|
|
static EcsComponentLifecycle()
|
2023-06-21 01:37:05 +08:00
|
|
|
{
|
2026-03-15 17:28:11 +08:00
|
|
|
T raw = default;
|
|
|
|
|
if (raw is IEcsComponentLifecycle<T> handler)
|
2023-06-21 01:37:05 +08:00
|
|
|
{
|
2026-03-15 17:28:11 +08:00
|
|
|
IsCustom = true;
|
|
|
|
|
CustomHandler = handler;
|
2023-06-21 01:37:05 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2026-03-15 17:28:11 +08:00
|
|
|
IsCustom = false;
|
|
|
|
|
CustomHandler = new DummyHandler();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static void OnAdd(bool isCustom, IEcsComponentLifecycle<T> custom, ref T component, short worldID, int entityID)
|
|
|
|
|
{
|
|
|
|
|
if (isCustom)
|
|
|
|
|
{
|
|
|
|
|
custom.OnAdd(ref component, worldID, entityID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static void OnDel(bool isCustom, IEcsComponentLifecycle<T> custom, ref T component, short worldID, int entityID)
|
|
|
|
|
{
|
|
|
|
|
if (isCustom)
|
|
|
|
|
{
|
|
|
|
|
custom.OnDel(ref component, worldID, entityID);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
component = default;
|
2023-06-21 01:37:05 +08:00
|
|
|
}
|
|
|
|
|
}
|
2024-03-10 19:22:47 +08:00
|
|
|
private sealed class DummyHandler : IEcsComponentLifecycle<T>
|
2023-06-21 01:37:05 +08:00
|
|
|
{
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2026-03-15 17:28:11 +08:00
|
|
|
public void OnAdd(ref T component, short worldID, int entityID) { component = default; }
|
2024-03-10 19:22:47 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2026-03-15 17:28:11 +08:00
|
|
|
public void OnDel(ref T component, short worldID, int entityID) { component = default; }
|
2023-06-21 01:37:05 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region IEcsComponentCopy
|
|
|
|
|
public interface IEcsComponentCopy<T>
|
|
|
|
|
{
|
|
|
|
|
void Copy(ref T from, ref T to);
|
|
|
|
|
}
|
2026-03-15 17:28:11 +08:00
|
|
|
public static class EcsComponentCopy<T> where T : struct
|
2023-06-21 01:37:05 +08:00
|
|
|
{
|
2026-03-15 17:28:11 +08:00
|
|
|
public static readonly IEcsComponentCopy<T> CustomHandler;
|
|
|
|
|
public static readonly bool IsCustom;
|
|
|
|
|
static EcsComponentCopy()
|
|
|
|
|
{
|
|
|
|
|
T raw = default;
|
|
|
|
|
if (raw is IEcsComponentCopy<T> handler)
|
|
|
|
|
{
|
|
|
|
|
IsCustom = true;
|
|
|
|
|
CustomHandler = handler;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
IsCustom = false;
|
|
|
|
|
CustomHandler = new DummyHandler();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static void Copy(bool isCustom, IEcsComponentCopy<T> custom, ref T from, ref T to)
|
2023-06-21 01:37:05 +08:00
|
|
|
{
|
2026-03-15 17:28:11 +08:00
|
|
|
if (isCustom)
|
2023-06-21 01:37:05 +08:00
|
|
|
{
|
2026-03-15 17:28:11 +08:00
|
|
|
custom.Copy(ref from, ref to);
|
2023-06-21 01:37:05 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2026-03-15 17:28:11 +08:00
|
|
|
to = from;
|
2023-06-21 01:37:05 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private sealed class DummyHandler : IEcsComponentCopy<T>
|
|
|
|
|
{
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-09-07 17:18:35 +08:00
|
|
|
public void Copy(ref T from, ref T to) { to = from; }
|
2023-06-21 01:37:05 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2026-03-15 17:28:11 +08:00
|
|
|
}
|