Rename enum TileShape to MapGridType

This commit is contained in:
Pavel Penev
2015-10-01 00:48:01 +03:00
parent 06ba175fde
commit bb3aea338a
17 changed files with 63 additions and 71 deletions

View File

@@ -20,7 +20,7 @@ namespace OpenRA
{
public readonly Size Size;
readonly Rectangle bounds;
public readonly TileShape Shape;
public readonly MapGridType GridType;
public event Action<CPos> CellEntryChanged = null;
readonly T[] entries;
@@ -28,28 +28,28 @@ namespace OpenRA
public CellLayer(Map map)
: this(map.Grid.Type, new Size(map.MapSize.X, map.MapSize.Y)) { }
public CellLayer(TileShape shape, Size size)
public CellLayer(MapGridType gridType, Size size)
{
Size = size;
bounds = new Rectangle(0, 0, Size.Width, Size.Height);
Shape = shape;
GridType = gridType;
entries = new T[size.Width * size.Height];
}
public void CopyValuesFrom(CellLayer<T> anotherLayer)
{
if (Size != anotherLayer.Size || Shape != anotherLayer.Shape)
if (Size != anotherLayer.Size || GridType != anotherLayer.GridType)
throw new ArgumentException(
"layers must have a matching size and shape.", "anotherLayer");
"layers must have a matching size and shape (grid type).", "anotherLayer");
if (CellEntryChanged != null)
throw new InvalidOperationException(
"Cannot copy values when there are listeners attached to the CellEntryChanged event.");
Array.Copy(anotherLayer.entries, entries, entries.Length);
}
public static CellLayer<T> CreateInstance(Func<MPos, T> initialCellValueFactory, Size size, TileShape tileShape)
public static CellLayer<T> CreateInstance(Func<MPos, T> initialCellValueFactory, Size size, MapGridType mapGridType)
{
var cellLayer = new CellLayer<T>(tileShape, size);
var cellLayer = new CellLayer<T>(mapGridType, size);
for (var v = 0; v < size.Height; v++)
{
for (var u = 0; u < size.Width; u++)
@@ -65,7 +65,7 @@ namespace OpenRA
// Resolve an array index from cell coordinates
int Index(CPos cell)
{
return Index(cell.ToMPos(Shape));
return Index(cell.ToMPos(GridType));
}
// Resolve an array index from map coordinates
@@ -104,7 +104,7 @@ namespace OpenRA
entries[Index(uv)] = value;
if (CellEntryChanged != null)
CellEntryChanged(uv.ToCPos(Shape));
CellEntryChanged(uv.ToCPos(GridType));
}
}
@@ -130,10 +130,10 @@ namespace OpenRA
// .ToMPos() returns the same result if the X and Y coordinates
// are switched. X < Y is invalid in the Diamond coordinate system,
// so we pre-filter these to avoid returning the wrong result
if (Shape == TileShape.Diamond && cell.X < cell.Y)
if (GridType == MapGridType.Diamond && cell.X < cell.Y)
return false;
return Contains(cell.ToMPos(Shape));
return Contains(cell.ToMPos(GridType));
}
public bool Contains(MPos uv)
@@ -143,7 +143,7 @@ namespace OpenRA
public CPos Clamp(CPos uv)
{
return Clamp(uv.ToMPos(Shape)).ToCPos(Shape);
return Clamp(uv.ToMPos(GridType)).ToCPos(GridType);
}
public MPos Clamp(MPos uv)
@@ -158,7 +158,7 @@ namespace OpenRA
/// <summary>Create a new layer by resizing another layer. New cells are filled with defaultValue.</summary>
public static CellLayer<T> Resize<T>(CellLayer<T> layer, Size newSize, T defaultValue)
{
var result = new CellLayer<T>(layer.Shape, newSize);
var result = new CellLayer<T>(layer.GridType, newSize);
var width = Math.Min(layer.Size.Width, newSize.Width);
var height = Math.Min(layer.Size.Height, newSize.Height);

View File

@@ -22,33 +22,33 @@ namespace OpenRA
// Corners of the region
public readonly CPos TopLeft;
public readonly CPos BottomRight;
readonly TileShape shape;
readonly MapGridType gridType;
// Corners in map coordinates
// These will only equal TopLeft and BottomRight for TileShape.Rectangular
// These will only equal TopLeft and BottomRight for MapGridType.Rectangular
readonly MPos mapTopLeft;
readonly MPos mapBottomRight;
public CellRegion(TileShape shape, CPos topLeft, CPos bottomRight)
public CellRegion(MapGridType gridType, CPos topLeft, CPos bottomRight)
{
this.shape = shape;
this.gridType = gridType;
TopLeft = topLeft;
BottomRight = bottomRight;
mapTopLeft = TopLeft.ToMPos(shape);
mapBottomRight = BottomRight.ToMPos(shape);
mapTopLeft = TopLeft.ToMPos(gridType);
mapBottomRight = BottomRight.ToMPos(gridType);
}
/// <summary>Expand the specified region with an additional cordon. This may expand the region outside the map borders.</summary>
public static CellRegion Expand(CellRegion region, int cordon)
{
var tl = new MPos(region.mapTopLeft.U - cordon, region.mapTopLeft.V - cordon).ToCPos(region.shape);
var br = new MPos(region.mapBottomRight.U + cordon, region.mapBottomRight.V + cordon).ToCPos(region.shape);
return new CellRegion(region.shape, tl, br);
var tl = new MPos(region.mapTopLeft.U - cordon, region.mapTopLeft.V - cordon).ToCPos(region.gridType);
var br = new MPos(region.mapBottomRight.U + cordon, region.mapBottomRight.V + cordon).ToCPos(region.gridType);
return new CellRegion(region.gridType, tl, br);
}
/// <summary>Returns the minimal region that covers at least the specified cells.</summary>
public static CellRegion BoundingRegion(TileShape shape, IEnumerable<CPos> cells)
public static CellRegion BoundingRegion(MapGridType shape, IEnumerable<CPos> cells)
{
if (cells == null || !cells.Any())
throw new ArgumentException("cells must not be null or empty.", "cells");
@@ -81,7 +81,7 @@ namespace OpenRA
public bool Contains(CPos cell)
{
var uv = cell.ToMPos(shape);
var uv = cell.ToMPos(gridType);
return uv.U >= mapTopLeft.U && uv.U <= mapBottomRight.U && uv.V >= mapTopLeft.V && uv.V <= mapBottomRight.V;
}
@@ -136,7 +136,7 @@ namespace OpenRA
return false;
}
current = new MPos(u, v).ToCPos(r.shape);
current = new MPos(u, v).ToCPos(r.gridType);
return true;
}

