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

@@ -57,10 +57,10 @@ namespace OpenRA
public class LocationInit : IActorInit<CPos>
{
[FieldFromYamlKey] public readonly int2 value = int2.Zero;
[FieldFromYamlKey] public readonly CPos value = CPos.Zero;
public LocationInit() { }
public LocationInit(CPos init) { value = init.ToInt2(); }
public CPos Value(World world) { return (CPos)value; }
public LocationInit(CPos init) { value = init; }
public CPos Value(World world) { return value; }
}
public class SubCellInit : IActorInit<SubCell>

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