Merge pull request #5758 from pchote/cell-world-conversions

Generalize cell/world coordinate conversions.
This commit is contained in:
Matthias Mailänder
2014-06-28 08:27:18 +02:00
100 changed files with 289 additions and 298 deletions

View File

@@ -463,7 +463,25 @@ namespace OpenRA
return dataStream.ToArray();
}
public bool Contains(CPos xy) { return Bounds.Contains(xy.X, xy.Y); }
public bool Contains(CPos cell)
{
return Bounds.Contains(cell.X, cell.Y);
}
public WPos CenterOfCell(CPos c)
{
return new WPos(1024 * c.X + 512, 1024 * c.Y + 512, 0);
}
public CPos CellContaining(WPos pos)
{
return new CPos(pos.X / 1024, pos.Y / 1024);
}
public int FacingBetween(CPos cell, CPos towards, int fallbackfacing)
{
return Traits.Util.GetFacing(CenterOfCell(towards) - CenterOfCell(cell), fallbackfacing);
}
public void Resize(int width, int height) // editor magic.
{
@@ -604,8 +622,8 @@ namespace OpenRA
public WRange DistanceToEdge(WPos pos, WVec dir)
{
var tl = Bounds.TopLeftAsCPos().TopLeft;
var br = Bounds.BottomRightAsCPos().BottomRight;
var tl = CenterOfCell(new CPos(Bounds.Left, Bounds.Top)) - new WVec(512, 512, 0);
var br = CenterOfCell(new CPos(Bounds.Right, Bounds.Bottom)) + new WVec(511, 511, 0);
var x = dir.X == 0 ? int.MaxValue : ((dir.X < 0 ? tl.X : br.X) - pos.X) / dir.X;
var y = dir.Y == 0 ? int.MaxValue : ((dir.Y < 0 ? tl.Y : br.Y) - pos.Y) / dir.Y;
return new WRange(Math.Min(x, y) * dir.Length);