DragonECS/src/DataInterfaces.cs

94 lines
2.6 KiB
C#
Raw Normal View History

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-01-25 20:11:06 +08:00
instance = intrf;
2023-06-21 01:37:05 +08:00
}
else
{
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 IEcsComponentReset<T>
{
void Reset(ref T component);
}
public static class EcsComponentResetHandler<T>
{
public static readonly IEcsComponentReset<T> instance;
public static readonly bool isHasHandler;
static EcsComponentResetHandler()
{
2024-01-25 20:11:06 +08:00
T def = default;
if (def is IEcsComponentReset<T> intrf)
2023-06-21 01:37:05 +08:00
{
2024-01-25 20:11:06 +08:00
instance = intrf;
2023-06-21 01:37:05 +08:00
}
else
{
instance = new DummyHandler();
}
}
private sealed class DummyHandler : IEcsComponentReset<T>
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Reset(ref T component) => component = default;
}
}
#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-01-25 20:11:06 +08:00
instance = intrf;
2023-06-21 01:37:05 +08:00
}
else
{
instance = new DummyHandler();
}
}
private sealed class DummyHandler : IEcsComponentCopy<T>
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Copy(ref T from, ref T to) => to = from;
}
}
#endregion
}