Add some world-coordinate helpers.

This commit is contained in:
Paul Chote
2013-07-20 13:06:13 +12:00
parent 8751d29a9a
commit 1dfc4837d4
3 changed files with 27 additions and 1 deletions

View File

@@ -27,6 +27,16 @@ namespace OpenRA
return FindUnits(world, loc, loc).Where(a => !world.FogObscures(a));
}
public static IEnumerable<Actor> FindUnits(this World world, CPos tl, CPos br)
{
return world.FindUnits(tl.TopLeft, br.BottomRight);
}
public static IEnumerable<Actor> FindUnits(this World world, WPos tl, WPos br)
{
return world.FindUnits(PPos.FromWPos(tl), PPos.FromWPos(br));
}
public static IEnumerable<Actor> FindUnits(this World world, PPos a, PPos b)
{
var u = PPos.Min(a, b);
@@ -34,6 +44,17 @@ namespace OpenRA
return world.WorldActor.Trait<SpatialBins>().ActorsInBox(u,v);
}
public static Actor ClosestTo(this IEnumerable<Actor> actors, Actor a)
{
var pos = a.CenterPosition;
return actors.OrderBy(b => (b.CenterPosition - pos).LengthSquared).FirstOrDefault();
}
public static Actor ClosestTo(this IEnumerable<Actor> actors, WPos pos)
{
return actors.OrderBy(a => (a.CenterPosition - pos).LengthSquared).FirstOrDefault();
}
public static Actor ClosestTo(this IEnumerable<Actor> actors, PPos px)
{
return actors.OrderBy( a => (a.CenterLocation - px).LengthSquared ).FirstOrDefault();