Skip to content

Commit

Permalink
Virtual API a little more efficient.
Browse files Browse the repository at this point in the history
  • Loading branch information
thygrrr committed Oct 31, 2024
1 parent 0004676 commit 796dedd
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions fennecs/reflection/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,21 @@ public static Entity AddVirtual(this Entity entity, object value, Match match =
/// </remarks>
public static T[] GetVirtual<T>(this Entity entity)
{
var components = entity.Components;
var filtered = components.Where(c => c.Type.IsAssignableTo(typeof(T))).Select(c => c.Box.Value).Cast<T>().ToArray();
var filtered = entity.Components
.Where(c => c.Type.IsAssignableTo(typeof(T)))
.Select(c => c.Box.Value).Cast<T>().ToArray();
return filtered;
}


/// <summary>
/// Returns true if the entity has any components that are <see cref="Type.IsAssignableTo"/> to the Type Parameter <c>T</c>.
/// </summary>
/// <remarks>
/// TODO: This call has room for optimization, and may benefit from match expression support.
/// </remarks>
public static bool HasVirtual<T>(this Entity entity) => entity.GetVirtual<T>().Length > 0;
public static bool HasVirtual<T>(this Entity entity)
{
return entity.Components.Any(c => c.Type.IsAssignableTo(typeof(T)));
}
}

0 comments on commit 796dedd

Please sign in to comment.