// Returns the tuple located at the corner of a particular hexagon piece private HexagonTuple GetTupleAtCorner(int x, int y, HexagonPiece.Corner corner) { // Make sure that there is a tuple at this corner if (grid[x][y].GetPickableCorner(corner) == corner) { int y2 = x % 2 == 0 ? y : y + 1; switch (corner) { // It is important that the pieces are stored in clockwise order case HexagonPiece.Corner.BottomLeft: return(new HexagonTuple(grid[x][y], grid[x][y - 1], grid[x - 1][y2 - 1])); case HexagonPiece.Corner.BottomRight: return(new HexagonTuple(grid[x][y], grid[x + 1][y2 - 1], grid[x][y - 1])); case HexagonPiece.Corner.Left: return(new HexagonTuple(grid[x][y], grid[x - 1][y2 - 1], grid[x - 1][y2])); case HexagonPiece.Corner.Right: return(new HexagonTuple(grid[x][y], grid[x + 1][y2], grid[x + 1][y2 - 1])); case HexagonPiece.Corner.TopLeft: return(new HexagonTuple(grid[x][y], grid[x - 1][y2], grid[x][y + 1])); case HexagonPiece.Corner.TopRight: return(new HexagonTuple(grid[x][y], grid[x][y + 1], grid[x + 1][y2])); } } return(new HexagonTuple()); }
// If points resides inside the grid, locate the three adjacent hexagon pieces (tuple) that are closest to the point public bool TryGetTupleAt(Vector2 point, out HexagonTuple tuple) { if (point.x <= 0f || point.x >= gridSize.x || point.y <= 0f || point.y >= gridSize.y) { tuple = new HexagonTuple(); return(false); } // Calculate the row and column indices of the hexagon piece that the point resides inside int x, y; Utils.GetCoordinatesFrom(point, out x, out y); // Find the hexagon piece's corner that is closest to the point // This corner is guaranteed to have two adjacent hexagon pieces (i.e. GetPickableCorner) HexagonPiece.Corner corner = grid[x][y].GetPickableCorner(grid[x][y].GetClosestCorner(point - (Vector2)grid[x][y].transform.localPosition)); tuple = GetTupleAtCorner(x, y, corner); return(true); }