public List <HexTile> GetTilesInRange(HexTile fromTile, int range) { //Debug.Log("Range: " + range); if (range <= 0 || range > mapRadius) { throw new System.Exception("range is out of bounds. GetTilesInRange()"); } List <HexTile> tilesInRange = new List <HexTile>(); for (int x = -range; x <= range; x++) { for (int y = Mathf.Max(-range, -x - range); y <= Mathf.Min(range, -x + range); y++) { if (x == fromTile.x && y == fromTile.y) { continue; } var z = -x - y; Vector3Int coords = AxialToCube(fromTile.GetAxialCoords()) + new Vector3Int(x, z, y); HexTile tileInRange = GetTileAxial(CubeToAxial(coords)); if (tileInRange != null) { tilesInRange.Add(tileInRange); } //Debug.Log("tile: " + neighbour); } } return(tilesInRange); }
/// <summary> /// Get the surrounding neighbours based on axial directions /// </summary> /// <param name="tile"></param> /// <returns></returns> public List <HexTile> GetNeighbours(HexTile tile) { if (tile == null) { return(null); } Vector2Int tileCoord = tile.GetAxialCoords(); List <HexTile> neighbours = new List <HexTile>(); for (int i = 0; i < 6; i++) { HexTile neighbour = GetTileAxial(tileCoord + axialDirections[i]); //In axial space, where center is 0,0 if (neighbour != null) { neighbours.Add(neighbour); } } return(neighbours); }
public List <HexTile> GetTilesRing(HexTile fromTile, int range) { if (range <= 0 || range > mapRadius) { throw new System.Exception("range is out of bounds. GetTilesAtRing()"); } List <HexTile> ring = new List <HexTile>(); Vector3Int coord = AxialToCube(fromTile.GetAxialCoords()) + AxialToCube(axialDirections[4]) * range; for (int i = 0; i < 6; i++) { for (int j = 0; j < range; j++) { HexTile ringTile = GetTileAxial(CubeToAxial(coord)); //Debug.Log("Tile: " + ringTile); //if(ringTile != null) ring.Add(ringTile); coord = AxialToCube(ringTile.GetAxialCoords()) + AxialToCube(axialDirections[i]); } } return(ring); }