feat: refactoring for more consistent naming

This commit is contained in:
Dylan R. E. Moonfire 2022-07-06 01:34:51 -05:00
parent 4e91e64ed8
commit 034b6c0fa7
11 changed files with 567 additions and 268 deletions

View file

@ -11,21 +11,15 @@ namespace Gallium.Tests
{ {
Entity[] entities = Entity[] entities =
{ {
new Entity() new Entity().Add("1").Add(new TestComponent1()),
.Add("1") new Entity().Add("2").Add(new TestComponent2()),
.Add(new TestComponent1()), new Entity().Add("3").Add(new TestComponent1()),
new Entity()
.Add("2")
.Add(new TestComponent2()),
new Entity()
.Add("3")
.Add(new TestComponent1()),
}; };
Assert.Equal( Assert.Equal(
new[] { "1!", "2", "3!" }, new[] { "1!", "2", "3!" },
entities entities
.ForEachEntity<TestComponent1>( .ForEntity<TestComponent1>(
(e, _) => e.Set(e.Get<string>() + "!")) (e, _) => e.Set(e.Get<string>() + "!"))
.Select(x => x.Get<string>()) .Select(x => x.Get<string>())
.ToArray()); .ToArray());
@ -36,14 +30,9 @@ namespace Gallium.Tests
{ {
Entity[] entities = Entity[] entities =
{ {
new Entity() new Entity().Add("1").Add(new TestComponent1()),
.Add("1") new Entity().Add("2").Add(new TestComponent2()),
.Add(new TestComponent1()), new Entity().Add("3")
new Entity()
.Add("2")
.Add(new TestComponent2()),
new Entity()
.Add("3")
.Add(new TestComponent1()) .Add(new TestComponent1())
.Add(new TestComponent2()), .Add(new TestComponent2()),
}; };
@ -51,7 +40,7 @@ namespace Gallium.Tests
Assert.Equal( Assert.Equal(
new[] { "1", "2", "3!" }, new[] { "1", "2", "3!" },
entities entities
.ForEachEntity<TestComponent1, TestComponent2>( .ForEntity<TestComponent1, TestComponent2>(
(e, _, _) => e.Set(e.Get<string>() + "!")) (e, _, _) => e.Set(e.Get<string>() + "!"))
.Select(x => x.Get<string>()) .Select(x => x.Get<string>())
.ToArray()); .ToArray());
@ -62,15 +51,11 @@ namespace Gallium.Tests
{ {
Entity[] entities = Entity[] entities =
{ {
new Entity() new Entity().Add("1").Add(new TestComponent1()),
.Add("1") new Entity().Add("2")
.Add(new TestComponent1()),
new Entity()
.Add("2")
.Add(new TestComponent2()) .Add(new TestComponent2())
.Add<ITestComponent3>(new TestComponent3b()), .Add<ITestComponent3>(new TestComponent3b()),
new Entity() new Entity().Add("3")
.Add("3")
.Add<ITestComponent3>(new TestComponent3a()) .Add<ITestComponent3>(new TestComponent3a())
.Add(new TestComponent1()) .Add(new TestComponent1())
.Add(new TestComponent2()), .Add(new TestComponent2()),
@ -79,8 +64,7 @@ namespace Gallium.Tests
Assert.Equal( Assert.Equal(
new[] { "1", "2", "3-TestComponent3a" }, new[] { "1", "2", "3-TestComponent3a" },
entities entities
.ForEachEntity<TestComponent1, TestComponent2, .ForEntity<TestComponent1, TestComponent2, ITestComponent3>(
ITestComponent3>(
(e, _, _, t) => e.Set( (e, _, _, t) => e.Set(
e.Get<string>() + "-" + t.GetType().Name)) e.Get<string>() + "-" + t.GetType().Name))
.Select(x => x.Get<string>()) .Select(x => x.Get<string>())
@ -92,21 +76,14 @@ namespace Gallium.Tests
{ {
Entity[] entities = Entity[] entities =
{ {
new Entity() new Entity().Add("1").Add(new TestComponent1()),
.Add("1") new Entity().Add("2").Add(new TestComponent2()),
.Add(new TestComponent1()), new Entity().Add("3").Add(new TestComponent1()),
new Entity()
.Add("2")
.Add(new TestComponent2()),
new Entity()
.Add("3")
.Add(new TestComponent1()),
}; };
Assert.Equal( Assert.Equal(
new[] { "1", "3" }, new[] { "1", "3" },
entities entities.WhereAllComponents<TestComponent1>()
.HasComponents<TestComponent1>()
.Select(x => x.Get<string>()) .Select(x => x.Get<string>())
.ToArray()); .ToArray());
} }
@ -116,22 +93,16 @@ namespace Gallium.Tests
{ {
Entity[] entities = Entity[] entities =
{ {
new Entity() new Entity().Add("1").Add(new TestComponent1()),
.Add("1") new Entity().Add("2")
.Add(new TestComponent1()),
new Entity()
.Add("2")
.Add(new TestComponent2()) .Add(new TestComponent2())
.Add(new TestComponent1()), .Add(new TestComponent1()),
new Entity() new Entity().Add("3").Add(new TestComponent1()),
.Add("3")
.Add(new TestComponent1()),
}; };
Assert.Equal( Assert.Equal(
new[] { "2" }, new[] { "2" },
entities entities.WhereAllComponents<TestComponent1, TestComponent2>()
.HasComponents<TestComponent1, TestComponent2>()
.Select(x => x.Get<string>()) .Select(x => x.Get<string>())
.ToArray()); .ToArray());
} }
@ -141,24 +112,21 @@ namespace Gallium.Tests
{ {
Entity[] entities = Entity[] entities =
{ {
new Entity() new Entity().Add("1")
.Add("1")
.Add(new TestComponent1()) .Add(new TestComponent1())
.Add<ITestComponent3>(new TestComponent3b()) .Add<ITestComponent3>(new TestComponent3b())
.Add(new TestComponent2()), .Add(new TestComponent2()),
new Entity() new Entity().Add("2")
.Add("2")
.Add(new TestComponent2()) .Add(new TestComponent2())
.Add(new TestComponent1()), .Add(new TestComponent1()),
new Entity() new Entity().Add("3")
.Add("3")
.Add<ITestComponent3>(new TestComponent3a()), .Add<ITestComponent3>(new TestComponent3a()),
}; };
Assert.Equal( Assert.Equal(
new[] { "1" }, new[] { "1" },
entities entities
.HasComponents<TestComponent1, TestComponent2, .WhereAllComponents<TestComponent1, TestComponent2,
ITestComponent3>() ITestComponent3>()
.Select(x => x.Get<string>()) .Select(x => x.Get<string>())
.ToArray()); .ToArray());
@ -169,21 +137,14 @@ namespace Gallium.Tests
{ {
Entity[] entities = Entity[] entities =
{ {
new Entity() new Entity().Add("1").Add(new TestComponent1()),
.Add("1") new Entity().Add("2").Add(new TestComponent2()),
.Add(new TestComponent1()), new Entity().Add("3").Add(new TestComponent1()),
new Entity()
.Add("2")
.Add(new TestComponent2()),
new Entity()
.Add("3")
.Add(new TestComponent1()),
}; };
Assert.Equal( Assert.Equal(
new[] { "2" }, new[] { "2" },
entities entities.WhereNotComponent<TestComponent1>()
.NotComponents<TestComponent1>()
.Select(x => x.Get<string>()) .Select(x => x.Get<string>())
.ToArray()); .ToArray());
} }
@ -193,21 +154,16 @@ namespace Gallium.Tests
{ {
Entity[] entities = Entity[] entities =
{ {
new Entity() new Entity().Add("1").Add(new TestComponent1()),
.Add("1") new Entity().Add("2")
.Add(new TestComponent1()),
new Entity()
.Add("2")
.Add(new TestComponent2()) .Add(new TestComponent2())
.Add(new TestComponent1()), .Add(new TestComponent1()),
new Entity() new Entity().Add("3"),
.Add("3"),
}; };
Assert.Equal( Assert.Equal(
new[] { "1", "3" }, new[] { "1", "3" },
entities entities.WhereNotAllComponents<TestComponent1, TestComponent2>()
.NotComponents<TestComponent1, TestComponent2>()
.Select(x => x.Get<string>()) .Select(x => x.Get<string>())
.ToArray()); .ToArray());
} }
@ -217,23 +173,19 @@ namespace Gallium.Tests
{ {
Entity[] entities = Entity[] entities =
{ {
new Entity() new Entity().Add("1").Add(new TestComponent1()),
.Add("1") new Entity().Add("2")
.Add(new TestComponent1()),
new Entity()
.Add("2")
.Add(new TestComponent1()) .Add(new TestComponent1())
.Add(new TestComponent2()) .Add(new TestComponent2())
.Add<ITestComponent3>(new TestComponent3b()), .Add<ITestComponent3>(new TestComponent3b()),
new Entity() new Entity().Add("3")
.Add("3")
.Add<ITestComponent3>(new TestComponent3a()), .Add<ITestComponent3>(new TestComponent3a()),
}; };
Assert.Equal( Assert.Equal(
new string[] { "1", "3" }, new string[] { "1", "3" },
entities entities
.NotComponents<TestComponent1, TestComponent2, .WhereNotAllComponents<TestComponent1, TestComponent2,
ITestComponent3>() ITestComponent3>()
.Select(x => x.Get<string>()) .Select(x => x.Get<string>())
.ToArray()); .ToArray());

View file

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<RootNamespace>Gallium.Tests</RootNamespace> <RootNamespace>Gallium.Tests</RootNamespace>

View file

@ -57,11 +57,76 @@ namespace Gallium
/// Gets a value indicating whether the entity has a specific type of /// Gets a value indicating whether the entity has a specific type of
/// component registered. /// component registered.
/// </summary> /// </summary>
/// <typeparam name="TType">The component type.</typeparam> /// <typeparam name="T1">The component type.</typeparam>
/// <returns>True if the type exists, otherwise false.</returns> /// <returns>True if the type exists, otherwise false.</returns>
public bool Has<TType>() public bool Has<T1>()
{ {
return this.Has(typeof(TType)); return this.Has(typeof(T1));
}
/// <summary>
/// Gets a value indicating whether the entity has components of the given types
/// registered.
/// </summary>
/// <typeparam name="T1">The first component type.</typeparam>
/// <returns>
/// True if there are components of the given type exists, otherwise
/// false.
/// </returns>
public bool HasAll<T1>()
{
return this.Has<T1>();
}
/// <summary>
/// Gets a value indicating whether the entity has components of the given types
/// registered.
/// </summary>
/// <typeparam name="T1">The first component type.</typeparam>
/// <typeparam name="T2">The second component type.</typeparam>
/// <returns>
/// True if there are components of the given type exists, otherwise
/// false.
/// </returns>
public bool HasAll<T1, T2>()
{
return this.Has<T1>() && this.Has<T2>();
}
/// <summary>
/// Gets a value indicating whether the entity has components of the given types
/// registered.
/// </summary>
/// <typeparam name="T1">The first component type.</typeparam>
/// <typeparam name="T2">The second component type.</typeparam>
/// <typeparam name="T3">The third component type.</typeparam>
/// <returns>
/// True if there are components of the given type exists, otherwise
/// false.
/// </returns>
public bool HasAll<T1, T2, T3>()
{
return this.Has<T1>() && this.Has<T2>() && this.Has<T3>();
}
/// <summary>
/// Gets a value indicating whether the entity has components of the given types
/// registered.
/// </summary>
/// <typeparam name="T1">The first component type.</typeparam>
/// <typeparam name="T2">The second component type.</typeparam>
/// <typeparam name="T3">The third component type.</typeparam>
/// <typeparam name="T4">The third component type.</typeparam>
/// <returns>
/// True if there are components of the given type exists, otherwise
/// false.
/// </returns>
public bool HasAll<T1, T2, T3, T4>()
{
return this.Has<T1>()
&& this.Has<T2>()
&& this.Has<T3>()
&& this.Has<T4>();
} }
/// <summary> /// <summary>
@ -104,7 +169,7 @@ namespace Gallium
/// <summary> /// <summary>
/// Gets the given component type if inside the entity, otherwise the /// Gets the given component type if inside the entity, otherwise the
/// default avlue. /// default value.
/// </summary> /// </summary>
/// <typeparam name="TType">The component type.</typeparam> /// <typeparam name="TType">The component type.</typeparam>
/// <returns>The found component or default (typically null).</returns> /// <returns>The found component or default (typically null).</returns>
@ -145,7 +210,7 @@ namespace Gallium
/// <returns>True if found, otherwise false.</returns> /// <returns>True if found, otherwise false.</returns>
public bool TryGet<T1, T2>(out T1 value1, out T2 value2) public bool TryGet<T1, T2>(out T1 value1, out T2 value2)
{ {
if (this.Has<T1>() && this.Has<T2>()) if (this.HasAll<T1, T2>())
{ {
value1 = this.Get<T1>(); value1 = this.Get<T1>();
value2 = this.Get<T2>(); value2 = this.Get<T2>();
@ -157,6 +222,70 @@ namespace Gallium
return false; return false;
} }
/// <summary>
/// Attempts to get the values, if present. If not, this returns false
/// and the value is undefined. Otherwise, this method returns true
/// and the actual value inside that variable.
/// </summary>
/// <param name="value1">The value if contained in the entity.</param>
/// <param name="value2">The value if contained in the entity.</param>
/// <typeparam name="T1">The first component type.</typeparam>
/// <typeparam name="T2">The second component type.</typeparam>
/// <typeparam name="T3">The third component type.</typeparam>
/// <returns>True if found, otherwise false.</returns>
public bool TryGet<T1, T2, T3>(
out T1 value1,
out T2 value2,
out T3 value3)
{
if (this.HasAll<T1, T2, T3>())
{
value1 = this.Get<T1>();
value2 = this.Get<T2>();
value3 = this.Get<T3>();
return true;
}
value1 = default!;
value2 = default!;
value3 = default!;
return false;
}
/// <summary>
/// Attempts to get the values, if present. If not, this returns false
/// and the value is undefined. Otherwise, this method returns true
/// and the actual value inside that variable.
/// </summary>
/// <param name="value1">The value if contained in the entity.</param>
/// <param name="value2">The value if contained in the entity.</param>
/// <typeparam name="T1">The first component type.</typeparam>
/// <typeparam name="T2">The second component type.</typeparam>
/// <typeparam name="T3">The third component type.</typeparam>
/// <typeparam name="T4">The fourth component type.</typeparam>
/// <returns>True if found, otherwise false.</returns>
public bool TryGet<T1, T2, T3, T4>(
out T1 value1,
out T2 value2,
out T3 value3,
out T4 value4)
{
if (this.HasAll<T1, T2, T3, T4>())
{
value1 = this.Get<T1>();
value2 = this.Get<T2>();
value3 = this.Get<T3>();
value4 = this.Get<T4>();
return true;
}
value1 = default!;
value2 = default!;
value3 = default!;
value4 = default!;
return false;
}
/// <summary> /// <summary>
/// Sets the component in the entity, regardless if there was a /// Sets the component in the entity, regardless if there was a
/// component already registered. /// component already registered.

View file

@ -1,109 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using EE = System.Collections.Generic.IEnumerable<Gallium.Entity>;
namespace Gallium
{
public static class EnumerableEntityExtensions
{
public static EE ForEachEntity<T1, T2>(
this EE entities,
Func<Entity, T1, T2, Entity> lambda)
{
return entities
.Select(
x => !x.Has<T1>() || !x.Has<T2>()
? x
: lambda(x, x.Get<T1>(), x.Get<T2>()));
}
public static EE ForEachEntity<T1, T2, T3>(
this EE entities,
Func<Entity, T1, T2, T3, Entity> lambda)
{
return entities
.Select(
x => !x.Has<T1>() || !x.Has<T2>() || !x.Has<T3>()
? x
: lambda(x, x.Get<T1>(), x.Get<T2>(), x.Get<T3>()));
}
public static EE ForEntities<T1, T2>(
this EE entities,
Func<EE, EE> lambda)
{
List<Entity> list = entities.ToList();
EE has = lambda(list.HasComponents<T1, T2>());
EE hasNot = list.NotComponents<T1, T2>();
return hasNot.Union(has);
}
public static EE ForEntities<T1, T2, T3>(
this EE entities,
Func<EE, EE> lambda)
{
List<Entity> list = entities.ToList();
EE has = lambda(list.HasComponents<T1, T2, T3>());
EE hasNot = list.NotComponents<T1, T2, T3>();
return hasNot.Union(has);
}
public static EE ForEntities<T1, T2, T3, T4>(
this EE entities,
Func<EE, EE> lambda)
{
List<Entity> list = entities.ToList();
EE has = lambda(list.HasComponents<T1, T2, T3, T4>());
EE hasNot = list.NotComponents<T1, T2, T3, T4>();
return hasNot.Union(has);
}
public static EE HasComponents<T1, T2>(this EE entities)
{
return entities
.Where(x => x.Has<T1>() && x.Has<T2>());
}
public static EE HasComponents<T1, T2, T3>(this EE entities)
{
return entities
.Where(x => x.Has<T1>() && x.Has<T2>() && x.Has<T3>());
}
public static EE HasComponents<T1, T2, T3, T4>(this EE entities)
{
return entities
.Where(
x => x.Has<T1>()
&& x.Has<T2>()
&& x.Has<T3>()
&& x.Has<T4>());
}
public static EE NotComponents<T1, T2>(this EE entities)
{
return entities
.Where(x => !x.Has<T1>() || !x.Has<T2>());
}
public static EE NotComponents<T1, T2, T3>(this EE entities)
{
return entities
.Where(x => !x.Has<T1>() || !x.Has<T2>() || !x.Has<T3>());
}
public static EE NotComponents<T1, T2, T3, T4>(this EE entities)
{
return entities
.Where(
x => !x.Has<T1>()
|| !x.Has<T2>()
|| !x.Has<T3>()
|| !x.Has<T4>());
}
}
}

View file

@ -1,68 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using EE = System.Collections.Generic.IEnumerable<Gallium.Entity>;
namespace Gallium
{
public static class EnumerableEntityExtensions1
{
public static EE ForEachEntity<T1>(
this EE entities,
Func<Entity, T1, Entity> lambda)
{
return entities
.Select(x => x.Has<T1>() ? lambda(x, x.Get<T1>()) : x);
}
/// <summary>
/// Runs an inner set of operation for entities that have a specific
/// component.
/// </summary>
/// <param name="entities">The input to scan.</param>
/// <param name="lambda">
/// The list operation for the ones that have those
/// components.
/// </param>
/// <typeparam name="T1">The type of the first component.</typeparam>
/// <returns>
/// A combined list of components, with entities without the component
/// coming first and the ones with the component coming last after being
/// processed by the lambda.
/// </returns>
public static EE ForEntities<T1>(
this EE entities,
Func<EE, EE> lambda)
{
List<Entity> list = entities.ToList();
EE has = lambda(list.HasComponents<T1>());
EE hasNot = list.NotComponents<T1>();
return hasNot.Union(has);
}
public static EE HasComponents<T1>(this EE entities)
{
return entities
.Where(x => x.Has<T1>());
}
public static EE NotComponents<T1>(this EE entities)
{
return entities
.Where(x => !x.Has<T1>());
}
public static EE WhereEntities<T1>(
this EE entities,
Func<Entity, T1, bool> lambda,
bool includeWithoutComponent = true)
{
return entities
.Where(
x => x.Has<T1>()
? lambda(x, x.Get<T1>())
: includeWithoutComponent);
}
}
}

View file

@ -0,0 +1,92 @@
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

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>

View file

@ -0,0 +1,171 @@
using System;
using System.Collections.Generic;
namespace Gallium
{
public static class SelectEntityExtensions
{
/// <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,
/// then the entity will not be included.
/// </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,
Func<Entity, Entity?>? selectWithoutComponents = null)
{
foreach (Entity entity in entities)
{
Entity? result = entity.TryGet(out T1 value1)
? selectWithComponents?.Invoke(entity, value1)
: selectWithoutComponents?.Invoke(entity);
if (result != null)
{
yield return result;
}
}
}
/// <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,
/// then the entity will not be included.
/// </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,
Func<Entity, Entity?>? selectWithoutComponents = null)
{
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);
if (result != null)
{
yield return result;
}
}
}
/// <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,
/// then the entity will not be included.
/// </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,
Func<Entity, Entity?>? selectWithoutComponents = null)
{
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)
: selectWithoutComponents?.Invoke(entity);
if (result != null)
{
yield return result;
}
}
}
/// <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,
/// then the entity will not be included.
/// </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> SelectEntity<T1, T2, T3, T4>(
this IEnumerable<Entity> entities,
Func<Entity, T1, T2, T3, T4, Entity?> selectWithComponents,
Func<Entity, Entity?>? selectWithoutComponents = null)
{
foreach (Entity entity in entities)
{
Entity? result =
entity.TryGet(out T1 value1)
&& entity.TryGet(out T2 value2)
&& entity.TryGet(out T3 value3)
&& entity.TryGet(out T4 value4)
? selectWithComponents?.Invoke(
entity,
value1,
value2,
value3,
value4)
: selectWithoutComponents?.Invoke(entity);
if (result != null)
{
yield return result;
}
}
}
}
}

View file

@ -0,0 +1,34 @@
using System.Collections.Generic;
using System.Linq;
namespace Gallium
{
public static class WhereAllComponentsExtensions
{
public static IEnumerable<Entity> WhereAllComponents<T1>(
this IEnumerable<Entity> entities)
{
return entities.Where(x => x.Has<T1>());
}
public static IEnumerable<Entity> WhereAllComponents<T1, T2>(
this IEnumerable<Entity> entities)
{
return entities.Where(x => x.Has<T1>() && x.Has<T2>());
}
public static IEnumerable<Entity> WhereAllComponents<T1, T2, T3>(
this IEnumerable<Entity> entities)
{
return entities.Where(
x => x.Has<T1>() && x.Has<T2>() && x.Has<T3>());
}
public static IEnumerable<Entity> WhereAllComponents<T1, T2, T3, T4>(
this IEnumerable<Entity> entities)
{
return entities.Where(
x => x.Has<T1>() && x.Has<T2>() && x.Has<T3>() && x.Has<T4>());
}
}
}

View file

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Gallium
{
public static class WhereComponentsExtensions
{
public static IEnumerable<Entity> WhereComponents<T1>(
this IEnumerable<Entity> entities,
Func<Entity, T1, bool> include)
{
return entities.Where(x => x.Has<T1>() && include(x, x.Get<T1>()));
}
public static IEnumerable<Entity> WhereComponents<T1, T2>(
this IEnumerable<Entity> entities,
Func<Entity, T1, T2, bool> include)
{
return entities.Where(
x => x.Has<T1>()
&& x.Has<T2>()
&& include(x, x.Get<T1>(), x.Get<T2>()));
}
public static IEnumerable<Entity> WhereComponents<T1, T2, T3>(
this IEnumerable<Entity> entities,
Func<Entity, T1, T2, T3, bool> include)
{
return entities.Where(
x => x.Has<T1>()
&& x.Has<T2>()
&& x.Has<T3>()
&& include(x, x.Get<T1>(), x.Get<T2>(), x.Get<T3>()));
}
public static IEnumerable<Entity> WhereComponents<T1, T2, T3, T4>(
this IEnumerable<Entity> entities,
Func<Entity, T1, T2, T3, T4, bool> include)
{
return entities.Where(
x => x.Has<T1>()
&& x.Has<T2>()
&& x.Has<T3>()
&& x.Has<T4>()
&& include(
x,
x.Get<T1>(),
x.Get<T2>(),
x.Get<T3>(),
x.Get<T4>()));
}
}
}

View file

@ -0,0 +1,44 @@
using System.Collections.Generic;
using System.Linq;
namespace Gallium
{
public static class WhereNotAllComponentExtensions
{
public static IEnumerable<Entity> WhereNotComponent<T1>(
this IEnumerable<Entity> entities)
{
return entities.Where(x => !x.Has<T1>());
}
public static IEnumerable<Entity> WhereNotAllComponents<T1>(
this IEnumerable<Entity> entities)
{
return entities.Where(x => !x.Has<T1>());
}
public static IEnumerable<Entity> WhereNotAllComponents<T1, T2>(
this IEnumerable<Entity> entities)
{
return entities.Where(x => !x.HasAll<T1, T2>());
}
public static IEnumerable<Entity> WhereNotAllComponents<T1, T2, T3>(
this IEnumerable<Entity> entities)
{
return entities.Where(
x => !x.HasAll<T1, T2, T3>());
}
public static IEnumerable<Entity> WhereNotAllComponents<T1, T2, T3, T4>(
this IEnumerable<Entity> entities)
{
return entities.Where(
x => !x.Has<T1>()
|| !x.Has<T2>()
|| !x.Has<T3>()
|| !x.Has<T4>());
}
}
}