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/EnumerableEntityExtensions.cs
Dylan R. E. Moonfire e9dad5bbe4 feat: initial commit
2021-09-06 23:15:21 -05:00

110 lines
3.2 KiB
C#

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