MoonTools.ECS/src/FilterBuilder.cs

44 lines
973 B
C#
Raw Normal View History

using MoonTools.ECS.Collections;
2022-03-06 06:12:27 +00:00
2022-04-08 05:52:03 +00:00
namespace MoonTools.ECS
2022-03-06 06:12:27 +00:00
{
2022-04-08 05:52:03 +00:00
public struct FilterBuilder
2022-03-06 06:12:27 +00:00
{
World World;
IndexableSet<TypeId> Included;
IndexableSet<TypeId> Excluded;
2022-03-06 06:12:27 +00:00
internal FilterBuilder(World world)
2022-04-08 05:52:03 +00:00
{
World = world;
Included = new IndexableSet<TypeId>();
Excluded = new IndexableSet<TypeId>();
2022-04-08 05:52:03 +00:00
}
2022-03-06 06:12:27 +00:00
private FilterBuilder(World world, IndexableSet<TypeId> included, IndexableSet<TypeId> excluded)
2022-04-08 05:52:03 +00:00
{
World = world;
2022-04-08 05:52:03 +00:00
Included = included;
Excluded = excluded;
}
2022-03-06 06:12:27 +00:00
public FilterBuilder Include<T>() where T : unmanaged
2022-04-08 05:52:03 +00:00
{
Included.Add(World.GetComponentTypeId<T>());
return new FilterBuilder(World, Included, Excluded);
2022-04-08 05:52:03 +00:00
}
2022-03-06 06:12:27 +00:00
public FilterBuilder Exclude<T>() where T : unmanaged
2022-04-08 05:52:03 +00:00
{
Excluded.Add(World.GetComponentTypeId<T>());
return new FilterBuilder(World, Included, Excluded);
2022-04-08 05:52:03 +00:00
}
public Filter Build()
{
var signature = new FilterSignature(Included, Excluded);
return World.GetFilter(signature);
2022-04-08 05:52:03 +00:00
}
2022-03-06 06:12:27 +00:00
}
}