private HexagonGroup GetGroupAtCorner(int x, int y, HexagonPiece.Edge corner) { if (grid[x][y].HandleEdge(corner) != corner) { return(new HexagonGroup()); } int y2 = x % 2 == 0 ? y : y + 1; switch (corner) { // It is important that the pieces are stored in clockwise order case HexagonPiece.Edge.BottomLeft: return(new HexagonGroup(grid[x][y], grid[x][y - 1], grid[x - 1][y2 - 1])); case HexagonPiece.Edge.BottomRight: return(new HexagonGroup(grid[x][y], grid[x + 1][y2 - 1], grid[x][y - 1])); case HexagonPiece.Edge.Left: return(new HexagonGroup(grid[x][y], grid[x - 1][y2 - 1], grid[x - 1][y2])); case HexagonPiece.Edge.Right: return(new HexagonGroup(grid[x][y], grid[x + 1][y2], grid[x + 1][y2 - 1])); case HexagonPiece.Edge.TopLeft: return(new HexagonGroup(grid[x][y], grid[x - 1][y2], grid[x][y + 1])); case HexagonPiece.Edge.TopRight: return(new HexagonGroup(grid[x][y], grid[x][y + 1], grid[x + 1][y2])); default: return(new HexagonGroup()); } }
/// <summary> /// If points resides inside the grid, locate the group that are closest to the point /// </summary> /// <param name="point"></param> /// <param name="group">false;null true; sets group to the found one.</param> /// <returns>false; point is outside of grid, true; inside.</returns> public bool TryGetGroup(Vector2 point, out HexagonGroup group) { if (point.x <= 0f || point.x >= gridSize.x || point.y <= 0f || point.y >= gridSize.y) { group = new HexagonGroup(); return(false); } // Calculate the row and column indices of the hexagon piece that the point resides inside GetCoordinatesFrom(point, out int x, out int y); // Find the hexagon piece's corner that is closest to the point HexagonPiece.Edge corner = grid[x][y].HandleEdge(grid[x][y].GetClosestEdge(point - (Vector2)grid[x][y].transform.localPosition)); group = GetGroupAtCorner(x, y, corner); return(true); }