mirror of
https://github.com/DCFApixels/DragonECS.git
synced 2025-09-17 17:34:36 +08:00
refactoring
This commit is contained in:
parent
815b1c2bb0
commit
8f39070e22
@ -88,7 +88,7 @@ namespace DCFApixels.DragonECS
|
||||
_items = new T[capacity];
|
||||
_itemsCount = 0;
|
||||
|
||||
_componentResetHandler = ComponentResetHandler.New<T>();
|
||||
_componentResetHandler = IEcsComponentReset<T>.Handler;
|
||||
_poolRunnres = poolRunnres;
|
||||
}
|
||||
#endregion
|
||||
|
@ -1,7 +1,43 @@
|
||||
namespace DCFApixels.DragonECS
|
||||
using System.Runtime.CompilerServices;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace DCFApixels.DragonECS
|
||||
{
|
||||
public interface IEcsComponentReset<T>
|
||||
{
|
||||
public void Reset(ref T component);
|
||||
|
||||
|
||||
private static IEcsComponentReset<T> _handler;
|
||||
public static IEcsComponentReset<T> Handler
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
if(_handler == null)
|
||||
{
|
||||
Type targetType = typeof(T);
|
||||
if (targetType.GetInterfaces().Contains(typeof(IEcsComponentReset<>).MakeGenericType(targetType)))
|
||||
_handler = (IEcsComponentReset<T>)Activator.CreateInstance(typeof(ComponentResetHandler<>).MakeGenericType(targetType));
|
||||
else
|
||||
_handler = new ComponentResetDummy<T>();
|
||||
}
|
||||
return _handler;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class ComponentResetDummy<T> : IEcsComponentReset<T>
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Reset(ref T component) => component = default;
|
||||
}
|
||||
internal sealed class ComponentResetHandler<T> : IEcsComponentReset<T>
|
||||
where T : IEcsComponentReset<T>
|
||||
{
|
||||
private T _fakeInstnace = default;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Reset(ref T component) => _fakeInstnace.Reset(ref component);
|
||||
}
|
||||
}
|
||||
|
@ -1,32 +0,0 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace DCFApixels.DragonECS
|
||||
{
|
||||
internal static class ComponentResetHandler
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static IEcsComponentReset<T> New<T>()
|
||||
{
|
||||
Type targetType = typeof(T);
|
||||
if (targetType.GetInterfaces().Contains(typeof(IEcsComponentReset<>).MakeGenericType(targetType)))
|
||||
{
|
||||
return (IEcsComponentReset<T>)Activator.CreateInstance(typeof(ComponentResetHandler<>).MakeGenericType(targetType));
|
||||
}
|
||||
return (IEcsComponentReset<T>)Activator.CreateInstance(typeof(ComponentResetDummy<>).MakeGenericType(targetType));
|
||||
}
|
||||
}
|
||||
internal sealed class ComponentResetDummy<T> : IEcsComponentReset<T>
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Reset(ref T component) => component = default;
|
||||
}
|
||||
internal sealed class ComponentResetHandler<T> : IEcsComponentReset<T>
|
||||
where T : IEcsComponentReset<T>
|
||||
{
|
||||
private T _fakeInstnace = default;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Reset(ref T component) => _fakeInstnace.Reset(ref component);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user