DragonECS/src/Builtin/Systems.cs

66 lines
2.5 KiB
C#
Raw Normal View History

2023-05-30 01:31:04 +08:00
using DCFApixels.DragonECS.Internal;
using System.Collections.Generic;
namespace DCFApixels.DragonECS
2023-03-27 20:31:45 +08:00
{
2023-05-26 03:45:35 +08:00
namespace Internal
2023-03-27 20:31:45 +08:00
{
2023-05-26 03:45:35 +08:00
[DebugHide, DebugColor(DebugColor.Black)]
2023-05-30 18:27:30 +08:00
public class SystemsLayerMarkerSystem : IEcsProcess
{
2023-05-26 03:45:35 +08:00
public readonly string name;
2023-05-28 05:53:08 +08:00
public SystemsLayerMarkerSystem(string name) => this.name = name;
}
2023-05-26 03:45:35 +08:00
[DebugHide, DebugColor(DebugColor.Grey)]
public class EndFrameSystem : IEcsRunProcess, IEcsInject<EcsWorld>
{
private readonly List<EcsWorld> _worlds = new List<EcsWorld>();
2023-05-30 01:31:04 +08:00
public void Inject(EcsWorld obj) => _worlds.Add(obj);
2023-05-26 03:45:35 +08:00
public void Run(EcsPipeline pipeline)
{
foreach (var world in _worlds)
{
2023-05-26 03:45:35 +08:00
world.DeleteEmptyEntites();
world.ReleaseDelEntityBuffer();
}
}
}
2023-05-26 03:45:35 +08:00
[DebugHide, DebugColor(DebugColor.Grey)]
2023-05-30 01:31:04 +08:00
public class DeleteOneFrameComponentSystem<TComponent> : IEcsRunProcess, IEcsInject<EcsWorld>
2023-05-26 03:45:35 +08:00
where TComponent : struct, IEcsComponent
{
2023-06-22 14:31:13 +08:00
private sealed class Aspect : EcsAspect
2023-05-26 03:45:35 +08:00
{
public EcsPool<TComponent> pool;
2023-06-22 14:31:13 +08:00
public Aspect(Builder b) => pool = b.Include<TComponent>();
2023-05-26 03:45:35 +08:00
}
private readonly List<EcsWorld> _worlds = new List<EcsWorld>();
2023-05-30 01:31:04 +08:00
public void Inject(EcsWorld obj) => _worlds.Add(obj);
2023-05-26 03:45:35 +08:00
public void Run(EcsPipeline pipeline)
{
2023-05-30 00:13:50 +08:00
for (int i = 0, iMax = _worlds.Count; i < iMax; i++)
{
EcsWorld world = _worlds[i];
if (world.IsComponentTypeDeclared<TComponent>())
{
2023-06-22 14:31:13 +08:00
foreach (var e in world.Where(out Aspect a))
a.pool.Del(e);
2023-05-30 00:13:50 +08:00
}
}
2023-05-26 03:45:35 +08:00
}
}
}
2023-05-26 03:45:35 +08:00
public static class DeleteOneFrameComponentSystemExtensions
{
private const string AUTO_DEL_LAYER = nameof(AUTO_DEL_LAYER);
2023-05-30 00:13:50 +08:00
public static EcsPipeline.Builder AutoDel<TComponent>(this EcsPipeline.Builder b, string layerName = AUTO_DEL_LAYER)
where TComponent : struct, IEcsComponent
{
2023-05-30 18:30:10 +08:00
if (AUTO_DEL_LAYER == layerName)
b.Layers.InsertAfter(EcsConsts.POST_END_LAYER, AUTO_DEL_LAYER);
2023-05-30 00:13:50 +08:00
b.AddUnique(new DeleteOneFrameComponentSystem<TComponent>(), layerName);
return b;
}
}
2023-03-27 20:31:45 +08:00
}