Introduce a new type for representing map coordinates.

To resolve the ambiguity introduced when the introduction of isometric maps meant that cell and map coordinates were no longer equivalent, a new type has been introduced so they can each be represented separately.
This commit is contained in:
RoosterDragon
2014-12-22 19:07:20 +00:00
parent 40c9d0a47d
commit 7cfece6dc0
21 changed files with 257 additions and 232 deletions

View File

@@ -44,7 +44,7 @@ namespace OpenRA.Graphics
{
var mapX = x + b.Left;
var mapY = y + b.Top;
var type = tileset[tileset.GetTerrainIndex(mapTiles[mapX, mapY])];
var type = tileset[tileset.GetTerrainIndex(mapTiles[new MPos(mapX, mapY)])];
colors[y * stride + x] = type.Color.ToArgb();
}
}
@@ -74,11 +74,11 @@ namespace OpenRA.Graphics
{
var mapX = x + b.Left;
var mapY = y + b.Top;
if (map.MapResources.Value[mapX, mapY].Type == 0)
if (map.MapResources.Value[new MPos(mapX, mapY)].Type == 0)
continue;
var res = resourceRules.Actors["world"].Traits.WithInterface<ResourceTypeInfo>()
.Where(t => t.ResourceType == map.MapResources.Value[mapX, mapY].Type)
.Where(t => t.ResourceType == map.MapResources.Value[new MPos(mapX, mapY)].Type)
.Select(t => t.TerrainType).FirstOrDefault();
if (res == null)
@@ -114,7 +114,7 @@ namespace OpenRA.Graphics
{
var mapX = x + b.Left;
var mapY = y + b.Top;
var custom = map.CustomTerrain[mapX, mapY];
var custom = map.CustomTerrain[new MPos(mapX, mapY)];
if (custom == byte.MaxValue)
continue;
colors[y * stride + x] = world.TileSet[custom].Color.ToArgb();
@@ -148,9 +148,9 @@ namespace OpenRA.Graphics
var color = t.Trait.RadarSignatureColor(t.Actor);
foreach (var cell in t.Trait.RadarSignatureCells(t.Actor))
{
var uv = Map.CellToMap(map.TileShape, cell);
if (b.Contains(uv.X, uv.Y))
colors[(uv.Y - b.Top) * stride + uv.X - b.Left] = color.ToArgb();
var uv = cell.ToMPos(map);
if (b.Contains(uv.U, uv.V))
colors[(uv.V - b.Top) * stride + uv.U - b.Left] = color.ToArgb();
}
}
}
@@ -174,7 +174,6 @@ namespace OpenRA.Graphics
var shroud = Color.Black.ToArgb();
var fog = Color.FromArgb(128, Color.Black).ToArgb();
var offset = new CVec(b.Left, b.Top);
unsafe
{
@@ -184,11 +183,11 @@ namespace OpenRA.Graphics
var fogObscured = world.FogObscuresTest(map.Cells);
foreach (var uv in map.Cells.MapCoords)
{
var bitmapUv = uv - offset;
if (shroudObscured(uv.X, uv.Y))
colors[bitmapUv.Y * stride + bitmapUv.X] = shroud;
else if (fogObscured(uv.X, uv.Y))
colors[bitmapUv.Y * stride + bitmapUv.X] = fog;
var bitmapXy = new int2(uv.U - b.Left, uv.V - b.Top);
if (shroudObscured(uv))
colors[bitmapXy.Y * stride + bitmapXy.X] = shroud;
else if (fogObscured(uv))
colors[bitmapXy.Y * stride + bitmapXy.X] = fog;
}
}

View File

@@ -44,12 +44,11 @@ namespace OpenRA.Graphics
{
var verticesPerRow = 4 * map.Bounds.Width;
var cells = viewport.VisibleCells;
var shape = wr.World.Map.TileShape;
// Only draw the rows that are visible.
// VisibleCells is clamped to the map, so additional checks are unnecessary
var firstRow = Map.CellToMap(shape, cells.TopLeft).Y - map.Bounds.Top;
var lastRow = Map.CellToMap(shape, cells.BottomRight).Y - map.Bounds.Top + 1;
var firstRow = cells.TopLeft.ToMPos(map).V - map.Bounds.Top;
var lastRow = cells.BottomRight.ToMPos(map).V - map.Bounds.Top + 1;
Game.Renderer.WorldSpriteRenderer.DrawVertexBuffer(
vertexBuffer, verticesPerRow * firstRow, verticesPerRow * (lastRow - firstRow),

View File

@@ -94,8 +94,8 @@ namespace OpenRA.Graphics
var b = map.Bounds;
// Expand to corners of cells
var tl = wr.ScreenPxPosition(map.CenterOfCell(Map.MapToCell(map.TileShape, new CPos(b.Left, b.Top))) - new WVec(512, 512, 0));
var br = wr.ScreenPxPosition(map.CenterOfCell(Map.MapToCell(map.TileShape, new CPos(b.Right, b.Bottom))) + new WVec(511, 511, 0));
var tl = wr.ScreenPxPosition(map.CenterOfCell(new MPos(b.Left, b.Top).ToCPos(map)) - new WVec(512, 512, 0));
var br = wr.ScreenPxPosition(map.CenterOfCell(new MPos(b.Right, b.Bottom).ToCPos(map)) + new WVec(511, 511, 0));
mapBounds = Rectangle.FromLTRB(tl.X, tl.Y, br.X, br.Y);
maxGroundHeight = wr.World.TileSet.MaxGroundHeight;
@@ -164,15 +164,15 @@ namespace OpenRA.Graphics
var wbr = worldRenderer.Position(BottomRight);
// Visible rectangle in map coordinates
var ctl = new CPos(wtl.X / 1024, wtl.Y / 1024);
var ctl = new MPos(wtl.X / 1024, wtl.Y / 1024);
var dy = map.TileShape == TileShape.Diamond ? 512 : 1024;
var cbr = new CPos((wbr.X + 1023) / 1024, (wbr.Y + dy - 1) / dy);
var cbr = new MPos((wbr.X + 1023) / 1024, (wbr.Y + dy - 1) / dy);
// Add a 1 cell cordon to prevent holes, then convert back to cell coordinates
var tl = map.Clamp(Map.MapToCell(map.TileShape, ctl - new CVec(1, 1)));
var tl = map.Clamp(new MPos(ctl.U - 1, ctl.V - 1).ToCPos(map));
// Also need to account for height of cells in rows below the bottom
var br = map.Clamp(Map.MapToCell(map.TileShape, cbr + new CVec(1, 2 + maxGroundHeight / 2)));
var br = map.Clamp(new MPos(cbr.U + 1, cbr.V + 2 + maxGroundHeight / 2).ToCPos(map));
cells = new CellRegion(map.TileShape, tl, br);
cellsDirty = false;