mirror of
https://github.com/DCFApixels/DragonECS.git
synced 2025-11-14 01:35:54 +08:00
refactoring
This commit is contained in:
parent
6fd85813c3
commit
caef8d8bae
@ -5,37 +5,8 @@ using System.Linq;
|
|||||||
namespace DCFApixels.DragonECS
|
namespace DCFApixels.DragonECS
|
||||||
{
|
{
|
||||||
//TODO развить идею инжектов
|
//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
|
public interface IEcsPreInject : IEcsSystem
|
||||||
{
|
{
|
||||||
void PreInject(object obj);
|
void PreInject(object obj);
|
||||||
@ -52,6 +23,29 @@ namespace DCFApixels.DragonECS
|
|||||||
|
|
||||||
namespace Internal
|
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)]
|
[DebugHide, DebugColor(DebugColor.Gray)]
|
||||||
public sealed class EcsPreInjectRunner : EcsRunner<IEcsPreInject>, IEcsPreInject
|
public sealed class EcsPreInjectRunner : EcsRunner<IEcsPreInject>, IEcsPreInject
|
||||||
{
|
{
|
||||||
@ -86,28 +80,20 @@ namespace DCFApixels.DragonECS
|
|||||||
foreach (var item in targets) item.OnPreInitInjectionBefore();
|
foreach (var item in targets) item.OnPreInitInjectionBefore();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public class InjectSystemBase { }
|
public class InjectSystemBase { }
|
||||||
|
|
||||||
[DebugHide, DebugColor(DebugColor.Gray)]
|
[DebugHide, DebugColor(DebugColor.Gray)]
|
||||||
public class InjectSystem<T> : InjectSystemBase, IEcsPreInitProcess, IEcsInject<PreInitInjectController>, IEcsPreInitInjectProcess
|
public class InjectSystem<T> : InjectSystemBase, IEcsPreInitProcess, IEcsInject<PreInitInjectController>, IEcsPreInitInjectProcess
|
||||||
{
|
{
|
||||||
private T _injectedData;
|
private T _injectedData;
|
||||||
|
|
||||||
private PreInitInjectController _injectController;
|
private PreInitInjectController _injectController;
|
||||||
void IEcsInject<PreInitInjectController>.Inject(PreInitInjectController obj) => _injectController = obj;
|
void IEcsInject<PreInitInjectController>.Inject(PreInitInjectController obj) => _injectController = obj;
|
||||||
|
|
||||||
public InjectSystem(T injectedData)
|
public InjectSystem(T injectedData)
|
||||||
{
|
{
|
||||||
_injectedData = injectedData;
|
_injectedData = injectedData;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void PreInit(EcsPipeline pipeline)
|
public void PreInit(EcsPipeline pipeline)
|
||||||
{
|
{
|
||||||
if (_injectedData == null)
|
if (_injectedData == null) return;
|
||||||
return;
|
|
||||||
|
|
||||||
if (_injectController == null)
|
if (_injectController == null)
|
||||||
{
|
{
|
||||||
_injectController = new PreInitInjectController(pipeline);
|
_injectController = new PreInitInjectController(pipeline);
|
||||||
@ -115,10 +101,8 @@ namespace DCFApixels.DragonECS
|
|||||||
pipeline.GetRunner<IEcsPreInitInjectProcess>().OnPreInitInjectionBefore();
|
pipeline.GetRunner<IEcsPreInitInjectProcess>().OnPreInitInjectionBefore();
|
||||||
injectMapRunner.Inject(_injectController);
|
injectMapRunner.Inject(_injectController);
|
||||||
}
|
}
|
||||||
|
|
||||||
var injectRunnerGeneric = pipeline.GetRunner<IEcsInject<T>>();
|
var injectRunnerGeneric = pipeline.GetRunner<IEcsInject<T>>();
|
||||||
injectRunnerGeneric.Inject(_injectedData);
|
injectRunnerGeneric.Inject(_injectedData);
|
||||||
|
|
||||||
if (_injectController.OnInject())
|
if (_injectController.OnInject())
|
||||||
{
|
{
|
||||||
_injectController.Destroy();
|
_injectController.Destroy();
|
||||||
@ -128,13 +112,11 @@ namespace DCFApixels.DragonECS
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void OnPreInitInjectionBefore() { }
|
public void OnPreInitInjectionBefore() { }
|
||||||
public void OnPreInitInjectionAfter()
|
public void OnPreInitInjectionAfter() => _injectController = null;
|
||||||
{
|
|
||||||
_injectController = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class InjectSystemExstensions
|
public static class InjectSystemExtensions
|
||||||
{
|
{
|
||||||
public static EcsPipeline.Builder Inject<T>(this EcsPipeline.Builder self, T data)
|
public static EcsPipeline.Builder Inject<T>(this EcsPipeline.Builder self, T data)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,14 +1,16 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using DCFApixels.DragonECS.Internal;
|
||||||
|
|
||||||
namespace DCFApixels.DragonECS
|
namespace DCFApixels.DragonECS
|
||||||
|
{
|
||||||
|
namespace Internal
|
||||||
{
|
{
|
||||||
[DebugHide, DebugColor(DebugColor.Black)]
|
[DebugHide, DebugColor(DebugColor.Black)]
|
||||||
public class SystemsBlockMarkerSystem : IEcsSystem
|
public class SystemsBlockMarkerSystem : IEcsSystem
|
||||||
{
|
{
|
||||||
public readonly string name;
|
public readonly string name;
|
||||||
public SystemsBlockMarkerSystem(string name) { this.name = name; }
|
public SystemsBlockMarkerSystem(string name) => this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
[DebugHide, DebugColor(DebugColor.Grey)]
|
[DebugHide, DebugColor(DebugColor.Grey)]
|
||||||
public class DeleteEmptyEntitesSystem : IEcsRunProcess, IEcsPreInject
|
public class DeleteEmptyEntitesSystem : IEcsRunProcess, IEcsPreInject
|
||||||
{
|
{
|
||||||
@ -24,30 +26,26 @@ namespace DCFApixels.DragonECS
|
|||||||
world.DeleteEmptyEntites();
|
world.DeleteEmptyEntites();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[DebugHide, DebugColor(DebugColor.Grey)]
|
[DebugHide, DebugColor(DebugColor.Grey)]
|
||||||
public class DeleteOneFrameComponentSystem<TWorld, TComponent> : IEcsRunProcess, IEcsInject<TWorld>
|
public class DeleteOneFrameComponentSystem<TWorld, TComponent> : IEcsRunProcess, IEcsInject<TWorld>
|
||||||
where TWorld : EcsWorld<TWorld>
|
where TWorld : EcsWorld<TWorld>
|
||||||
where TComponent : struct, IEcsComponent
|
where TComponent : struct, IEcsComponent
|
||||||
{
|
{
|
||||||
private TWorld _world;
|
|
||||||
public void Inject(TWorld obj) => _world = obj;
|
|
||||||
private sealed class Subject : EcsSubject
|
private sealed class Subject : EcsSubject
|
||||||
{
|
{
|
||||||
public EcsPool<TComponent> pool;
|
public EcsPool<TComponent> pool;
|
||||||
public Subject(Builder b)
|
public Subject(Builder b) => pool = b.Include<TComponent>();
|
||||||
{
|
|
||||||
pool = b.Include<TComponent>();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
private TWorld _world;
|
||||||
|
public void Inject(TWorld obj) => _world = obj;
|
||||||
public void Run(EcsPipeline pipeline)
|
public void Run(EcsPipeline pipeline)
|
||||||
{
|
{
|
||||||
foreach (var e in _world.Where(out Subject s))
|
foreach (var e in _world.Where(out Subject s))
|
||||||
s.pool.Del(e);
|
s.pool.Del(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public static class DeleteOneFrameComponentSystemExt
|
public static class DeleteOneFrameComponentSystemExtensions
|
||||||
{
|
{
|
||||||
private const string AUTO_DEL_LAYER = nameof(AUTO_DEL_LAYER);
|
private const string AUTO_DEL_LAYER = nameof(AUTO_DEL_LAYER);
|
||||||
public static EcsPipeline.Builder AutoDel<TWorld, TComponent>(this EcsPipeline.Builder b)
|
public static EcsPipeline.Builder AutoDel<TWorld, TComponent>(this EcsPipeline.Builder b)
|
||||||
|
|||||||
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,5 +1,9 @@
|
|||||||
namespace DCFApixels.DragonECS
|
namespace DCFApixels.DragonECS
|
||||||
{
|
{
|
||||||
|
internal sealed class EcsNullWorld : EcsWorld<EcsNullWorld>
|
||||||
|
{
|
||||||
|
public EcsNullWorld() : base(false) { }
|
||||||
|
}
|
||||||
public sealed class EcsDefaultWorld : EcsWorld<EcsDefaultWorld> { }
|
public sealed class EcsDefaultWorld : EcsWorld<EcsDefaultWorld> { }
|
||||||
public sealed class EcsEventWorld : EcsWorld<EcsEventWorld> { }
|
public sealed class EcsEventWorld : EcsWorld<EcsEventWorld> { }
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,16 +1,10 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
using DCFApixels.DragonECS.Internal;
|
||||||
|
|
||||||
namespace DCFApixels.DragonECS
|
namespace DCFApixels.DragonECS
|
||||||
{
|
{
|
||||||
using Internal;
|
|
||||||
|
|
||||||
internal sealed class EcsNullWorld : EcsWorld<EcsNullWorld>
|
|
||||||
{
|
|
||||||
public EcsNullWorld() : base(false) { }
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract class EcsWorld
|
public abstract class EcsWorld
|
||||||
{
|
{
|
||||||
private const short GEN_BITS = 0x7fff;
|
private const short GEN_BITS = 0x7fff;
|
||||||
@ -19,6 +13,7 @@ namespace DCFApixels.DragonECS
|
|||||||
|
|
||||||
public static EcsWorld[] Worlds = new EcsWorld[8];
|
public static EcsWorld[] Worlds = new EcsWorld[8];
|
||||||
private static IntDispenser _worldIdDispenser = new IntDispenser(0);
|
private static IntDispenser _worldIdDispenser = new IntDispenser(0);
|
||||||
|
|
||||||
public readonly short uniqueID;
|
public readonly short uniqueID;
|
||||||
|
|
||||||
private int _worldTypeID;
|
private int _worldTypeID;
|
||||||
@ -598,9 +593,6 @@ namespace DCFApixels.DragonECS
|
|||||||
void OnNewEntity(int entityID);
|
void OnNewEntity(int entityID);
|
||||||
void OnDelEntity(int entityID);
|
void OnDelEntity(int entityID);
|
||||||
}
|
}
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Extensions
|
|
||||||
internal static class WorldEventListExtensions
|
internal static class WorldEventListExtensions
|
||||||
{
|
{
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user