From 4e21b3b6f584eeac946357ed41cffc6553259ca8 Mon Sep 17 00:00:00 2001 From: "Dylan R. E. Moonfire" Date: Sat, 9 Jul 2022 16:59:15 -0500 Subject: [PATCH] feat: introduced SelectComponent and SelectComponentOrDefault --- src/Gallium/SelectComponentExtensions.cs | 49 +++++++++++++++++ .../SelectComponentOrDefaultExtensions.cs | 52 +++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/Gallium/SelectComponentExtensions.cs create mode 100644 src/Gallium/SelectComponentOrDefaultExtensions.cs 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; + } + } +}