Implement heightmap-aware view -> cell conversion.

This commit is contained in:
Paul Chote
2015-03-27 21:43:48 +00:00
parent c68cb995f4
commit 1e54b19bd3
3 changed files with 87 additions and 5 deletions

View File

@@ -105,9 +105,66 @@ namespace OpenRA.Graphics
public CPos ViewToWorld(int2 view)
{
var world = worldRenderer.Viewport.ViewToWorldPx(view);
var map = worldRenderer.World.Map;
var ts = Game.ModData.Manifest.TileSize;
var candidates = CandidateMouseoverCells(world);
var tileSet = worldRenderer.World.TileSet;
foreach (var uv in candidates)
{
// Coarse filter to nearby cells
var p = map.CenterOfCell(uv.ToCPos(map.TileShape));
var s = worldRenderer.ScreenPxPosition(p);
if (Math.Abs(s.X - world.X) <= ts.Width && Math.Abs(s.Y - world.Y) <= ts.Height)
{
var tile = map.MapTiles.Value[uv];
var ti = tileSet.GetTileInfo(tile);
var ramp = ti != null ? ti.RampType : 0;
var corners = map.CellCorners[ramp];
var pos = map.CenterOfCell(uv.ToCPos(map));
var screen = corners.Select(c => worldRenderer.ScreenPxPosition(pos + c)).ToArray();
if (screen.PolygonContains(world))
return uv.ToCPos(map);
}
}
// Mouse is not directly over a cell (perhaps on a cliff)
// Try and find the closest cell
if (candidates.Any())
{
return candidates.OrderBy(uv =>
{
var p = map.CenterOfCell(uv.ToCPos(map.TileShape));
var s = worldRenderer.ScreenPxPosition(p);
var dx = Math.Abs(s.X - world.X);
var dy = Math.Abs(s.Y - world.Y);
return dx * dx + dy * dy;
}).First().ToCPos(map);
}
// Something is very wrong, but lets return something that isn't completely bogus and hope the caller can recover
return worldRenderer.World.Map.CellContaining(worldRenderer.Position(ViewToWorldPx(view)));
}
/// <summary> Returns an unfiltered list of all cells that could potentially contain the mouse cursor</summary>
IEnumerable<MPos> CandidateMouseoverCells(int2 world)
{
var map = worldRenderer.World.Map;
var minPos = worldRenderer.Position(world);
// Find all the cells that could potentially have been clicked
var a = map.CellContaining(minPos - new WVec(1024, 0, 0)).ToMPos(map.TileShape);
var b = map.CellContaining(minPos + new WVec(512, 512 * maxGroundHeight, 0)).ToMPos(map.TileShape);
for (var v = b.V; v >= a.V; v--)
for (var u = b.U; u >= a.U; u--)
yield return new MPos(u, v);
}
public int2 ViewToWorldPx(int2 view) { return (1f / Zoom * view.ToFloat2()).ToInt2() + TopLeft; }
public int2 WorldToViewPx(int2 world) { return (Zoom * (world - TopLeft).ToFloat2()).ToInt2(); }