Refactoring

Rename some variables
This commit is contained in:
Kurt 2020-04-06 14:57:37 -07:00
parent 0f28da8b2f
commit 5293ec594a
4 changed files with 53 additions and 53 deletions

View File

@ -62,7 +62,7 @@ public void SetAcreBytes(byte[] data)
data.CopyTo(Data, Offsets.Acres);
}
public TerrainTile[] GetTerrain() => TerrainTile.GetArray(Data.Slice(Offsets.Terrain, MapGrid.TileCount * TerrainTile.SIZE));
public TerrainTile[] GetTerrain() => TerrainTile.GetArray(Data.Slice(Offsets.Terrain, MapGrid.MapTileCount * TerrainTile.SIZE));
public void SetTerrain(IReadOnlyList<TerrainTile> array) => TerrainTile.SetArray(array).CopyTo(Data, Offsets.Terrain);
public uint PlazaX

View File

@ -1,4 +1,6 @@
namespace NHSE.Core
using System;
namespace NHSE.Core
{
/// <summary>
/// Basic logic implementation for interacting with the manipulatable map grid.
@ -12,22 +14,22 @@ public abstract class MapGrid
public const int AcreWidth = 7;
public const int AcreHeight = 6;
public const int AcreSize = GridWidth * GridHeight;
public const int AcreTileCount = GridWidth * GridHeight;
public const int MapHeight = AcreHeight * GridHeight; // 92
public const int MapHeight = AcreHeight * GridHeight; // 96
public const int MapWidth = AcreWidth * GridWidth; // 112
public const int TileCount = MapWidth * MapHeight; // 0x2A00
public const int MapTileCount = MapWidth * MapHeight; // 0x2A00 bytes for
public static int GetTileIndex(int x, int y) => (MapHeight * x) + y;
protected static int GetTileIndex(int x, int y) => (MapHeight * x) + y;
public static int GetTileIndex(int acreX, int acreY, int gridX, int gridY)
protected static int GetTileIndex(int acreX, int acreY, int gridX, int gridY)
{
var x = (acreX * GridWidth) + gridX;
var y = (acreY * GridHeight) + gridY;
return GetTileIndex(x, y);
}
public static int GetAcreTileIndex(int acreIndex, int tileIndex)
protected static int GetAcreTileIndex(int acreIndex, int tileIndex)
{
var acre = Acres[acreIndex];
var x = (tileIndex % GridWidth);
@ -36,6 +38,31 @@ public static int GetAcreTileIndex(int acreIndex, int tileIndex)
}
public static int GetAcre(int x, int y) => (x / GridWidth) + ((y / GridHeight) * AcreWidth);
public static int GetIndex(int x, int y) => (MapHeight * x) + y;
public static void GetViewAnchorCoordinates(ref int x, ref int y, bool centerReticle)
{
// If we aren't snapping the reticle to the nearest acre
// we want to put the middle of the reticle rectangle where the cursor is.
// Adjust the view coordinate
if (!centerReticle)
{
// Reticle size is GridWidth, center = /2
x -= GridWidth / 2;
y -= GridWidth / 2;
}
// Clamp to viewport dimensions, and center to nearest acre if desired.
// Clamp to boundaries so that we always have 16x16 to view.
const int maxX = ((AcreWidth - 1) * GridWidth);
const int maxY = ((AcreHeight - 1) * GridHeight);
x = Math.Max(0, Math.Min(x, maxX));
y = Math.Max(0, Math.Min(y, maxY));
}
public static void GetViewAnchorCoordinates(int acre, out int x, out int y)
{
x = (acre % AcreWidth) * GridWidth;
y = (acre / AcreWidth) * GridHeight;
}
}
}
}

View File

