diff --git a/src/Gallium/ForEntityExtensions.cs b/src/Gallium/ForEntityExtensions.cs deleted file mode 100644 index a73823d..0000000 --- a/src/Gallium/ForEntityExtensions.cs +++ /dev/null @@ -1,92 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Gallium -{ - public static class ForEntityExtensions - { - /// - /// Selects an entity from the given list, filtering on entities with - /// the given components. If the entity doesn't match, it is passed as it. - /// - /// 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 type of the first component. - /// An enumeration of transformed entities. - public static IEnumerable ForEntity( - this IEnumerable entities, - Func selectWithComponents) - { - return entities.SelectEntity(selectWithComponents, a => a); - } - - /// - /// Selects an entity from the given list, filtering on entities with - /// the given components. If the entity doesn't match, it is passed as it. - /// - /// 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 type of the first component. - /// The type of the second component. - /// An enumeration of transformed entities. - public static IEnumerable ForEntity( - this IEnumerable entities, - Func selectWithComponents) - { - return entities.SelectEntity(selectWithComponents, a => a); - } - - /// - /// Selects an entity from the given list, filtering on entities with - /// the given components. If the entity doesn't match, it is passed as it. - /// - /// 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 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 ForEntity( - this IEnumerable entities, - Func selectWithComponents) - { - return entities.SelectEntity(selectWithComponents, a => a); - } - - /// - /// Selects an entity from the given list, filtering on entities with - /// the given components. If the entity doesn't match, it is passed as it. - /// - /// 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 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 ForEntity( - this IEnumerable entities, - Func selectWithComponents) - { - return entities.SelectEntity( - selectWithComponents, - a => a); - } - } -} diff --git a/src/Gallium/SelectEntityExtensions.cs b/src/Gallium/SelectEntityExtensions.cs index 624181d..f291758 100644 --- a/src/Gallium/SelectEntityExtensions.cs +++ b/src/Gallium/SelectEntityExtensions.cs @@ -11,13 +11,115 @@ namespace Gallium /// /// 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 transformation function for the entity and selected components. If this + /// returns null, then the entity + /// will be filtered out. + /// + /// + /// If true, then entities without all the components are included. Otherwise, they + /// are excluded. + /// + /// The type of the first component. + /// An enumeration of transformed entities. + public static IEnumerable SelectEntity( + this IEnumerable entities, + Func selectWithComponents, + bool includeEntitiesWithoutComponents = true) + { + return entities.SelectEntity(selectWithComponents, includeEntitiesWithoutComponents ? a => a : a => null); + } + + /// + /// 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. + /// + /// + /// If true, then entities without all the components are included. Otherwise, they + /// are excluded. + /// + /// 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, + bool includeEntitiesWithoutComponents = true) + { + return entities.SelectEntity(selectWithComponents, includeEntitiesWithoutComponents ? a => a : a => null); + } + + /// + /// 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. + /// + /// + /// If true, then entities without all the components are included. Otherwise, they + /// are excluded. + /// + /// 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, + bool includeEntitiesWithoutComponents = true) + { + return entities.SelectEntity(selectWithComponents, includeEntitiesWithoutComponents ? a => a : a => null); + } + + /// + /// 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. + /// + /// + /// If true, then entities without all the components are included. Otherwise, they + /// are excluded. + /// + /// The type of the first component. + /// The type of the second component. + /// The type of the third component. + /// The type of the fourth component. + /// An enumeration of transformed entities. + public static IEnumerable SelectEntity( + this IEnumerable entities, + Func selectWithComponents, + bool includeEntitiesWithoutComponents = true) + { + return entities.SelectEntity(selectWithComponents, includeEntitiesWithoutComponents ? a => a : a => null); + } + + /// + /// 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, + /// The optional transformation function for entities that do not have all the + /// components. If returns null, /// then the entity will not be included. /// /// The type of the first component. @@ -25,7 +127,7 @@ namespace Gallium public static IEnumerable SelectEntity( this IEnumerable entities, Func selectWithComponents, - Func? selectWithoutComponents = null) + Func selectWithoutComponents) { foreach (Entity entity in entities) { @@ -46,13 +148,13 @@ namespace Gallium /// /// 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 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, + /// The optional transformation function for entities that do not have all the + /// components. If returns null, /// then the entity will not be included. /// /// The type of the first component. @@ -61,14 +163,13 @@ namespace Gallium public static IEnumerable SelectEntity( this IEnumerable entities, Func selectWithComponents, - Func? selectWithoutComponents = null) + Func selectWithoutComponents) { 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); + Entity? result = entity.TryGet(out T1 value1) && entity.TryGet(out T2 value2) + ? selectWithComponents?.Invoke(entity, value1, value2) + : selectWithoutComponents?.Invoke(entity); if (result != null) { @@ -83,13 +184,13 @@ namespace Gallium /// /// 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 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, + /// The optional transformation function for entities that do not have all the + /// components. If returns null, /// then the entity will not be included. /// /// The type of the first component. @@ -99,19 +200,13 @@ namespace Gallium public static IEnumerable SelectEntity( this IEnumerable entities, Func selectWithComponents, - Func? selectWithoutComponents = null) + Func selectWithoutComponents) { 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) + 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) @@ -127,13 +222,13 @@ namespace Gallium /// /// 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 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, + /// The optional transformation function for entities that do not have all the + /// components. If returns null, /// then the entity will not be included. /// /// The type of the first component. @@ -144,7 +239,7 @@ namespace Gallium public static IEnumerable SelectEntity( this IEnumerable entities, Func selectWithComponents, - Func? selectWithoutComponents = null) + Func selectWithoutComponents) { foreach (Entity entity in entities) { @@ -153,12 +248,7 @@ namespace Gallium && entity.TryGet(out T2 value2) && entity.TryGet(out T3 value3) && entity.TryGet(out T4 value4) - ? selectWithComponents?.Invoke( - entity, - value1, - value2, - value3, - value4) + ? selectWithComponents?.Invoke(entity, value1, value2, value3, value4) : selectWithoutComponents?.Invoke(entity); if (result != null) diff --git a/tests/Gallium.Tests/EnumerableEntityTests.cs b/tests/Gallium.Tests/EnumerableEntityTests.cs index 3795bc4..2d72544 100644 --- a/tests/Gallium.Tests/EnumerableEntityTests.cs +++ b/tests/Gallium.Tests/EnumerableEntityTests.cs @@ -18,9 +18,7 @@ namespace Gallium.Tests Assert.Equal( new[] { "1!", "2", "3!" }, - entities - .ForEntity( - (e, _) => e.Set(e.Get() + "!")) + entities.SelectEntity((e, _) => e.Set(e.Get() + "!")) .Select(x => x.Get()) .ToArray()); } @@ -32,16 +30,12 @@ namespace Gallium.Tests { new Entity().Add("1").Add(new TestComponent1()), new Entity().Add("2").Add(new TestComponent2()), - new Entity().Add("3") - .Add(new TestComponent1()) - .Add(new TestComponent2()), + new Entity().Add("3").Add(new TestComponent1()).Add(new TestComponent2()), }; Assert.Equal( new[] { "1", "2", "3!" }, - entities - .ForEntity( - (e, _, _) => e.Set(e.Get() + "!")) + entities.SelectEntity((e, _, _) => e.Set(e.Get() + "!")) .Select(x => x.Get()) .ToArray()); } @@ -52,9 +46,7 @@ namespace Gallium.Tests Entity[] entities = { new Entity().Add("1").Add(new TestComponent1()), - new Entity().Add("2") - .Add(new TestComponent2()) - .Add(new TestComponent3b()), + new Entity().Add("2").Add(new TestComponent2()).Add(new TestComponent3b()), new Entity().Add("3") .Add(new TestComponent3a()) .Add(new TestComponent1()) @@ -63,10 +55,8 @@ namespace Gallium.Tests Assert.Equal( new[] { "1", "2", "3-TestComponent3a" }, - entities - .ForEntity( - (e, _, _, t) => e.Set( - e.Get() + "-" + t.GetType().Name)) + entities.SelectEntity( + (e, _, _, t) => e.Set(e.Get() + "-" + t.GetType().Name)) .Select(x => x.Get()) .ToArray()); } @@ -83,9 +73,7 @@ namespace Gallium.Tests Assert.Equal( new[] { "1", "3" }, - entities.WhereAllComponents() - .Select(x => x.Get()) - .ToArray()); + entities.WhereAllComponents().Select(x => x.Get()).ToArray()); } [Fact] @@ -94,17 +82,13 @@ namespace Gallium.Tests Entity[] entities = { new Entity().Add("1").Add(new TestComponent1()), - new Entity().Add("2") - .Add(new TestComponent2()) - .Add(new TestComponent1()), + new Entity().Add("2").Add(new TestComponent2()).Add(new TestComponent1()), new Entity().Add("3").Add(new TestComponent1()), }; Assert.Equal( new[] { "2" }, - entities.WhereAllComponents() - .Select(x => x.Get()) - .ToArray()); + entities.WhereAllComponents().Select(x => x.Get()).ToArray()); } [Fact] @@ -116,18 +100,13 @@ namespace Gallium.Tests .Add(new TestComponent1()) .Add(new TestComponent3b()) .Add(new TestComponent2()), - new Entity().Add("2") - .Add(new TestComponent2()) - .Add(new TestComponent1()), - new Entity().Add("3") - .Add(new TestComponent3a()), + new Entity().Add("2").Add(new TestComponent2()).Add(new TestComponent1()), + new Entity().Add("3").Add(new TestComponent3a()), }; Assert.Equal( new[] { "1" }, - entities - .WhereAllComponents() + entities.WhereAllComponents() .Select(x => x.Get()) .ToArray()); } @@ -144,9 +123,7 @@ namespace Gallium.Tests Assert.Equal( new[] { "2" }, - entities.WhereNotComponent() - .Select(x => x.Get()) - .ToArray()); + entities.WhereNotComponent().Select(x => x.Get()).ToArray()); } [Fact] @@ -155,9 +132,7 @@ namespace Gallium.Tests Entity[] entities = { new Entity().Add("1").Add(new TestComponent1()), - new Entity().Add("2") - .Add(new TestComponent2()) - .Add(new TestComponent1()), + new Entity().Add("2").Add(new TestComponent2()).Add(new TestComponent1()), new Entity().Add("3"), }; @@ -178,15 +153,12 @@ namespace Gallium.Tests .Add(new TestComponent1()) .Add(new TestComponent2()) .Add(new TestComponent3b()), - new Entity().Add("3") - .Add(new TestComponent3a()), + new Entity().Add("3").Add(new TestComponent3a()), }; Assert.Equal( new string[] { "1", "3" }, - entities - .WhereNotAllComponents() + entities.WhereNotAllComponents() .Select(x => x.Get()) .ToArray()); }