MoonTools.ECS/src/FilterBuilder.cs

44 lines
973 B
C#
Raw Normal View History

2023-11-21 07:13:04 +00:00
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
{
2023-11-03 19:40:26 +00:00
World World;
2023-11-08 01:46:44 +00:00
IndexableSet<TypeId> Included;
IndexableSet<TypeId> Excluded;
2022-03-06 06:12:27 +00:00
2023-11-03 19:40:26 +00:00
internal FilterBuilder(World world)
2022-04-08 05:52:03 +00:00
{
2023-11-03 19:40:26 +00:00
World = world;
2023-11-08 01:46:44 +00:00
Included = new IndexableSet<TypeId>();
Excluded = new IndexableSet<TypeId>();
2022-04-08 05:52:03 +00:00
}
2022-03-06 06:12:27 +00:00
2023-11-08 01:46:44 +00:00
private FilterBuilder(World world, IndexableSet<TypeId> included, IndexableSet<TypeId> excluded)
2022-04-08 05:52:03 +00:00
{
2023-11-03 19:40:26 +00:00
World = world;
2022-04-08 05:52:03 +00:00
Included = included;
Excluded = excluded;
}
2022-03-06 06:12:27 +00:00
2023-11-03 19:40:26 +00:00
public FilterBuilder Include<T>() where T : unmanaged
2022-04-08 05:52:03 +00:00
{
2023-11-08 01:46:44 +00:00
Included.Add(World.GetComponentTypeId<T>());
2023-11-03 19:40:26 +00:00
return new FilterBuilder(World, Included, Excluded);
2022-04-08 05:52:03 +00:00
}
2022-03-06 06:12:27 +00:00
2023-11-03 19:40:26 +00:00
public FilterBuilder Exclude<T>() where T : unmanaged
2022-04-08 05:52:03 +00:00
{
2023-11-08 01:46:44 +00:00
Excluded.Add(World.GetComponentTypeId<T>());
2023-11-03 19:40:26 +00:00
return new FilterBuilder(World, Included, Excluded);
2022-04-08 05:52:03 +00:00
}
public Filter Build()
{
2023-11-08 01:46:44 +00:00
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
}
}