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>
/// <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.
/// 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>
/// <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 name="selectWithoutComponents">
/// 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.
/// </param>
/// <typeparam name="T1">The type of the first component.</typeparam>
@ -25,7 +127,7 @@ namespace Gallium
public static IEnumerable<Entity> SelectEntity<T1>(
this IEnumerable<Entity> entities,
Func<Entity, T1, Entity?> selectWithComponents,
Func<Entity, Entity?>? selectWithoutComponents = null)
Func<Entity, Entity?> selectWithoutComponents)
{
foreach (Entity entity in entities)
{
@ -46,13 +148,13 @@ namespace Gallium
/// </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.
/// The transformation function for the entity and selected components. If this
/// returns null, then the entity
/// will be filtered out.
/// </param>
/// <param name="selectWithoutComponents">
/// 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.
/// </param>
/// <typeparam name="T1">The type of the first component.</typeparam>
@ -61,14 +163,13 @@ namespace Gallium
public static IEnumerable<Entity> SelectEntity<T1, T2>(
this IEnumerable<Entity> entities,
Func<Entity, T1, T2, Entity?> selectWithComponents,
Func<Entity, Entity?>? selectWithoutComponents = null)
Func<Entity, Entity?> 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
/// </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.
/// The transformation function for the entity and selected components. If this
/// returns null, then the entity
/// will be filtered out.
/// </param>
/// <param name="selectWithoutComponents">
/// 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.
/// </param>
/// <typeparam name="T1">The type of the first component.</typeparam>
@ -99,19 +200,13 @@ namespace Gallium
public static IEnumerable<Entity> SelectEntity<T1, T2, T3>(
this IEnumerable<Entity> entities,
Func<Entity, T1, T2, T3, Entity?> selectWithComponents,
Func<Entity, Entity?>? selectWithoutComponents = null)
Func<Entity, Entity?> 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
/// </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.
/// The transformation function for the entity and selected components. If this
/// returns null, then the entity
/// will be filtered out.
/// </param>
/// <param name="selectWithoutComponents">
/// 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.
/// </param>
/// <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>(
this IEnumerable<Entity> entities,
Func<Entity, T1, T2, T3, T4, Entity?> selectWithComponents,
Func<Entity, Entity?>? selectWithoutComponents = null)
Func<Entity, Entity?> 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)

View file

@ -18,9 +18,7 @@ namespace Gallium.Tests
Assert.Equal(
new[] { "1!", "2", "3!" },
entities
.ForEntity<TestComponent1>(
(e, _) => e.Set(e.Get<string>() + "!"))
entities.SelectEntity<TestComponent1>((e, _) => e.Set(e.Get<string>() + "!"))
.Select(x => x.Get<string>())
.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<TestComponent1, TestComponent2>(
(e, _, _) => e.Set(e.Get<string>() + "!"))
entities.SelectEntity<TestComponent1, TestComponent2>((e, _, _) => e.Set(e.Get<string>() + "!"))
.Select(x => x.Get<string>())
.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<ITestComponent3>(new TestComponent3b()),
new Entity().Add("2").Add(new TestComponent2()).Add<ITestComponent3>(new TestComponent3b()),
new Entity().Add("3")
.Add<ITestComponent3>(new TestComponent3a())
.Add(new TestComponent1())
@ -63,10 +55,8 @@ namespace Gallium.Tests
Assert.Equal(
new[] { "1", "2", "3-TestComponent3a" },
entities
.ForEntity<TestComponent1, TestComponent2, ITestComponent3>(
(e, _, _, t) => e.Set(
e.Get<string>() + "-" + t.GetType().Name))
entities.SelectEntity<TestComponent1, TestComponent2, ITestComponent3>(
(e, _, _, t) => e.Set(e.Get<string>() + "-" + t.GetType().Name))
.Select(x => x.Get<string>())
.ToArray());
}
@ -83,9 +73,7 @@ namespace Gallium.Tests
Assert.Equal(
new[] { "1", "3" },
entities.WhereAllComponents<TestComponent1>()
.Select(x => x.Get<string>())
.ToArray());
entities.WhereAllComponents<TestComponent1>().Select(x => x.Get<string>()).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<TestComponent1, TestComponent2>()
.Select(x => x.Get<string>())
.ToArray());
entities.WhereAllComponents<TestComponent1, TestComponent2>().Select(x => x.Get<string>()).ToArray());
}
[Fact]
@ -116,18 +100,13 @@ namespace Gallium.Tests
.Add(new TestComponent1())
.Add<ITestComponent3>(new TestComponent3b())
.Add(new TestComponent2()),
new Entity().Add("2")
.Add(new TestComponent2())
.Add(new TestComponent1()),
new Entity().Add("3")
.Add<ITestComponent3>(new TestComponent3a()),
new Entity().Add("2").Add(new TestComponent2()).Add(new TestComponent1()),
new Entity().Add("3").Add<ITestComponent3>(new TestComponent3a()),
};
Assert.Equal(
new[] { "1" },
entities
.WhereAllComponents<TestComponent1, TestComponent2,
ITestComponent3>()
entities.WhereAllComponents<TestComponent1, TestComponent2, ITestComponent3>()
.Select(x => x.Get<string>())
.ToArray());
}
@ -144,9 +123,7 @@ namespace Gallium.Tests
Assert.Equal(
new[] { "2" },
entities.WhereNotComponent<TestComponent1>()
.Select(x => x.Get<string>())
.ToArray());
entities.WhereNotComponent<TestComponent1>().Select(x => x.Get<string>()).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<ITestComponent3>(new TestComponent3b()),
new Entity().Add("3")
.Add<ITestComponent3>(new TestComponent3a()),
new Entity().Add("3").Add<ITestComponent3>(new TestComponent3a()),
};
Assert.Equal(
new string[] { "1", "3" },
entities
.WhereNotAllComponents<TestComponent1, TestComponent2,
ITestComponent3>()
entities.WhereNotAllComponents<TestComponent1, TestComponent2, ITestComponent3>()
.Select(x => x.Get<string>())
.ToArray());
}