DragonECS/src/Builtin/Systems.cs
2023-05-28 05:53:08 +08:00

69 lines
2.6 KiB
C#

using System.Collections.Generic;
using DCFApixels.DragonECS.Internal;
namespace DCFApixels.DragonECS
{
namespace Internal
{
[DebugHide, DebugColor(DebugColor.Black)]
public class SystemsLayerMarkerSystem : IEcsSystem
{
public readonly string name;
public SystemsLayerMarkerSystem(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)
{
if (obj is EcsWorld world)
_worlds.Add(world);
}
public void Run(EcsPipeline pipeline)
{
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 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 DeleteOneFrameComponentSystemExtensions
{
private const string AUTO_DEL_LAYER = nameof(AUTO_DEL_LAYER);
public static EcsPipeline.Builder AutoDel<TWorld, TComponent>(this EcsPipeline.Builder b)
where TWorld : EcsWorld<TWorld>
where TComponent : struct, IEcsComponent
{
b.Layers.Insert(EcsConsts.POST_END_LAYER, AUTO_DEL_LAYER);
b.AddUnique(new DeleteOneFrameComponentSystem<TWorld, TComponent>(), AUTO_DEL_LAYER);
return b;
}
/// <summary>for EcsDefaultWorld</summary>
public static EcsPipeline.Builder AutoDel<TComponent>(this EcsPipeline.Builder b)
where TComponent : struct, IEcsComponent
{
b.Layers.Insert(EcsConsts.POST_END_LAYER, AUTO_DEL_LAYER);
b.AddUnique(new DeleteOneFrameComponentSystem<EcsDefaultWorld, TComponent>(), AUTO_DEL_LAYER);
return b;
}
}
}