MoonTools.ECS/src/FilterBuilder.cs

42 lines
1.0 KiB
C#
Raw Normal View History

2022-03-06 06:12:27 +00:00
namespace MoonTools.ECS;
public struct FilterBuilder
{
private ComponentDepot ComponentDepot;
private HashSet<Type> Included;
private HashSet<Type> Excluded;
internal FilterBuilder(ComponentDepot componentDepot)
{
ComponentDepot = componentDepot;
Included = new HashSet<Type>();
Excluded = new HashSet<Type>();
}
private FilterBuilder(ComponentDepot componentDepot, HashSet<Type> included, HashSet<Type> excluded)
{
ComponentDepot = componentDepot;
Included = included;
Excluded = excluded;
}
public FilterBuilder Include<TComponent>() where TComponent : struct
{
ComponentDepot.Register<TComponent>();
2022-03-06 06:12:27 +00:00
Included.Add(typeof(TComponent));
return new FilterBuilder(ComponentDepot, Included, Excluded);
}
public FilterBuilder Exclude<TComponent>() where TComponent : struct
{
ComponentDepot.Register<TComponent>();
2022-03-06 06:12:27 +00:00
Excluded.Add(typeof(TComponent));
return new FilterBuilder(ComponentDepot, Included, Excluded);
}
public Filter Build()
{
return ComponentDepot.CreateFilter(Included, Excluded);
}
}