MoonTools.ECS/src/FilterBuilder.cs

47 lines
1.3 KiB
C#
Raw Normal View History

2022-04-08 05:52:03 +00:00
using System;
using System.Collections.Generic;
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 HashSet<int> Included;
private HashSet<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 HashSet<int>();
Excluded = new HashSet<int>();
2022-04-08 05:52:03 +00:00
}
2022-03-06 06:12:27 +00:00
private FilterBuilder(FilterStorage filterStorage, TypeIndices componentTypeIndices, HashSet<int> included, HashSet<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
}
}