@ -9,10 +9,10 @@ public class TerrainManager : MapGrid
public TerrainManager(TerrainTile[] tiles)
{
Tiles = tiles;
Debug.Assert(TileCount == tiles.Length);
Debug.Assert(MapTileCount == tiles.Length);
}
public TerrainTile GetTile(int x, int y) => this[GetIndex(x, y)];
public TerrainTile GetTile(int x, int y) => this[GetTileIndex(x, y)];
public TerrainTile GetTile(int acreX, int acreY, int gridX, int gridY) => this[GetTileIndex(acreX, acreY, gridX, gridY)];
public TerrainTile GetAcreTile(int acreIndex, int tileIndex) => this[GetAcreTileIndex(acreIndex, tileIndex)];
@ -32,7 +32,7 @@ public byte[] DumpAllAcres()
public byte[] DumpAcre(int acre)
{
int count = (GridWidth * GridHeight);
const int count = AcreTileCount;
var result = new byte[TerrainTile.SIZE * count];
for (int i = 0; i < count; i++)
{
@ -52,7 +52,7 @@ public void ImportAllAcres(byte[] data)
public void ImportAcre(int acre, byte[] data)
{
int count = (GridWidth * GridHeight);
const int count = AcreTileCount;
var tiles = TerrainTile.GetArray(data);
for (int i = 0; i < count; i++)
{

View File

@ -26,7 +26,7 @@ public TerrainEditor(MainSave sav)
InitializeComponent();
Terrain = new TerrainManager(SAV.GetTerrain());
Grid = GenerateGrid(MapGrid.GridWidth, MapGrid.GridHeight);
Grid = GenerateGrid(GridWidth, GridHeight);
foreach (var acre in MapGrid.Acres)
CB_Acre.Items.Add(acre.Name);
@ -45,9 +45,7 @@ public TerrainEditor(MainSave sav)
private void ChangeViewToAcre(int acre)
{
X = (acre % MapGrid.AcreWidth) * GridWidth;
Y = (acre / MapGrid.AcreWidth) * GridHeight;
MapGrid.GetViewAnchorCoordinates(acre, out X, out Y);
LoadGrid(X, Y);
UpdateArrowVisibility(acre);
}
@ -60,21 +58,13 @@ private void LoadGrid(int topX, int topY)
{
for (int y = 0; y < GridHeight; y++)
{
var i = (y * GridWidth) + x;
var controlIndex = (y * GridWidth) + x;
var b = Grid[controlIndex];
var rx = topX + x;
var ry = topY + y;
var ri = MapGrid.GetIndex(rx, ry);
var b = Grid[i];
if (ri >= Terrain.Tiles.Length)
{
b.Visible = false;
}
else
{
b.Visible = true;
var tile = Terrain.GetTile(rx, ry);
RefreshTile(b, tile);
}
var tile = Terrain.GetTile(rx, ry);
RefreshTile(b, tile);
}
}
ReloadMap();
@ -96,7 +86,7 @@ private Button[] GenerateGrid(int w, int h)
{
for (int x = 0; x < w; x++)
{
var item = GetGridItem(index: i, x, y);
var item = CreateGridItem(index: i, x, y);
FLP_Tile.Controls.Add(item);
grid[i++] = item;
}
@ -108,7 +98,7 @@ private Button[] GenerateGrid(int w, int h)
return grid;
}
private Button GetGridItem(int index, int x, int y)
private Button CreateGridItem(int index, int x, int y)
{
var button = new Button
{
@ -271,7 +261,7 @@ private void B_ImportAcre_Click(object sender, EventArgs e)
var path = ofd.FileName;
var fi = new FileInfo(path);
const int expect = MapGrid.AcreSize * TerrainTile.SIZE;
const int expect = MapGrid.AcreTileCount * TerrainTile.SIZE;
if (fi.Length != expect)
{
WinFormsUtil.Error($"Expected size (0x{expect:X}) != Input size (0x{fi.Length:X}", path);
@ -297,7 +287,7 @@ private void B_ImportAllAcres_Click(object sender, EventArgs e)
var path = ofd.FileName;
var fi = new FileInfo(path);
const int expect = MapGrid.TileCount * TerrainTile.SIZE;
const int expect = MapGrid.MapTileCount * TerrainTile.SIZE;
if (fi.Length != expect)
{
WinFormsUtil.Error($"Expected size (0x{expect:X}) != Input size (0x{fi.Length:X}", path);
@ -379,24 +369,7 @@ private static void GetCursorCoordinates(int mX, int mY, out int x, out int y)
private static void GetViewAnchorCoordinates(int mX, int mY, out int x, out int y, bool centerReticle)
{
GetCursorCoordinates(mX, mY, out x, out y);
// Clamp to viewport dimensions, and center to nearest acre if desired.
const int maxX = ((MapGrid.AcreWidth - 1) * GridWidth);
const int maxY = ((MapGrid.AcreHeight - 1) * GridHeight);
// If we aren't snapping the reticle to the nearest acre
// we want to put the middle of the reticle rectangle where the cursor is.
// Adjust the view coordinate
if (!centerReticle)
{
// Reticle size is GridWidth, center = /2
x -= GridWidth / 2;
y -= GridWidth / 2;
}
// Clamp to boundaries so that we always have 16x16 to view.
x = Math.Max(0, Math.Min(x, maxX));
y = Math.Max(0, Math.Min(y, maxY));
MapGrid.GetViewAnchorCoordinates(ref x, ref y, centerReticle);
}
private void PB_Map_MouseMove(object sender, MouseEventArgs e)