mirror of
https://github.com/kwsch/NHSE.git
synced 2026-04-24 07:07:44 -05:00
Extract some grid logic to its own class
Name it more clearly so that the intent is there
This commit is contained in:
parent
8444fb4145
commit
0f28da8b2f
|
|
@ -62,7 +62,7 @@ public void SetAcreBytes(byte[] data)
|
|||
data.CopyTo(Data, Offsets.Acres);
|
||||
}
|
||||
|
||||
public TerrainTile[] GetTerrain() => TerrainTile.GetArray(Data.Slice(Offsets.Terrain, TerrainManager.TileCount * TerrainTile.SIZE));
|
||||
public TerrainTile[] GetTerrain() => TerrainTile.GetArray(Data.Slice(Offsets.Terrain, MapGrid.TileCount * TerrainTile.SIZE));
|
||||
public void SetTerrain(IReadOnlyList<TerrainTile> array) => TerrainTile.SetArray(array).CopyTo(Data, Offsets.Terrain);
|
||||
|
||||
public uint PlazaX
|
||||
|
|
|
|||
41
NHSE.Core/Structures/Acres/Terrain/MapGrid.cs
Normal file
41
NHSE.Core/Structures/Acres/Terrain/MapGrid.cs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
namespace NHSE.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Basic logic implementation for interacting with the manipulatable map grid.
|
||||
/// </summary>
|
||||
public abstract class MapGrid
|
||||
{
|
||||
public static readonly AcreCoordinate[] Acres = AcreCoordinate.GetGrid(AcreWidth, AcreHeight);
|
||||
|
||||
public const int GridWidth = 16;
|
||||
public const int GridHeight = 16;
|
||||
|
||||
public const int AcreWidth = 7;
|
||||
public const int AcreHeight = 6;
|
||||
public const int AcreSize = GridWidth * GridHeight;
|
||||
|
||||
public const int MapHeight = AcreHeight * GridHeight; // 92
|
||||
public const int MapWidth = AcreWidth * GridWidth; // 112
|
||||
public const int TileCount = MapWidth * MapHeight; // 0x2A00
|
||||
|
||||
public static int GetTileIndex(int x, int y) => (MapHeight * x) + y;
|
||||
|
||||
public 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)
|
||||
{
|
||||
var acre = Acres[acreIndex];
|
||||
var x = (tileIndex % GridWidth);
|
||||
var y = (tileIndex / GridHeight);
|
||||
return GetTileIndex(acre.X, acre.Y, x, y);
|
||||
}
|
||||
|
||||
public static int GetAcre(int x, int y) => (x / GridWidth) + ((y / GridHeight) * AcreWidth);
|
||||
public static int GetIndex(int x, int y) => (MapHeight * x) + y;
|
||||
}
|
||||
}
|
||||
|
|
@ -2,48 +2,19 @@
|
|||
|
||||
namespace NHSE.Core
|
||||
{
|
||||
public class TerrainManager
|
||||
public class TerrainManager : MapGrid
|
||||
{
|
||||
public readonly TerrainTile[] Tiles;
|
||||
public readonly AcreCoordinate[] Acres = AcreCoordinate.GetGrid(AcreWidth, AcreHeight);
|
||||
|
||||
public const int GridWidth = 16;
|
||||
public const int GridHeight = 16;
|
||||
|
||||
public const int AcreWidth = 7;
|
||||
public const int AcreHeight = 6;
|
||||
public const int AcreSize = GridWidth * GridHeight;
|
||||
|
||||
public const int MapHeight = AcreHeight * GridHeight; // 92
|
||||
public const int MapWidth = AcreWidth * GridWidth; // 112
|
||||
public const int TileCount = MapWidth * MapHeight; // 0x2A00
|
||||
|
||||
public TerrainManager(TerrainTile[] tiles)
|
||||
{
|
||||
Debug.Assert(TileCount == tiles.Length);
|
||||
Tiles = tiles;
|
||||
Debug.Assert(TileCount == tiles.Length);
|
||||
}
|
||||
|
||||
public static int GetIndex(int x, int y) => (MapHeight * x) + y;
|
||||
|
||||
public TerrainTile GetTile(int x, int y) => this[GetIndex(x, y)];
|
||||
|
||||
public TerrainTile GetTile(int acreX, int acreY, int gridX, int gridY)
|
||||
{
|
||||
var x = (acreX * GridWidth) + gridX;
|
||||
var y = (acreY * GridHeight) + gridY;
|
||||
return this[GetIndex(x, y)];
|
||||
}
|
||||
|
||||
public TerrainTile GetAcreTile(int acreIndex, int tileIndex)
|
||||
{
|
||||
var acre = Acres[acreIndex];
|
||||
var x = (tileIndex % GridWidth);
|
||||
var y = (tileIndex / GridHeight);
|
||||
return GetTile(acre.X, acre.Y, x, y);
|
||||
}
|
||||
|
||||
public static int GetAcre(int x, int y) => (x / GridWidth) + ((y / GridHeight) * AcreWidth);
|
||||
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)];
|
||||
|
||||
public TerrainTile this[int index]
|
||||
{
|
||||
|
|
@ -61,7 +32,7 @@ public byte[] DumpAllAcres()
|
|||
|
||||
public byte[] DumpAcre(int acre)
|
||||
{
|
||||
const int count = (GridWidth * GridHeight);
|
||||
int count = (GridWidth * GridHeight);
|
||||
var result = new byte[TerrainTile.SIZE * count];
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
|
|
@ -81,7 +52,7 @@ public void ImportAllAcres(byte[] data)
|
|||
|
||||
public void ImportAcre(int acre, byte[] data)
|
||||
{
|
||||
const int count = (GridWidth * GridHeight);
|
||||
int count = (GridWidth * GridHeight);
|
||||
var tiles = TerrainTile.GetArray(data);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -41,10 +41,10 @@ public static string GetTileName(TerrainTile tile)
|
|||
|
||||
public static Bitmap CreateMap(TerrainManager mgr)
|
||||
{
|
||||
var bmp = new Bitmap(TerrainManager.MapWidth, TerrainManager.MapHeight);
|
||||
for (int x = 0; x < TerrainManager.MapWidth; x++)
|
||||
var bmp = new Bitmap(MapGrid.MapWidth, MapGrid.MapHeight);
|
||||
for (int x = 0; x < MapGrid.MapWidth; x++)
|
||||
{
|
||||
for (int y = 0; y < TerrainManager.MapHeight; y++)
|
||||
for (int y = 0; y < MapGrid.MapHeight; y++)
|
||||
{
|
||||
var tile = mgr.GetTile(x, y);
|
||||
var color = GetTileColor(tile);
|
||||
|
|
@ -70,9 +70,9 @@ public static Bitmap CreateMap(TerrainManager mgr, int scale, int acreIndex = -1
|
|||
if (acreIndex < 0)
|
||||
return map;
|
||||
|
||||
var acre = mgr.Acres[acreIndex];
|
||||
var x = acre.X * TerrainManager.GridWidth;
|
||||
var y = acre.Y * TerrainManager.GridHeight;
|
||||
var acre = MapGrid.Acres[acreIndex];
|
||||
var x = acre.X * MapGrid.GridWidth;
|
||||
var y = acre.Y * MapGrid.GridHeight;
|
||||
|
||||
return DrawReticle(map, x, y, scale);
|
||||
}
|
||||
|
|
@ -82,8 +82,8 @@ private static Bitmap DrawReticle(Bitmap map, int x, int y, int scale)
|
|||
using var gfx = Graphics.FromImage(map);
|
||||
using var pen = new Pen(Color.Red);
|
||||
|
||||
int w = TerrainManager.GridWidth * scale;
|
||||
int h = TerrainManager.GridHeight * scale;
|
||||
int w = MapGrid.GridWidth * scale;
|
||||
int h = MapGrid.GridHeight * scale;
|
||||
gfx.DrawRectangle(pen, x * scale, y * scale, w, h);
|
||||
return map;
|
||||
}
|
||||
|
|
@ -135,7 +135,7 @@ private static void GetBuildingCoordinate(ushort bx, ushort by, int scale, out i
|
|||
{
|
||||
// Although there is terrain in the Top Row and Left Column, no buildings can be placed there.
|
||||
// Adjust the building coordinates down-right by an acre.
|
||||
const int buildingShift = TerrainManager.GridWidth;
|
||||
const int buildingShift = MapGrid.GridWidth;
|
||||
x = (int) (((bx / 2f) - buildingShift) * scale);
|
||||
y = (int) (((by / 2f) - buildingShift) * scale);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,8 +14,8 @@ public partial class TerrainEditor : Form
|
|||
private readonly Button[] Grid;
|
||||
private readonly TerrainManager Terrain;
|
||||
|
||||
private const int GridWidth = TerrainManager.GridWidth;
|
||||
private const int GridHeight = TerrainManager.GridHeight;
|
||||
private const int GridWidth = MapGrid.GridWidth;
|
||||
private const int GridHeight = MapGrid.GridHeight;
|
||||
|
||||
private const int SquareSize = 50;
|
||||
private const int MapScale = 2;
|
||||
|
|
@ -26,9 +26,9 @@ public TerrainEditor(MainSave sav)
|
|||
InitializeComponent();
|
||||
|
||||
Terrain = new TerrainManager(SAV.GetTerrain());
|
||||
Grid = GenerateGrid(TerrainManager.GridWidth, TerrainManager.GridHeight);
|
||||
Grid = GenerateGrid(MapGrid.GridWidth, MapGrid.GridHeight);
|
||||
|
||||
foreach (var acre in Terrain.Acres)
|
||||
foreach (var acre in MapGrid.Acres)
|
||||
CB_Acre.Items.Add(acre.Name);
|
||||
|
||||
PG_Tile.SelectedObject = new TerrainTile();
|
||||
|
|
@ -45,8 +45,8 @@ public TerrainEditor(MainSave sav)
|
|||
|
||||
private void ChangeViewToAcre(int acre)
|
||||
{
|
||||
X = (acre % TerrainManager.AcreWidth) * GridWidth;
|
||||
Y = (acre / TerrainManager.AcreWidth) * GridHeight;
|
||||
X = (acre % MapGrid.AcreWidth) * GridWidth;
|
||||
Y = (acre / MapGrid.AcreWidth) * GridHeight;
|
||||
|
||||
LoadGrid(X, Y);
|
||||
UpdateArrowVisibility(acre);
|
||||
|
|
@ -63,7 +63,7 @@ private void LoadGrid(int topX, int topY)
|
|||
var i = (y * GridWidth) + x;
|
||||
var rx = topX + x;
|
||||
var ry = topY + y;
|
||||
var ri = TerrainManager.GetIndex(rx, ry);
|
||||
var ri = MapGrid.GetIndex(rx, ry);
|
||||
var b = Grid[i];
|
||||
if (ri >= Terrain.Tiles.Length)
|
||||
{
|
||||
|
|
@ -82,10 +82,10 @@ private void LoadGrid(int topX, int topY)
|
|||
|
||||
private void UpdateArrowVisibility(int index)
|
||||
{
|
||||
B_Up.Enabled = index >= TerrainManager.AcreWidth;
|
||||
B_Down.Enabled = index < TerrainManager.AcreWidth * (TerrainManager.AcreHeight - 1);
|
||||
B_Left.Enabled = index % TerrainManager.AcreWidth != 0;
|
||||
B_Right.Enabled = index % TerrainManager.AcreWidth != TerrainManager.AcreWidth - 1;
|
||||
B_Up.Enabled = index >= MapGrid.AcreWidth;
|
||||
B_Down.Enabled = index < MapGrid.AcreWidth * (MapGrid.AcreHeight - 1);
|
||||
B_Left.Enabled = index % MapGrid.AcreWidth != 0;
|
||||
B_Right.Enabled = index % MapGrid.AcreWidth != MapGrid.AcreWidth - 1;
|
||||
}
|
||||
|
||||
private Button[] GenerateGrid(int w, int h)
|
||||
|
|
@ -201,10 +201,10 @@ private static void RefreshTile(Control button, TerrainTile tile)
|
|||
button.BackColor = TerrainSprite.GetTileColor(tile);
|
||||
}
|
||||
|
||||
private void B_Up_Click(object sender, EventArgs e) => CB_Acre.SelectedIndex -= TerrainManager.AcreWidth;
|
||||
private void B_Up_Click(object sender, EventArgs e) => CB_Acre.SelectedIndex -= MapGrid.AcreWidth;
|
||||
private void B_Left_Click(object sender, EventArgs e) => --CB_Acre.SelectedIndex;
|
||||
private void B_Right_Click(object sender, EventArgs e) => ++CB_Acre.SelectedIndex;
|
||||
private void B_Down_Click(object sender, EventArgs e) => CB_Acre.SelectedIndex += TerrainManager.AcreWidth;
|
||||
private void B_Down_Click(object sender, EventArgs e) => CB_Acre.SelectedIndex += MapGrid.AcreWidth;
|
||||
|
||||
private void B_ZeroElevation_Click(object sender, EventArgs e)
|
||||
{
|
||||
|
|
@ -271,7 +271,7 @@ private void B_ImportAcre_Click(object sender, EventArgs e)
|
|||
var path = ofd.FileName;
|
||||
var fi = new FileInfo(path);
|
||||
|
||||
const int expect = TerrainManager.AcreSize * TerrainTile.SIZE;
|
||||
const int expect = MapGrid.AcreSize * TerrainTile.SIZE;
|
||||
if (fi.Length != expect)
|
||||
{
|
||||
WinFormsUtil.Error($"Expected size (0x{expect:X}) != Input size (0x{fi.Length:X}", path);
|
||||
|
|
@ -297,7 +297,7 @@ private void B_ImportAllAcres_Click(object sender, EventArgs e)
|
|||
var path = ofd.FileName;
|
||||
var fi = new FileInfo(path);
|
||||
|
||||
const int expect = TerrainManager.TileCount * TerrainTile.SIZE;
|
||||
const int expect = MapGrid.TileCount * TerrainTile.SIZE;
|
||||
if (fi.Length != expect)
|
||||
{
|
||||
WinFormsUtil.Error($"Expected size (0x{expect:X}) != Input size (0x{fi.Length:X}", path);
|
||||
|
|
@ -341,7 +341,7 @@ private void ClickMapAt(MouseEventArgs e, bool skipLagCheck)
|
|||
bool centerReticle = CHK_SnapToAcre.Checked;
|
||||
GetViewAnchorCoordinates(mX, mY, out var x, out var y, centerReticle);
|
||||
|
||||
var acre = TerrainManager.GetAcre(x, y);
|
||||
var acre = MapGrid.GetAcre(x, y);
|
||||
bool sameAcre = AcreIndex == acre;
|
||||
if (!skipLagCheck)
|
||||
{
|
||||
|
|
@ -381,8 +381,8 @@ private static void GetViewAnchorCoordinates(int mX, int mY, out int x, out int
|
|||
GetCursorCoordinates(mX, mY, out x, out y);
|
||||
|
||||
// Clamp to viewport dimensions, and center to nearest acre if desired.
|
||||
const int maxX = ((TerrainManager.AcreWidth - 1) * GridWidth);
|
||||
const int maxY = ((TerrainManager.AcreHeight - 1) * GridHeight);
|
||||
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.
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user