Last active
February 5, 2019 06:59
-
-
Save skrolikowski/11d64a71178fdd82cd5af16c356ca3b9 to your computer and use it in GitHub Desktop.
GameExample.Map.Grid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using Microsoft.Xna.Framework; | |
using MonoGame.Extended; | |
using MonoGame.Extended.Shapes; | |
namespace EGen.Map | |
{ | |
public class Grid | |
{ | |
public int Rows { get; } | |
public int Cols { get; } | |
public int Size { get; } | |
public List<Cell> Cells { get; } | |
public Grid(int rows, int cols, int size) | |
{ | |
Rows = rows; | |
Cols = cols; | |
Size = size; | |
Cells = new List<Cell>(rows * cols); | |
for (int r = 0; r < rows; ++r) | |
for (int c = 0; c < cols; ++c) | |
Cells.Add(new Cell(this, r, c)); | |
} | |
public Cell GetCell(int row, int col) => Cells[GetIndex(row, col)]; | |
public int GetIndex(int row, int col) | |
{ | |
int idx = (col - 1) + (row - 1) * Cols; | |
if (idx < 0 || idx > Cells.Capacity) | |
throw new ArgumentException("Invalid " + (row > Rows ? "Row" : "Col") + " Request."); | |
return idx; | |
} | |
} | |
public class Cell | |
{ | |
private readonly Grid _grid; | |
public int Row { get; set; } | |
public int Col { get; set; } | |
public int Index { get => _grid.GetIndex(Row, Col); } | |
public Vector2 Position { get; set; } | |
public Size2 Dimensions { get; set; } | |
public RectangleF Container { get; set; } | |
public Cell(Grid grid, int row, int col) | |
{ | |
_grid = grid; | |
Row = row; | |
Col = col; | |
Dimensions = new Size2(grid.Size, grid.Size); | |
Position = new Vector2((Row - 1) * Dimensions.Height, (Col - 1) * Dimensions.Width); | |
Container = new RectangleF(Position, Dimensions); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment