using System; using System.Collections.Generic; namespace Gallium { public static class SelectEntityExtensions { /// /// Selects an entity from the given list, filtering on entities with /// the given components. /// /// The entities to parse. /// /// The transformation function for the entity /// and selected components. If this returns null, then the entity will be filtered /// out. /// /// /// The optional transformation function for /// entities that do not have all the components. If this is null or returns null, /// then the entity will not be included. /// /// The type of the first component. /// An enumeration of transformed entities. public static IEnumerable SelectEntity( this IEnumerable entities, Func selectWithComponents, Func? selectWithoutComponents = null) { foreach (Entity entity in entities) { Entity? result = entity.TryGet(out T1 value1) ? selectWithComponents?.Invoke(entity, value1) : selectWithoutComponents?.Invoke(entity); if (result != null) { yield return result; } } } /// /// Selects an entity from the given list, filtering on entities with /// the given components. /// /// The entities to parse. /// /// The transformation function for the entity /// and selected components. If this returns null, then the entity will be filtered /// out. /// /// /// The optional transformation function for /// entities that do not have all the components. If this is null or returns null, /// then the entity will not be included. /// /// The type of the first component. /// The type of the second component. /// An enumeration of transformed entities. public static IEnumerable SelectEntity( this IEnumerable entities, Func selectWithComponents, Func? selectWithoutComponents = null) { foreach (Entity entity in entities) { Entity? result = entity.TryGet(out T1 value1) && entity.TryGet(out T2 value2) ? selectWithComponents?.Invoke(entity, value1, value2) : selectWithoutComponents?.Invoke(entity); if (result != null) { yield return result; } } } /// /// Selects an entity from the given list, filtering on entities with /// the given components. /// /// The entities to parse. /// /// The transformation function for the entity /// and selected components. If this returns null, then the entity will be filtered /// out. /// /// /// The optional transformation function for /// entities that do not have all the components. If this is null or returns null, /// then the entity will not be included. /// /// The type of the first component. /// The type of the second component. /// The type of the third component. /// An enumeration of transformed entities. public static IEnumerable SelectEntity( this IEnumerable entities, Func selectWithComponents, Func? selectWithoutComponents = null) { foreach (Entity entity in entities) { Entity? result = entity.TryGet(out T1 value1) && entity.TryGet(out T2 value2) && entity.TryGet(out T3 value3) ? selectWithComponents?.Invoke( entity, value1, value2, value3) : selectWithoutComponents?.Invoke(entity); if (result != null) { yield return result; } } } /// /// Selects an entity from the given list, filtering on entities with /// the given components. /// /// The entities to parse. /// /// The transformation function for the entity /// and selected components. If this returns null, then the entity will be filtered /// out. /// /// /// The optional transformation function for /// entities that do not have all the components. If this is null or returns null, /// then the entity will not be included. /// /// The type of the first component. /// The type of the second component. /// The type of the third component. /// The type of the third component. /// An enumeration of transformed entities. public static IEnumerable SelectEntity( this IEnumerable entities, Func selectWithComponents, Func? selectWithoutComponents = null) { foreach (Entity entity in entities) { Entity? result = entity.TryGet(out T1 value1) && entity.TryGet(out T2 value2) && entity.TryGet(out T3 value3) && entity.TryGet(out T4 value4) ? selectWithComponents?.Invoke( entity, value1, value2, value3, value4) : selectWithoutComponents?.Invoke(entity); if (result != null) { yield return result; } } } } }