Add some world-coordinate helpers.
This commit is contained in:
@@ -27,6 +27,9 @@ namespace OpenRA
|
|||||||
public static WRange operator +(WRange a, WRange b) { return new WRange(a.Range + b.Range); }
|
public static WRange operator +(WRange a, WRange b) { return new WRange(a.Range + b.Range); }
|
||||||
public static WRange operator -(WRange a, WRange b) { return new WRange(a.Range - b.Range); }
|
public static WRange operator -(WRange a, WRange b) { return new WRange(a.Range - b.Range); }
|
||||||
public static WRange operator -(WRange a) { return new WRange(-a.Range); }
|
public static WRange operator -(WRange a) { return new WRange(-a.Range); }
|
||||||
|
public static WRange operator /(WRange a, int b) { return new WRange(a.Range / b); }
|
||||||
|
public static WRange operator *(WRange a, int b) { return new WRange(a.Range * b); }
|
||||||
|
public static WRange operator *(int a, WRange b) { return new WRange(a * b.Range); }
|
||||||
|
|
||||||
public static bool operator ==(WRange me, WRange other) { return (me.Range == other.Range); }
|
public static bool operator ==(WRange me, WRange other) { return (me.Range == other.Range); }
|
||||||
public static bool operator !=(WRange me, WRange other) { return !(me == other); }
|
public static bool operator !=(WRange me, WRange other) { return !(me == other); }
|
||||||
|
|||||||
@@ -41,7 +41,9 @@ namespace OpenRA
|
|||||||
public int2 ToInt2() { return new int2(X, Y); }
|
public int2 ToInt2() { return new int2(X, Y); }
|
||||||
public PPos ToPPos() { return new PPos(Game.CellSize * X, Game.CellSize * Y); }
|
public PPos ToPPos() { return new PPos(Game.CellSize * X, Game.CellSize * Y); }
|
||||||
|
|
||||||
public WPos CenterPosition { get { return new WPos(1024*X + 512, 1024*Y + 512, 0); } }
|
public WPos CenterPosition { get { return new WPos(1024 * X + 512, 1024 * Y + 512, 0); } }
|
||||||
|
public WPos TopLeft { get { return new WPos(1024 * X, 1024 * Y, 0); } }
|
||||||
|
public WPos BottomRight { get { return new WPos(1024 * X + 1023, 1024 * Y + 1023, 0); } }
|
||||||
|
|
||||||
public CPos Clamp(Rectangle r)
|
public CPos Clamp(Rectangle r)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -27,6 +27,16 @@ namespace OpenRA
|
|||||||
return FindUnits(world, loc, loc).Where(a => !world.FogObscures(a));
|
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)
|
public static IEnumerable<Actor> FindUnits(this World world, PPos a, PPos b)
|
||||||
{
|
{
|
||||||
var u = PPos.Min(a, b);
|
var u = PPos.Min(a, b);
|
||||||
@@ -34,6 +44,17 @@ namespace OpenRA
|
|||||||
return world.WorldActor.Trait<SpatialBins>().ActorsInBox(u,v);
|
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)
|
public static Actor ClosestTo(this IEnumerable<Actor> actors, PPos px)
|
||||||
{
|
{
|
||||||
return actors.OrderBy( a => (a.CenterLocation - px).LengthSquared ).FirstOrDefault();
|
return actors.OrderBy( a => (a.CenterLocation - px).LengthSquared ).FirstOrDefault();
|
||||||
|
|||||||
Reference in New Issue
Block a user