encompass-cs/encompass-cs/EntitySetQuery.cs

67 lines
2.7 KiB
C#
Raw Normal View History

2019-12-22 01:58:07 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
namespace Encompass
{
2019-12-22 09:15:58 +00:00
/// <summary>
/// EntitySetQuery is used to efficiently obtain a set of Entities that have all required Components and do not have any forbidden Components.
/// </summary>
2019-12-22 01:58:07 +00:00
public struct EntitySetQuery : IEnumerable<Entity>
{
2019-12-22 09:15:58 +00:00
private EntitySetQuery(EntityManager entityManager, ComponentUpdateManager componentUpdateManager, HashSet<Type> readTypes, HashSet<Type> readPendingTypes, ImmutableArray<Type> includes, ImmutableArray<Type> excludes)
2019-12-22 01:58:07 +00:00
{
2019-12-22 09:15:58 +00:00
EntityManager = entityManager;
ComponentUpdateManager = componentUpdateManager;
ReadTypes = readTypes;
ReadPendingTypes = readPendingTypes;
2019-12-22 01:58:07 +00:00
Includes = includes;
Excludes = excludes;
}
2019-12-22 09:15:58 +00:00
internal EntitySetQuery(EntityManager entityManager, ComponentUpdateManager componentUpdateManager, HashSet<Type> readTypes, HashSet<Type> readPendingTypes)
2019-12-22 01:58:07 +00:00
{
2019-12-22 09:15:58 +00:00
EntityManager = entityManager;
ComponentUpdateManager = componentUpdateManager;
ReadTypes = readTypes;
ReadPendingTypes = readPendingTypes;
2019-12-22 01:58:07 +00:00
Includes = ImmutableArray.Create<Type>();
Excludes = ImmutableArray.Create<Type>();
}
2019-12-22 09:15:58 +00:00
private EntityManager EntityManager { get; }
private ComponentUpdateManager ComponentUpdateManager { get; }
private HashSet<Type> ReadTypes { get; }
private HashSet<Type> ReadPendingTypes { get; }
2019-12-22 01:58:07 +00:00
private ImmutableArray<Type> Includes { get; }
private ImmutableArray<Type> Excludes { get; }
2019-12-22 09:15:58 +00:00
/// <summary>
/// Designates that the given component type is required.
/// </summary>
2019-12-22 01:58:07 +00:00
public EntitySetQuery With<TComponent>() where TComponent : struct, IComponent
{
2019-12-22 09:15:58 +00:00
return new EntitySetQuery(EntityManager, ComponentUpdateManager, ReadTypes, ReadPendingTypes, Includes.Add(typeof(TComponent)), Excludes);
2019-12-22 01:58:07 +00:00
}
2019-12-22 09:15:58 +00:00
/// <summary>
/// Designates that the given component type is forbidden.
/// </summary>
2019-12-22 01:58:07 +00:00
public EntitySetQuery Without<TComponent>() where TComponent : struct, IComponent
{
2019-12-22 09:15:58 +00:00
return new EntitySetQuery(EntityManager, ComponentUpdateManager, ReadTypes, ReadPendingTypes, Includes, Excludes.Add(typeof(TComponent)));
2019-12-22 01:58:07 +00:00
}
public IEnumerator<Entity> GetEnumerator()
{
2019-12-22 09:15:58 +00:00
return ComponentUpdateManager.QueryEntities(EntityManager.Entities, ReadTypes, ReadPendingTypes, Includes, Excludes).GetEnumerator();
2019-12-22 01:58:07 +00:00
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}