MoonTools.ECS/src/FilterBuilder.cs

46 lines
1.3 KiB
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
{
private TypeIndices ComponentTypeIndices;
private FilterStorage FilterStorage;
private IndexableSet<int> Included;
private IndexableSet<int> Excluded;
2022-03-06 06:12:27 +00:00
internal FilterBuilder(FilterStorage filterStorage, TypeIndices componentTypeIndices)
2022-04-08 05:52:03 +00:00
{
FilterStorage = filterStorage;
ComponentTypeIndices = componentTypeIndices;
Included = new IndexableSet<int>();
Excluded = new IndexableSet<int>();
2022-04-08 05:52:03 +00:00
}
2022-03-06 06:12:27 +00:00
private FilterBuilder(FilterStorage filterStorage, TypeIndices componentTypeIndices, IndexableSet<int> included, IndexableSet<int> excluded)
2022-04-08 05:52:03 +00:00
{
FilterStorage = filterStorage;
ComponentTypeIndices = componentTypeIndices;
2022-04-08 05:52:03 +00:00
Included = included;
Excluded = excluded;
}
2022-03-06 06:12:27 +00:00
public FilterBuilder Include<TComponent>() where TComponent : unmanaged
2022-04-08 05:52:03 +00:00
{
Included.Add(ComponentTypeIndices.GetIndex<TComponent>());
return new FilterBuilder(FilterStorage, ComponentTypeIndices, Included, Excluded);
2022-04-08 05:52:03 +00:00
}
2022-03-06 06:12:27 +00:00
public FilterBuilder Exclude<TComponent>() where TComponent : unmanaged
2022-04-08 05:52:03 +00:00
{
Excluded.Add(ComponentTypeIndices.GetIndex<TComponent>());
return new FilterBuilder(FilterStorage, ComponentTypeIndices, Included, Excluded);
2022-04-08 05:52:03 +00:00
}
public Filter Build()
{
return FilterStorage.CreateFilter(Included, Excluded);
2022-04-08 05:52:03 +00:00
}
2022-03-06 06:12:27 +00:00
}
}