View File

@@ -9,10 +9,8 @@
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
@@ -428,10 +426,10 @@ namespace OpenRA
foreach (var uv in AllCells.MapCoords)
CustomTerrain[uv] = byte.MaxValue;
var leftDelta = Grid.Type == TileShape.Diamond ? new WVec(-512, 0, 0) : new WVec(-512, -512, 0);
var topDelta = Grid.Type == TileShape.Diamond ? new WVec(0, -512, 0) : new WVec(512, -512, 0);
var rightDelta = Grid.Type == TileShape.Diamond ? new WVec(512, 0, 0) : new WVec(512, 512, 0);
var bottomDelta = Grid.Type == TileShape.Diamond ? new WVec(0, 512, 0) : new WVec(-512, 512, 0);
var leftDelta = Grid.Type == MapGridType.Diamond ? new WVec(-512, 0, 0) : new WVec(-512, -512, 0);
var topDelta = Grid.Type == MapGridType.Diamond ? new WVec(0, -512, 0) : new WVec(512, -512, 0);
var rightDelta = Grid.Type == MapGridType.Diamond ? new WVec(512, 0, 0) : new WVec(512, 512, 0);
var bottomDelta = Grid.Type == MapGridType.Diamond ? new WVec(0, 512, 0) : new WVec(-512, 512, 0);
CellCorners = CellCornerHalfHeights.Select(ramp => new WVec[]
{
leftDelta + new WVec(0, 0, 512 * ramp[0]),
@@ -756,7 +754,7 @@ namespace OpenRA
// .ToMPos() returns the same result if the X and Y coordinates
// are switched. X < Y is invalid in the Diamond coordinate system,
// so we pre-filter these to avoid returning the wrong result
if (Grid.Type == TileShape.Diamond && cell.X < cell.Y)
if (Grid.Type == MapGridType.Diamond && cell.X < cell.Y)
return false;
return Contains(cell.ToMPos(this));
@@ -788,7 +786,7 @@ namespace OpenRA
public WPos CenterOfCell(CPos cell)
{
if (Grid.Type == TileShape.Rectangle)
if (Grid.Type == MapGridType.Rectangle)
return new WPos(1024 * cell.X + 512, 1024 * cell.Y + 512, 0);
// Convert from diamond cell position (x, y) to world position (u, v):
@@ -820,7 +818,7 @@ namespace OpenRA
public CPos CellContaining(WPos pos)
{
if (Grid.Type == TileShape.Rectangle)
if (Grid.Type == MapGridType.Rectangle)
return new CPos(pos.X / 1024, pos.Y / 1024);
// Convert from world position to diamond cell position:
@@ -901,7 +899,7 @@ namespace OpenRA
// for diamond cells.
var wtop = tl.V * 1024;
var wbottom = (br.V + 1) * 1024;
if (Grid.Type == TileShape.Diamond)
if (Grid.Type == MapGridType.Diamond)
{
wtop /= 2;
wbottom /= 2;

View File

@@ -23,7 +23,7 @@ namespace OpenRA
{
public sealed class MapCache : IEnumerable<MapPreview>, IDisposable
{
public static readonly MapPreview UnknownMap = new MapPreview(null, TileShape.Rectangle, null);
public static readonly MapPreview UnknownMap = new MapPreview(null, MapGridType.Rectangle, null);
readonly Cache<string, MapPreview> previews;
readonly ModData modData;
readonly SheetBuilder sheetBuilder;

View File

@@ -13,11 +13,11 @@ using System.IO;
namespace OpenRA
{
public enum TileShape { Rectangle, Diamond }
public enum MapGridType { Rectangle, Diamond }
public class MapGrid : IGlobalModData
{
public readonly TileShape Type = TileShape.Rectangle;
public readonly MapGridType Type = MapGridType.Rectangle;
public readonly Size TileSize = new Size(24, 24);
public readonly byte MaximumTerrainHeight = 0;
public readonly byte SubCellDefaultIndex = byte.MaxValue;

View File

@@ -47,7 +47,7 @@ namespace OpenRA
public readonly int players;
public readonly Rectangle bounds;
public readonly int[] spawnpoints = { };
public readonly TileShape map_grid_type;
public readonly MapGridType map_grid_type;
public readonly string minimap;
public readonly bool downloading;
}
@@ -63,7 +63,7 @@ namespace OpenRA
public string Author { get; private set; }
public int PlayerCount { get; private set; }
public CPos[] SpawnPoints { get; private set; }
public TileShape GridType { get; private set; }
public MapGridType GridType { get; private set; }
public Rectangle Bounds { get; private set; }
public Bitmap CustomPreview { get; private set; }
public Map Map { get; private set; }
@@ -99,7 +99,7 @@ namespace OpenRA
generatingMinimap = false;
}
public MapPreview(string uid, TileShape gridType, MapCache cache)
public MapPreview(string uid, MapGridType gridType, MapCache cache)
{
this.cache = cache;
Uid = uid;

View File

@@ -8,10 +8,8 @@
*/
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace OpenRA
{
@@ -43,7 +41,7 @@ namespace OpenRA
// Each height step is equivalent to 512 WRange units, which is one MPos
// step for diamond cells, but only half a MPos step for classic cells. Doh!
var maxHeight = map.Grid.MaximumTerrainHeight;
var heightOffset = map.Grid.Type == TileShape.Diamond ? maxHeight : maxHeight / 2;
var heightOffset = map.Grid.Type == MapGridType.Diamond ? maxHeight : maxHeight / 2;
// Use the MapHeight data array to clamp the bottom coordinate so it doesn't overflow the map
mapBottomRight = map.MapHeight.Value.Clamp(new MPos(bottomRight.U, bottomRight.V + heightOffset));