diff --git a/src/Gallium/SelectComponentExtensions.cs b/src/Gallium/SelectComponentExtensions.cs new file mode 100644 index 0000000..a6fbe19 --- /dev/null +++ b/src/Gallium/SelectComponentExtensions.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; + +namespace Gallium; + +/// +/// Extension methods for selecting components from a list. +/// +public static class SelectComponentExtensions +{ + /// + /// Retrieves a component from an entity and return it. If the entity does not have + /// the component, it will be + /// filtered out. + /// + /// The entities to process. + /// The component type being searched. + /// A sequence of T1. + public static IEnumerable SelectComponent(this IEnumerable entities) + { + foreach (Entity entity in entities) + { + if (entity.TryGet(out T1 v1)) + { + yield return v1; + } + } + } + + /// + /// Retrieves a component from an entity and return it. If the entity does not have + /// the component, it will be filtered out. + /// + /// The entities to process. + /// The component type being searched. + /// A sequence of T1. + public static IEnumerable SelectComponent( + IEnumerable entities, + Type t1) + { + foreach (Entity entity in entities) + { + if (entity.Has(t1)) + { + yield return entity.Get(t1); + } + } + } +} diff --git a/src/Gallium/SelectComponentOrDefaultExtensions.cs b/src/Gallium/SelectComponentOrDefaultExtensions.cs new file mode 100644 index 0000000..6e40b42 --- /dev/null +++ b/src/Gallium/SelectComponentOrDefaultExtensions.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; + +namespace Gallium; + +/// +/// Extension methods for selecting components from a list. +/// +public static class SelectComponentOrDefaultExtensions +{ + /// + /// Retrieves a component from an entity and return it. If the entity does not have + /// the component, then null will be returned. + /// + /// The entities to process. + /// The component type being searched. + /// A sequence of T1 or nulls. + public static IEnumerable SelectComponent( + IEnumerable entities, + Type t1) + { + foreach (Entity entity in entities) + { + if (entity.Has(t1)) + { + yield return entity.Get(t1); + } + + yield return null; + } + } + + /// + /// Retrieves a component from an entity and return it. If the entity does not have + /// the component, then the default value will be returned. + /// + /// The entities to process. + /// The component type being searched. + /// A sequence of T1. + public static IEnumerable SelectComponentOrDefault(this IEnumerable entities) + { + foreach (Entity entity in entities) + { + if (entity.TryGet(out T1 v1)) + { + yield return v1; + } + + yield return default; + } + } +}