feat: refactoring to make the naming easier to understand

This commit is contained in:
Dylan R. E. Moonfire 2022-07-08 00:25:12 -05:00
parent aeb95437ea
commit 2b73a04fdb
3 changed files with 148 additions and 178 deletions

View file

@ -1,92 +0,0 @@
using System;
using System.Collections.Generic;
namespace Gallium
{
public static class ForEntityExtensions
{
/// <summary>
/// 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.
/// </summary>
/// <param name="entities">The entities to parse.</param>
/// <param name="selectWithComponents">
/// The transformation function for the entity
/// and selected components. If this returns null, then the entity will be filtered
/// out.
/// </param>
/// <typeparam name="T1">The type of the first component.</typeparam>
/// <returns>An enumeration of transformed entities.</returns>
public static IEnumerable<Entity> ForEntity<T1>(
this IEnumerable<Entity> entities,
Func<Entity, T1, Entity?> selectWithComponents)
{
return entities.SelectEntity(selectWithComponents, a => a);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="entities">The entities to parse.</param>
/// <param name="selectWithComponents">
/// The transformation function for the entity
/// and selected components. If this returns null, then the entity will be filtered
/// out.
/// </param>
/// <typeparam name="T1">The type of the first component.</typeparam>
/// <typeparam name="T2">The type of the second component.</typeparam>
/// <returns>An enumeration of transformed entities.</returns>
public static IEnumerable<Entity> ForEntity<T1, T2>(
this IEnumerable<Entity> entities,
Func<Entity, T1, T2, Entity?> selectWithComponents)
{
return entities.SelectEntity(selectWithComponents, a => a);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="entities">The entities to parse.</param>
/// <param name="selectWithComponents">
/// The transformation function for the entity
/// and selected components. If this returns null, then the entity will be filtered
/// out.
/// </param>
/// <typeparam name="T1">The type of the first component.</typeparam>
/// <typeparam name="T2">The type of the second component.</typeparam>
/// <typeparam name="T3">The type of the third component.</typeparam>
/// <returns>An enumeration of transformed entities.</returns>
public static IEnumerable<Entity> ForEntity<T1, T2, T3>(
this IEnumerable<Entity> entities,
Func<Entity, T1, T2, T3, Entity?> selectWithComponents)
{
return entities.SelectEntity(selectWithComponents, a => a);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="entities">The entities to parse.</param>
/// <param name="selectWithComponents">
/// The transformation function for the entity
/// and selected components. If this returns null, then the entity will be filtered
/// out.
/// </param>
/// <typeparam name="T1">The type of the first component.</typeparam>
/// <typeparam name="T2">The type of the second component.</typeparam>
/// <typeparam name="T3">The type of the third component.</typeparam>
/// <typeparam name="T4">The type of the third component.</typeparam>
/// <returns>An enumeration of transformed entities.</returns>
public static IEnumerable<Entity> ForEntity<T1, T2, T3, T4>(
this IEnumerable<Entity> entities,
Func<Entity, T1, T2, T3, T4, Entity?> selectWithComponents)
{
return entities.SelectEntity<T1, T2, T3, T4>(
selectWithComponents,
a => a);
}
}
}

View file

@ -11,13 +11,115 @@ namespace Gallium
/// </summary> /// </summary>
/// <param name="entities">The entities to parse.</param> /// <param name="entities">The entities to parse.</param>
/// <param name="selectWithComponents"> /// <param name="selectWithComponents">
/// The transformation function for the entity /// The transformation function for the entity and selected components. If this
/// and selected components. If this returns null, then the entity will be filtered /// returns null, then the entity
/// out. /// will be filtered out.
/// </param>
/// <param name="includeEntitiesWithoutComponents">
/// If true, then entities without all the components are included. Otherwise, they
/// are excluded.
/// </param>
/// <typeparam name="T1">The type of the first component.</typeparam>
/// <returns>An enumeration of transformed entities.</returns>
public static IEnumerable<Entity> SelectEntity<T1>(
this IEnumerable<Entity> entities,
Func<Entity, T1, Entity?> selectWithComponents,
bool includeEntitiesWithoutComponents = true)
{
return entities.SelectEntity(selectWithComponents, includeEntitiesWithoutComponents ? a => a : a => null);
}
/// <summary>
/// Selects an entity from the given list, filtering on entities with
/// the given components.
/// </summary>
/// <param name="entities">The entities to parse.</param>
/// <param name="selectWithComponents">
/// The transformation function for the entity and selected components. If this
/// returns null, then the entity
/// will be filtered out.
/// </param>
/// <param name="includeEntitiesWithoutComponents">
/// If true, then entities without all the components are included. Otherwise, they
/// are excluded.
/// </param>
/// <typeparam name="T1">The type of the first component.</typeparam>
/// <typeparam name="T2">The type of the second component.</typeparam>
/// <returns>An enumeration of transformed entities.</returns>
public static IEnumerable<Entity> SelectEntity<T1, T2>(
this IEnumerable<Entity> entities,
Func<Entity, T1, T2, Entity?> selectWithComponents,
bool includeEntitiesWithoutComponents = true)
{
return entities.SelectEntity(selectWithComponents, includeEntitiesWithoutComponents ? a => a : a => null);
}
/// <summary>
/// Selects an entity from the given list, filtering on entities with
/// the given components.
/// </summary>
/// <param name="entities">The entities to parse.</param>
/// <param name="selectWithComponents">
/// The transformation function for the entity and selected components. If this
/// returns null, then the entity
/// will be filtered out.
/// </param>
/// <param name="includeEntitiesWithoutComponents">
/// If true, then entities without all the components are included. Otherwise, they
/// are excluded.
/// </param>
/// <typeparam name="T1">The type of the first component.</typeparam>
/// <typeparam name="T2">The type of the second component.</typeparam>
/// <typeparam name="T3">The type of the third component.</typeparam>
/// <returns>An enumeration of transformed entities.</returns>
public static IEnumerable<Entity> SelectEntity<T1, T2, T3>(
this IEnumerable<Entity> entities,
Func<Entity, T1, T2, T3, Entity?> selectWithComponents,
bool includeEntitiesWithoutComponents = true)
{
return entities.SelectEntity(selectWithComponents, includeEntitiesWithoutComponents ? a => a : a => null);
}
/// <summary>
/// Selects an entity from the given list, filtering on entities with
/// the given components.
/// </summary>
/// <param name="entities">The entities to parse.</param>
/// <param name="selectWithComponents">
/// The transformation function for the entity and selected components. If this
/// returns null, then the entity
/// will be filtered out.
/// </param>
/// <param name="includeEntitiesWithoutComponents">
/// If true, then entities without all the components are included. Otherwise, they
/// are excluded.
/// </param>
/// <typeparam name="T1">The type of the first component.</typeparam>
/// <typeparam name="T2">The type of the second component.</typeparam>
/// <typeparam name="T3">The type of the third component.</typeparam>
/// <typeparam name="T4">The type of the fourth component.</typeparam>
/// <returns>An enumeration of transformed entities.</returns>
public static IEnumerable<Entity> SelectEntity<T1, T2, T3, T4>(
this IEnumerable<Entity> entities,
Func<Entity, T1, T2, T3, T4, Entity?> selectWithComponents,
bool includeEntitiesWithoutComponents = true)
{
return entities.SelectEntity(selectWithComponents, includeEntitiesWithoutComponents ? a => a : a => null);
}
/// <summary>
/// Selects an entity from the given list, filtering on entities with
/// the given components.
/// </summary>
/// <param name="entities">The entities to parse.</param>
/// <param name="selectWithComponents">
/// The transformation function for the entity and selected components. If this
/// returns null, then the entity
/// will be filtered out.
/// </param> /// </param>
/// <param name="selectWithoutComponents"> /// <param name="selectWithoutComponents">
/// The optional transformation function for /// The optional transformation function for entities that do not have all the
/// entities that do not have all the components. If this is null or returns null, /// components. If returns null,
/// then the entity will not be included. /// then the entity will not be included.
/// </param> /// </param>
/// <typeparam name="T1">The type of the first component.</typeparam> /// <typeparam name="T1">The type of the first component.</typeparam>
@ -25,7 +127,7 @@ namespace Gallium
public static IEnumerable<Entity> SelectEntity<T1>( public static IEnumerable<Entity> SelectEntity<T1>(
this IEnumerable<Entity> entities, this IEnumerable<Entity> entities,
Func<Entity, T1, Entity?> selectWithComponents, Func<Entity, T1, Entity?> selectWithComponents,
Func<Entity, Entity?>? selectWithoutComponents = null) Func<Entity, Entity?> selectWithoutComponents)
{ {
foreach (Entity entity in entities) foreach (Entity entity in entities)
{ {
@ -46,13 +148,13 @@ namespace Gallium
/// </summary> /// </summary>
/// <param name="entities">The entities to parse.</param> /// <param name="entities">The entities to parse.</param>
/// <param name="selectWithComponents"> /// <param name="selectWithComponents">
/// The transformation function for the entity /// The transformation function for the entity and selected components. If this
/// and selected components. If this returns null, then the entity will be filtered /// returns null, then the entity
/// out. /// will be filtered out.
/// </param> /// </param>
/// <param name="selectWithoutComponents"> /// <param name="selectWithoutComponents">
/// The optional transformation function for /// The optional transformation function for entities that do not have all the
/// entities that do not have all the components. If this is null or returns null, /// components. If returns null,
/// then the entity will not be included. /// then the entity will not be included.
/// </param> /// </param>
/// <typeparam name="T1">The type of the first component.</typeparam> /// <typeparam name="T1">The type of the first component.</typeparam>
@ -61,14 +163,13 @@ namespace Gallium
public static IEnumerable<Entity> SelectEntity<T1, T2>( public static IEnumerable<Entity> SelectEntity<T1, T2>(
this IEnumerable<Entity> entities, this IEnumerable<Entity> entities,
Func<Entity, T1, T2, Entity?> selectWithComponents, Func<Entity, T1, T2, Entity?> selectWithComponents,
Func<Entity, Entity?>? selectWithoutComponents = null) Func<Entity, Entity?> selectWithoutComponents)
{ {
foreach (Entity entity in entities) foreach (Entity entity in entities)
{ {
Entity? result = Entity? result = entity.TryGet(out T1 value1) && entity.TryGet(out T2 value2)
entity.TryGet(out T1 value1) && entity.TryGet(out T2 value2) ? selectWithComponents?.Invoke(entity, value1, value2)
? selectWithComponents?.Invoke(entity, value1, value2) : selectWithoutComponents?.Invoke(entity);
: selectWithoutComponents?.Invoke(entity);
if (result != null) if (result != null)
{ {
@ -83,13 +184,13 @@ namespace Gallium
/// </summary> /// </summary>
/// <param name="entities">The entities to parse.</param> /// <param name="entities">The entities to parse.</param>
/// <param name="selectWithComponents"> /// <param name="selectWithComponents">
/// The transformation function for the entity /// The transformation function for the entity and selected components. If this
/// and selected components. If this returns null, then the entity will be filtered /// returns null, then the entity
/// out. /// will be filtered out.
/// </param> /// </param>
/// <param name="selectWithoutComponents"> /// <param name="selectWithoutComponents">
/// The optional transformation function for /// The optional transformation function for entities that do not have all the
/// entities that do not have all the components. If this is null or returns null, /// components. If returns null,
/// then the entity will not be included. /// then the entity will not be included.
/// </param> /// </param>
/// <typeparam name="T1">The type of the first component.</typeparam> /// <typeparam name="T1">The type of the first component.</typeparam>
@ -99,19 +200,13 @@ namespace Gallium
public static IEnumerable<Entity> SelectEntity<T1, T2, T3>( public static IEnumerable<Entity> SelectEntity<T1, T2, T3>(
this IEnumerable<Entity> entities, this IEnumerable<Entity> entities,
Func<Entity, T1, T2, T3, Entity?> selectWithComponents, Func<Entity, T1, T2, T3, Entity?> selectWithComponents,
Func<Entity, Entity?>? selectWithoutComponents = null) Func<Entity, Entity?> selectWithoutComponents)
{ {
foreach (Entity entity in entities) foreach (Entity entity in entities)
{ {
Entity? result = Entity? result =
entity.TryGet(out T1 value1) entity.TryGet(out T1 value1) && entity.TryGet(out T2 value2) && entity.TryGet(out T3 value3)
&& entity.TryGet(out T2 value2) ? selectWithComponents?.Invoke(entity, value1, value2, value3)
&& entity.TryGet(out T3 value3)
? selectWithComponents?.Invoke(
entity,
value1,
value2,
value3)
: selectWithoutComponents?.Invoke(entity); : selectWithoutComponents?.Invoke(entity);
if (result != null) if (result != null)
@ -127,13 +222,13 @@ namespace Gallium
/// </summary> /// </summary>
/// <param name="entities">The entities to parse.</param> /// <param name="entities">The entities to parse.</param>
/// <param name="selectWithComponents"> /// <param name="selectWithComponents">
/// The transformation function for the entity /// The transformation function for the entity and selected components. If this
/// and selected components. If this returns null, then the entity will be filtered /// returns null, then the entity
/// out. /// will be filtered out.
/// </param> /// </param>
/// <param name="selectWithoutComponents"> /// <param name="selectWithoutComponents">
/// The optional transformation function for /// The optional transformation function for entities that do not have all the
/// entities that do not have all the components. If this is null or returns null, /// components. If returns null,
/// then the entity will not be included. /// then the entity will not be included.
/// </param> /// </param>
/// <typeparam name="T1">The type of the first component.</typeparam> /// <typeparam name="T1">The type of the first component.</typeparam>
@ -144,7 +239,7 @@ namespace Gallium
public static IEnumerable<Entity> SelectEntity<T1, T2, T3, T4>( public static IEnumerable<Entity> SelectEntity<T1, T2, T3, T4>(
this IEnumerable<Entity> entities, this IEnumerable<Entity> entities,
Func<Entity, T1, T2, T3, T4, Entity?> selectWithComponents, Func<Entity, T1, T2, T3, T4, Entity?> selectWithComponents,
Func<Entity, Entity?>? selectWithoutComponents = null) Func<Entity, Entity?> selectWithoutComponents)
{ {
foreach (Entity entity in entities) foreach (Entity entity in entities)
{ {
@ -153,12 +248,7 @@ namespace Gallium
&& entity.TryGet(out T2 value2) && entity.TryGet(out T2 value2)
&& entity.TryGet(out T3 value3) && entity.TryGet(out T3 value3)
&& entity.TryGet(out T4 value4) && entity.TryGet(out T4 value4)
? selectWithComponents?.Invoke( ? selectWithComponents?.Invoke(entity, value1, value2, value3, value4)
entity,
value1,
value2,
value3,
value4)
: selectWithoutComponents?.Invoke(entity); : selectWithoutComponents?.Invoke(entity);
if (result != null) if (result != null)

View file

@ -18,9 +18,7 @@ namespace Gallium.Tests
Assert.Equal( Assert.Equal(
new[] { "1!", "2", "3!" }, new[] { "1!", "2", "3!" },
entities entities.SelectEntity<TestComponent1>((e, _) => e.Set(e.Get<string>() + "!"))
.ForEntity<TestComponent1>(
(e, _) => e.Set(e.Get<string>() + "!"))
.Select(x => x.Get<string>()) .Select(x => x.Get<string>())
.ToArray()); .ToArray());
} }
@ -32,16 +30,12 @@ namespace Gallium.Tests
{ {
new Entity().Add("1").Add(new TestComponent1()), new Entity().Add("1").Add(new TestComponent1()),
new Entity().Add("2").Add(new TestComponent2()), new Entity().Add("2").Add(new TestComponent2()),
new Entity().Add("3") new Entity().Add("3").Add(new TestComponent1()).Add(new TestComponent2()),
.Add(new TestComponent1())
.Add(new TestComponent2()),
}; };
Assert.Equal( Assert.Equal(
new[] { "1", "2", "3!" }, new[] { "1", "2", "3!" },
entities entities.SelectEntity<TestComponent1, TestComponent2>((e, _, _) => e.Set(e.Get<string>() + "!"))
.ForEntity<TestComponent1, TestComponent2>(
(e, _, _) => e.Set(e.Get<string>() + "!"))
.Select(x => x.Get<string>()) .Select(x => x.Get<string>())
.ToArray()); .ToArray());
} }
@ -52,9 +46,7 @@ namespace Gallium.Tests
Entity[] entities = Entity[] entities =
{ {
new Entity().Add("1").Add(new TestComponent1()), new Entity().Add("1").Add(new TestComponent1()),
new Entity().Add("2") new Entity().Add("2").Add(new TestComponent2()).Add<ITestComponent3>(new TestComponent3b()),
.Add(new TestComponent2())
.Add<ITestComponent3>(new TestComponent3b()),
new Entity().Add("3") new Entity().Add("3")
.Add<ITestComponent3>(new TestComponent3a()) .Add<ITestComponent3>(new TestComponent3a())
.Add(new TestComponent1()) .Add(new TestComponent1())
@ -63,10 +55,8 @@ namespace Gallium.Tests
Assert.Equal( Assert.Equal(
new[] { "1", "2", "3-TestComponent3a" }, new[] { "1", "2", "3-TestComponent3a" },
entities entities.SelectEntity<TestComponent1, TestComponent2, ITestComponent3>(
.ForEntity<TestComponent1, TestComponent2, ITestComponent3>( (e, _, _, t) => e.Set(e.Get<string>() + "-" + t.GetType().Name))
(e, _, _, t) => e.Set(
e.Get<string>() + "-" + t.GetType().Name))
.Select(x => x.Get<string>()) .Select(x => x.Get<string>())
.ToArray()); .ToArray());
} }
@ -83,9 +73,7 @@ namespace Gallium.Tests
Assert.Equal( Assert.Equal(
new[] { "1", "3" }, new[] { "1", "3" },
entities.WhereAllComponents<TestComponent1>() entities.WhereAllComponents<TestComponent1>().Select(x => x.Get<string>()).ToArray());
.Select(x => x.Get<string>())
.ToArray());
} }
[Fact] [Fact]
@ -94,17 +82,13 @@ namespace Gallium.Tests
Entity[] entities = Entity[] entities =
{ {
new Entity().Add("1").Add(new TestComponent1()), new Entity().Add("1").Add(new TestComponent1()),
new Entity().Add("2") new Entity().Add("2").Add(new TestComponent2()).Add(new TestComponent1()),
.Add(new TestComponent2())
.Add(new TestComponent1()),
new Entity().Add("3").Add(new TestComponent1()), new Entity().Add("3").Add(new TestComponent1()),
}; };
Assert.Equal( Assert.Equal(
new[] { "2" }, new[] { "2" },
entities.WhereAllComponents<TestComponent1, TestComponent2>() entities.WhereAllComponents<TestComponent1, TestComponent2>().Select(x => x.Get<string>()).ToArray());
.Select(x => x.Get<string>())
.ToArray());
} }
[Fact] [Fact]
@ -116,18 +100,13 @@ namespace Gallium.Tests
.Add(new TestComponent1()) .Add(new TestComponent1())
.Add<ITestComponent3>(new TestComponent3b()) .Add<ITestComponent3>(new TestComponent3b())
.Add(new TestComponent2()), .Add(new TestComponent2()),
new Entity().Add("2") new Entity().Add("2").Add(new TestComponent2()).Add(new TestComponent1()),
.Add(new TestComponent2()) new Entity().Add("3").Add<ITestComponent3>(new TestComponent3a()),
.Add(new TestComponent1()),
new Entity().Add("3")
.Add<ITestComponent3>(new TestComponent3a()),
}; };
Assert.Equal( Assert.Equal(
new[] { "1" }, new[] { "1" },
entities entities.WhereAllComponents<TestComponent1, TestComponent2, ITestComponent3>()
.WhereAllComponents<TestComponent1, TestComponent2,
ITestComponent3>()
.Select(x => x.Get<string>()) .Select(x => x.Get<string>())
.ToArray()); .ToArray());
} }
@ -144,9 +123,7 @@ namespace Gallium.Tests
Assert.Equal( Assert.Equal(
new[] { "2" }, new[] { "2" },
entities.WhereNotComponent<TestComponent1>() entities.WhereNotComponent<TestComponent1>().Select(x => x.Get<string>()).ToArray());
.Select(x => x.Get<string>())
.ToArray());
} }
[Fact] [Fact]
@ -155,9 +132,7 @@ namespace Gallium.Tests
Entity[] entities = Entity[] entities =
{ {
new Entity().Add("1").Add(new TestComponent1()), new Entity().Add("1").Add(new TestComponent1()),
new Entity().Add("2") new Entity().Add("2").Add(new TestComponent2()).Add(new TestComponent1()),
.Add(new TestComponent2())
.Add(new TestComponent1()),
new Entity().Add("3"), new Entity().Add("3"),
}; };
@ -178,15 +153,12 @@ namespace Gallium.Tests
.Add(new TestComponent1()) .Add(new TestComponent1())
.Add(new TestComponent2()) .Add(new TestComponent2())
.Add<ITestComponent3>(new TestComponent3b()), .Add<ITestComponent3>(new TestComponent3b()),
new Entity().Add("3") new Entity().Add("3").Add<ITestComponent3>(new TestComponent3a()),
.Add<ITestComponent3>(new TestComponent3a()),
}; };
Assert.Equal( Assert.Equal(
new string[] { "1", "3" }, new string[] { "1", "3" },
entities entities.WhereNotAllComponents<TestComponent1, TestComponent2, ITestComponent3>()
.WhereNotAllComponents<TestComponent1, TestComponent2,
ITestComponent3>()
.Select(x => x.Get<string>()) .Select(x => x.Get<string>())
.ToArray()); .ToArray());
} }