refactoring

This commit is contained in:
Mikhail 2023-05-26 03:45:35 +08:00
parent 6fd85813c3
commit caef8d8bae
5 changed files with 93 additions and 223 deletions

View File

@ -5,37 +5,8 @@ using System.Linq;
namespace DCFApixels.DragonECS
{
//TODO развить идею инжектов
//1) добавить расширенный метод инжекта, с 2 джинерик-аргументами, первый базовый тип и второй инжектируемый тип.
//напримере это будет работать так Inject<object, Foo> делает инжект объекта типа Foo для систем с IEcsInject<object> или с IEcsInject<Foo>
//2) добавить контейнер, который автоматически создается, собирает в себя все пре-инжекты и авто-инжектится во все системы.
//добавить контейнер, который автоматически создается, собирает в себя все пре-инжекты и авто-инжектится во все системы.
//но это спорная идея
namespace Internal
{
internal class PreInitInjectController
{
private EcsPipeline _source;
private InjectSystemBase[] _injectSystems;
private int _injectCount;
public PreInitInjectController(EcsPipeline source)
{
_injectCount = 0;
_source = source;
_injectSystems = _source.AllSystems.OfType<InjectSystemBase>().ToArray();
}
public bool OnInject()
{
_injectCount++;
return IsInjectionEnd;
}
public bool IsInjectionEnd => _injectCount >= _injectSystems.Length;
public void Destroy()
{
_source = null;
_injectSystems = null;
}
}
}
public interface IEcsPreInject : IEcsSystem
{
void PreInject(object obj);
@ -52,6 +23,29 @@ namespace DCFApixels.DragonECS
namespace Internal
{
internal class PreInitInjectController
{
private EcsPipeline _source;
private InjectSystemBase[] _injectSystems;
private int _injectCount;
public bool IsInjectionEnd => _injectCount >= _injectSystems.Length;
public PreInitInjectController(EcsPipeline source)
{
_injectCount = 0;
_source = source;
_injectSystems = _source.AllSystems.OfType<InjectSystemBase>().ToArray();
}
public bool OnInject()
{
_injectCount++;
return IsInjectionEnd;
}
public void Destroy()
{
_source = null;
_injectSystems = null;
}
}
[DebugHide, DebugColor(DebugColor.Gray)]
public sealed class EcsPreInjectRunner : EcsRunner<IEcsPreInject>, IEcsPreInject
{
@ -86,55 +80,43 @@ namespace DCFApixels.DragonECS
foreach (var item in targets) item.OnPreInitInjectionBefore();
}
}
}
public class InjectSystemBase { }
[DebugHide, DebugColor(DebugColor.Gray)]
public class InjectSystem<T> : InjectSystemBase, IEcsPreInitProcess, IEcsInject<PreInitInjectController>, IEcsPreInitInjectProcess
{
private T _injectedData;
private PreInitInjectController _injectController;
void IEcsInject<PreInitInjectController>.Inject(PreInitInjectController obj) => _injectController = obj;
public InjectSystem(T injectedData)
public class InjectSystemBase { }
[DebugHide, DebugColor(DebugColor.Gray)]
public class InjectSystem<T> : InjectSystemBase, IEcsPreInitProcess, IEcsInject<PreInitInjectController>, IEcsPreInitInjectProcess
{
_injectedData = injectedData;
}
public void PreInit(EcsPipeline pipeline)
{
if (_injectedData == null)
return;
if (_injectController == null)
private T _injectedData;
private PreInitInjectController _injectController;
void IEcsInject<PreInitInjectController>.Inject(PreInitInjectController obj) => _injectController = obj;
public InjectSystem(T injectedData)
{
_injectController = new PreInitInjectController(pipeline);
var injectMapRunner = pipeline.GetRunner<IEcsInject<PreInitInjectController>>();
pipeline.GetRunner<IEcsPreInitInjectProcess>().OnPreInitInjectionBefore();
injectMapRunner.Inject(_injectController);
_injectedData = injectedData;
}
var injectRunnerGeneric = pipeline.GetRunner<IEcsInject<T>>();
injectRunnerGeneric.Inject(_injectedData);
if (_injectController.OnInject())
public void PreInit(EcsPipeline pipeline)
{
_injectController.Destroy();
var injectCallbacksRunner = pipeline.GetRunner<IEcsPreInitInjectProcess>();
injectCallbacksRunner.OnPreInitInjectionAfter();
EcsRunner.Destroy(injectCallbacksRunner);
if (_injectedData == null) return;
if (_injectController == null)
{
_injectController = new PreInitInjectController(pipeline);
var injectMapRunner = pipeline.GetRunner<IEcsInject<PreInitInjectController>>();
pipeline.GetRunner<IEcsPreInitInjectProcess>().OnPreInitInjectionBefore();
injectMapRunner.Inject(_injectController);
}
var injectRunnerGeneric = pipeline.GetRunner<IEcsInject<T>>();
injectRunnerGeneric.Inject(_injectedData);
if (_injectController.OnInject())
{
_injectController.Destroy();
var injectCallbacksRunner = pipeline.GetRunner<IEcsPreInitInjectProcess>();
injectCallbacksRunner.OnPreInitInjectionAfter();
EcsRunner.Destroy(injectCallbacksRunner);
}
}
}
public void OnPreInitInjectionBefore() { }
public void OnPreInitInjectionAfter()
{
_injectController = null;
public void OnPreInitInjectionBefore() { }
public void OnPreInitInjectionAfter() => _injectController = null;
}
}
public static class InjectSystemExstensions
public static class InjectSystemExtensions
{
public static EcsPipeline.Builder Inject<T>(this EcsPipeline.Builder self, T data)
{

View File

@ -1,53 +1,51 @@
using System.Collections.Generic;
using DCFApixels.DragonECS.Internal;
namespace DCFApixels.DragonECS
{
[DebugHide, DebugColor(DebugColor.Black)]
public class SystemsBlockMarkerSystem : IEcsSystem
namespace Internal
{
public readonly string name;
public SystemsBlockMarkerSystem(string name) { this.name = name; }
}
[DebugHide, DebugColor(DebugColor.Grey)]
public class DeleteEmptyEntitesSystem : IEcsRunProcess, IEcsPreInject
{
private List<EcsWorld> _worlds = new List<EcsWorld>();
public void PreInject(object obj)
[DebugHide, DebugColor(DebugColor.Black)]
public class SystemsBlockMarkerSystem : IEcsSystem
{
if (obj is EcsWorld world)
_worlds.Add(world);
public readonly string name;
public SystemsBlockMarkerSystem(string name) => this.name = name;
}
public void Run(EcsPipeline pipeline)
[DebugHide, DebugColor(DebugColor.Grey)]
public class DeleteEmptyEntitesSystem : IEcsRunProcess, IEcsPreInject
{
foreach (var world in _worlds)
world.DeleteEmptyEntites();
}
}
[DebugHide, DebugColor(DebugColor.Grey)]
public class DeleteOneFrameComponentSystem<TWorld, TComponent> : IEcsRunProcess, IEcsInject<TWorld>
where TWorld : EcsWorld<TWorld>
where TComponent : struct, IEcsComponent
{
private TWorld _world;
public void Inject(TWorld obj) => _world = obj;
private sealed class Subject : EcsSubject
{
public EcsPool<TComponent> pool;
public Subject(Builder b)
private List<EcsWorld> _worlds = new List<EcsWorld>();
public void PreInject(object obj)
{
pool = b.Include<TComponent>();
if (obj is EcsWorld world)
_worlds.Add(world);
}
public void Run(EcsPipeline pipeline)
{
foreach (var world in _worlds)
world.DeleteEmptyEntites();
}
}
public void Run(EcsPipeline pipeline)
[DebugHide, DebugColor(DebugColor.Grey)]
public class DeleteOneFrameComponentSystem<TWorld, TComponent> : IEcsRunProcess, IEcsInject<TWorld>
where TWorld : EcsWorld<TWorld>
where TComponent : struct, IEcsComponent
{
foreach (var e in _world.Where(out Subject s))
s.pool.Del(e);
private sealed class Subject : EcsSubject
{
public EcsPool<TComponent> pool;
public Subject(Builder b) => pool = b.Include<TComponent>();
}
private TWorld _world;
public void Inject(TWorld obj) => _world = obj;
public void Run(EcsPipeline pipeline)
{
foreach (var e in _world.Where(out Subject s))
s.pool.Del(e);
}
}
}
public static class DeleteOneFrameComponentSystemExt
public static class DeleteOneFrameComponentSystemExtensions
{
private const string AUTO_DEL_LAYER = nameof(AUTO_DEL_LAYER);
public static EcsPipeline.Builder AutoDel<TWorld, TComponent>(this EcsPipeline.Builder b)

View File

@ -1,106 +0,0 @@
using DCFApixels.DragonECS.RunnersCore;
namespace DCFApixels.DragonECS
{
public interface IEcsComponentAdd : IEcsSystem
{
void OnComponentAdd<T>(int entityID);
}
public interface IEcsComponentWrite : IEcsSystem
{
void OnComponentWrite<T>(int entityID);
}
public interface IEcsComponentDel : IEcsSystem
{
void OnComponentDel<T>(int entityID);
}
public interface IEcsComponentLifecycle : IEcsComponentAdd, IEcsComponentWrite, IEcsComponentDel { }
namespace Internal
{
[DebugColor(DebugColor.Orange)]
public sealed class EcsEntityAddComponentRunner : EcsRunner<IEcsComponentAdd>, IEcsComponentAdd
{
public void OnComponentAdd<T>(int entityID)
{
foreach (var item in targets) item.OnComponentAdd<T>(entityID);
}
}
[DebugColor(DebugColor.Orange)]
public sealed class EcsEntityChangeComponentRunner : EcsRunner<IEcsComponentWrite>, IEcsComponentWrite
{
public void OnComponentWrite<T>(int entityID)
{
foreach (var item in targets) item.OnComponentWrite<T>(entityID);
}
}
[DebugColor(DebugColor.Orange)]
public sealed class EcsEntityDelComponentRunner : EcsRunner<IEcsComponentDel>, IEcsComponentDel
{
public void OnComponentDel<T>(int entityID)
{
foreach (var item in targets) item.OnComponentDel<T>(entityID);
}
}
}
public interface IEcsEntityCreate : IEcsSystem
{
void OnEntityCreate(int entityID);
}
public interface IEcsEntityDestroy : IEcsSystem
{
void OnEntityDestroy(int entityID);
}
public interface IEcsEntityLifecycle : IEcsEntityCreate, IEcsEntityDestroy { }
namespace Internal
{
[DebugColor(DebugColor.Orange)]
public sealed class EcsEntityCreateRunner : EcsRunner<IEcsEntityCreate>, IEcsEntityCreate
{
public void OnEntityCreate(int entityID)
{
foreach (var item in targets) item.OnEntityCreate(entityID);
}
}
[DebugColor(DebugColor.Orange)]
public sealed class EcsEntityDestroyRunner : EcsRunner<IEcsEntityDestroy>, IEcsEntityDestroy
{
public void OnEntityDestroy(int entityID)
{
foreach (var item in targets) item.OnEntityDestroy(entityID);
}
}
}
public interface IEcsWorldCreate : IEcsSystem
{
void OnWorldCreate(EcsWorld world);
}
public interface IEcsWorldDestroy : IEcsSystem
{
void OnWorldDestroy(EcsWorld world);
}
public interface IEcsWorldLifecycle : IEcsWorldCreate, IEcsWorldDestroy { }
namespace Internal
{
[DebugColor(DebugColor.Orange)]
public sealed class EcsWorldCreateRunner : EcsRunner<IEcsWorldCreate>, IEcsWorldCreate
{
public void OnWorldCreate(EcsWorld world)
{
foreach (var item in targets) item.OnWorldCreate(world);
}
}
[DebugColor(DebugColor.Orange)]
public sealed class EcsWorldDestryRunner : EcsRunner<IEcsWorldDestroy>, IEcsWorldDestroy
{
public void OnWorldDestroy(EcsWorld world)
{
foreach (var item in targets) item.OnWorldDestroy(world);
}
}
}
}

View File

@ -1,5 +1,9 @@
namespace DCFApixels.DragonECS
{
internal sealed class EcsNullWorld : EcsWorld<EcsNullWorld>
{
public EcsNullWorld() : base(false) { }
}
public sealed class EcsDefaultWorld : EcsWorld<EcsDefaultWorld> { }
public sealed class EcsEventWorld : EcsWorld<EcsEventWorld> { }
}

View File

@ -1,16 +1,10 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using DCFApixels.DragonECS.Internal;
namespace DCFApixels.DragonECS
{
using Internal;
internal sealed class EcsNullWorld : EcsWorld<EcsNullWorld>
{
public EcsNullWorld() : base(false) { }
}
public abstract class EcsWorld
{
private const short GEN_BITS = 0x7fff;
@ -19,6 +13,7 @@ namespace DCFApixels.DragonECS
public static EcsWorld[] Worlds = new EcsWorld[8];
private static IntDispenser _worldIdDispenser = new IntDispenser(0);
public readonly short uniqueID;
private int _worldTypeID;
@ -598,9 +593,6 @@ namespace DCFApixels.DragonECS
void OnNewEntity(int entityID);
void OnDelEntity(int entityID);
}
#endregion
#region Extensions
internal static class WorldEventListExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]