DragonECS/src/DataInterfaces.cs

106 lines
3.1 KiB
C#
Raw Normal View History

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;
namespace DCFApixels.DragonECS
{
#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
}
public static class EcsWorldComponentHandler<T>
{
public static readonly IEcsWorldComponent<T> instance;
public static readonly bool isHasHandler;
static EcsWorldComponentHandler()
{
2024-01-25 20:11:06 +08:00
T def = default;
if (def is IEcsWorldComponent<T> intrf)
2023-06-21 01:37:05 +08:00
{
2024-02-09 22:20:55 +08:00
isHasHandler = true;
2024-01-25 20:11:06 +08:00
instance = intrf;
2023-06-21 01:37:05 +08:00
}
else
{
2024-02-09 22:20:55 +08:00
isHasHandler = false;
2023-06-21 01:37:05 +08:00
instance = new DummyHandler();
}
}
private class DummyHandler : IEcsWorldComponent<T>
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Init(ref T component, EcsWorld world) { }
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
#region IEcsComponentReset
public interface IEcsComponentLifecycle<T>
2023-06-21 01:37:05 +08:00
{
void Enable(ref T component);
void Disable(ref T component);
2023-06-21 01:37:05 +08:00
}
public static class EcsComponentResetHandler<T>
{
public static readonly IEcsComponentLifecycle<T> instance;
2023-06-21 01:37:05 +08:00
public static readonly bool isHasHandler;
static EcsComponentResetHandler()
{
2024-01-25 20:11:06 +08:00
T def = default;
if (def is IEcsComponentLifecycle<T> intrf)
2023-06-21 01:37:05 +08:00
{
2024-02-09 22:20:55 +08:00
isHasHandler = true;
2024-01-25 20:11:06 +08:00
instance = intrf;
2023-06-21 01:37:05 +08:00
}
else
{
2024-02-09 22:20:55 +08:00
isHasHandler = false;
2023-06-21 01:37:05 +08:00
instance = new DummyHandler();
}
}
private sealed class DummyHandler : IEcsComponentLifecycle<T>
2023-06-21 01:37:05 +08:00
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-09-07 17:18:35 +08:00
public void Enable(ref T component) { component = default; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-09-07 17:18:35 +08:00
public void Disable(ref T component) { 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);
}
public static class EcsComponentCopyHandler<T>
{
public static readonly IEcsComponentCopy<T> instance;
public static readonly bool isHasHandler;
static EcsComponentCopyHandler()
{
2024-01-25 20:11:06 +08:00
T def = default;
2024-02-03 01:12:53 +08:00
if (def is IEcsComponentCopy<T> intrf)
2023-06-21 01:37:05 +08:00
{
2024-02-09 22:20:55 +08:00
isHasHandler = true;
2024-01-25 20:11:06 +08:00
instance = intrf;
2023-06-21 01:37:05 +08:00
}
else
{
2024-02-09 22:20:55 +08:00
isHasHandler = false;
2023-06-21 01:37:05 +08:00
instance = new DummyHandler();
}
}
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
}