DragonECS/src/Builtin/Systems.cs

63 lines
2.4 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)]
2023-05-30 01:31:04 +08:00
public class DeleteEmptyEntitesSystem : IEcsRunProcess, IEcsInject<EcsWorld>
{
2023-05-26 03:45:35 +08:00
private 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)
world.DeleteEmptyEntites();
}
}
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-05-26 03:45:35 +08:00
private sealed class Subject : EcsSubject
{
public EcsPool<TComponent> pool;
public Subject(Builder b) => pool = b.Include<TComponent>();
}
2023-05-30 00:13:50 +08:00
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>())
{
foreach (var e in world.Where(out Subject s))
s.pool.Del(e);
}
}
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 00:13:50 +08:00
if(AUTO_DEL_LAYER == layerName)
b.Layers.Insert(EcsConsts.POST_END_LAYER, AUTO_DEL_LAYER);
b.AddUnique(new DeleteOneFrameComponentSystem<TComponent>(), layerName);
return b;
}
}
2023-03-27 20:31:45 +08:00
}