/// <summary> /// Creates a new new HexGrid. /// </summary> /// <param name="width">The number of columns</param> /// <param name="height">The number of rows</param> public HexGrid(int width, int height) { _width = width + 1; _height = height; _gridOffset = HexCoordinate.FromCartesian(-new Vector2(_width * 0.5f, 0.66f * _height * 0.5f)); _cells = new T[_width, _height]; }
/// <summary> /// Creates a new new HexGrid at a specfic position. /// </summary> /// <param name="width">The number of columns</param> /// <param name="height">The number of rows</param> /// <param name="position">The center of the grid in cartesian space</param>> /// <param name="hexDimensions">Hex cell dimensions, x = width in world units, y = squash factor eg. 0.66 means hex height is 66% what it would be for a cell of that width</param> public HexGrid(int width, int height, Vector2 position, Vector2 hexDimensions) { _width = width + 1; _height = height; _gridOffset = HexCoordinate.FromCartesian(position - new Vector2(hexDimensions.x * _width * 0.5f, hexDimensions.y * _height * 0.5f)); _cells = new T[_width, _height]; }
/// <summary> /// Gets the cell stored at a particular coordinate. /// </summary> /// <param name="coordinate">The coordinate to check</param> /// <returns>The cell at the coordinate if it exists, otherwise null</returns> public T GetCell(HexCoordinate coordinate) { int column = GetColumn(coordinate); int row = GetRow(coordinate); if (CheckValidOffset(column, row)) { return(_cells[column, row]); } return(null); }
/// <summary> /// Finds all the stored cells neighbouring a particular coordinate. /// </summary> /// <param name="coordinate">The coordinate to check</param> /// <returns>A list of cells neighbouring the coordinate, starting east and going in a clockwise direction</returns> public List <T> GetNeighbours(HexCoordinate coordinate) { Assert.IsTrue(IsOnGrid(coordinate), "Invalid coordinate " + coordinate); List <T> neighbours = new List <T>(6); T cell = GetCell(coordinate + HexCoordinate.East); if (cell != null) { neighbours.Add(cell); } cell = GetCell(coordinate + HexCoordinate.SouthEast); if (cell != null) { neighbours.Add(cell); } cell = GetCell(coordinate + HexCoordinate.SouthWest); if (cell != null) { neighbours.Add(cell); } cell = GetCell(coordinate + HexCoordinate.West); if (cell != null) { neighbours.Add(cell); } cell = GetCell(coordinate + HexCoordinate.NorthWest); if (cell != null) { neighbours.Add(cell); } cell = GetCell(coordinate + HexCoordinate.NorthEast); if (cell != null) { neighbours.Add(cell); } return(neighbours); }
/// <summary> /// Finds all the stored cells that are within a radius of a coordinate. /// </summary> /// <param name="center">The center of the circle</param> /// <param name="radius">The number to cells outwards from the center to check</param> /// <returns>A list of cells stored around the center coordinate within the radius</returns> public List <T> GetCellsInRadius(HexCoordinate center, int radius) { Assert.IsTrue(IsOnGrid(center), "Invalid coordinate " + center); Assert.IsTrue(radius >= 0); List <HexCoordinate> coordinates = center.GetCoordinatesInRadius(radius); List <T> cells = new List <T>(coordinates.Count); for (int i = 0; i < coordinates.Count; i++) { T cell = GetCell(coordinates[i]); if (cell != null) { cells.Add(cell); } } return(cells); }
/// <summary> /// Finds the hex coordinate that is nearest to this position. /// </summary> /// <returns>The nearest hex coordinate to this cartesian position</returns> public static HexCoordinate ToHexCoordinate(this Vector2 vector) { return(HexCoordinate.FromCartesian(vector)); }
int GetColumn(HexCoordinate coordinate) { return((coordinate.X - _gridOffset.X) + ((coordinate.Z - _gridOffset.Z) - ((coordinate.Z - _gridOffset.Z) & 1)) / 2); }
int GetRow(HexCoordinate coordinate) { return(coordinate.Z - _gridOffset.Z); }
/// <summary> /// Checks whether a coordinate is a valid coordinate on this grid. /// </summary> /// <param name="coordinate">The coordinate to check</param> /// <returns>True if the coorinate is within the grid's bounds</returns> public bool IsOnGrid(HexCoordinate coordinate) { return(CheckValidOffset(GetColumn(coordinate), GetRow(coordinate))); }
/// <summary> /// Clears the cell at a specific coordinate. /// </summary> /// <param name="coordinate">The coordinate to clear</param> public void ClearCell(HexCoordinate coordinate) { Assert.IsTrue(IsOnGrid(coordinate), "Invalid coordinate " + coordinate); _cells[GetColumn(coordinate), GetRow(coordinate)] = null; }
/// <summary> /// Stores a cell at a coordinate. /// </summary> /// <param name="coordinate">The coordinate to store at</param> /// <param name="cell">The cell to store</param> public T SetCell(HexCoordinate coordinate, T cell) { Assert.IsTrue(IsOnGrid(coordinate), "Invalid coordinate " + coordinate); _cells[GetColumn(coordinate), GetRow(coordinate)] = cell; return(cell); }
/// <summary> /// Checks whether there is a cell stored at a specific coordinate. /// </summary> /// <param name="coordinate">The coordinate to check</param> /// <returns>True if there is no cell stored at the coordinate</returns> public bool IsCellEmpty(HexCoordinate coordinate) { Assert.IsTrue(IsOnGrid(coordinate), "Invalid coordinate " + coordinate); return(_cells[GetColumn(coordinate), GetRow(coordinate)] == null); }