This repository has been archived on 2023-02-02. You can view files and clone it, but cannot push or open issues or pull requests.
mfgames-gallium-cil/src/Gallium/EnumerableEntityExtensions1.cs
Dylan R. E. Moonfire e9dad5bbe4 feat: initial commit
2021-09-06 23:15:21 -05:00

69 lines
2.1 KiB
C#

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);
}
}
}