Files
OpenRA/OpenRA.Game/MPos.cs
Paul Chote e8794032e0 Introduce initial PPos plumbing.
PPos is best thought of as a cell grid applied in
screen space.  Multiple cells with different
terrain heights may be projected to the same PPos,
or to multiple PPos if they do not align with the
screen grid.

PPos coordinates are used primarily for map edge
checks and shroud / visibility queries.
2015-07-27 19:34:49 +01:00

94 lines
2.9 KiB
C#

#region Copyright & License Information
/*
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Drawing;
namespace OpenRA
{
public struct MPos : IEquatable<MPos>
{
public readonly int U, V;
public MPos(int u, int v) { U = u; V = v; }
public static readonly MPos Zero = new MPos(0, 0);
public static bool operator ==(MPos me, MPos other) { return me.U == other.U && me.V == other.V; }
public static bool operator !=(MPos me, MPos other) { return !(me == other); }
public override int GetHashCode() { return U.GetHashCode() ^ V.GetHashCode(); }
public bool Equals(MPos other) { return other == this; }
public override bool Equals(object obj) { return obj is MPos && Equals((MPos)obj); }
public MPos Clamp(Rectangle r)
{
return new MPos(Math.Min(r.Right, Math.Max(U, r.Left)),
Math.Min(r.Bottom, Math.Max(V, r.Top)));
}
public override string ToString() { return U + "," + V; }
public CPos ToCPos(Map map)
{
return ToCPos(map.TileShape);
}
public CPos ToCPos(TileShape shape)
{
if (shape == TileShape.Rectangle)
return new CPos(U, V);
// Convert from rectangular map position to diamond cell position
// - The staggered rows make this fiddly (hint: draw a diagram!)
// (a) Consider the relationships:
// - +1u (even -> odd) adds (1, -1) to (x, y)
// - +1v (even -> odd) adds (1, 0) to (x, y)
// - +1v (odd -> even) adds (0, 1) to (x, y)
// (b) Therefore:
// - au + 2bv adds (a + b) to (x, y)
// - a correction factor is added if v is odd
var offset = (V & 1) == 1 ? 1 : 0;
var y = (V - offset) / 2 - U;
var x = V - y;
return new CPos(x, y);
}
}
/// <summary>
/// Projected map position
/// </summary>
public struct PPos : IEquatable<PPos>
{
public readonly int U, V;
public PPos(int u, int v) { U = u; V = v; }
public static readonly PPos Zero = new PPos(0, 0);
public static bool operator ==(PPos me, PPos other) { return me.U == other.U && me.V == other.V; }
public static bool operator !=(PPos me, PPos other) { return !(me == other); }
public static explicit operator MPos(PPos puv) { return new MPos(puv.U, puv.V); }
public static explicit operator PPos(MPos uv) { return new PPos(uv.U, uv.V); }
public PPos Clamp(Rectangle r)
{
return new PPos(Math.Min(r.Right, Math.Max(U, r.Left)),
Math.Min(r.Bottom, Math.Max(V, r.Top)));
}
public override int GetHashCode() { return U.GetHashCode() ^ V.GetHashCode(); }
public bool Equals(PPos other) { return other == this; }
public override bool Equals(object obj) { return obj is PPos && Equals((PPos)obj); }
public override string ToString() { return U + "," + V; }
}
}