/// <summary>Creates a grid full of maze cells based on given width and heigth values</summary> private void CreateMazeCells() { Cells = new MazeCell[width, height]; Camera camera = Camera.main; Bounds cellBounds = prefabMazeCell.GetComponent <SpriteRenderer>().bounds; Vector3 cellScale = prefabMazeCell.transform.localScale; Vector3 bottomLeft = camera.ScreenToWorldPoint(Vector3.zero); Vector3 topLeft = camera.ScreenToWorldPoint(new Vector3(0, Screen.height)); Vector3 bottomRight = camera.ScreenToWorldPoint(new Vector3(Screen.width, 0)); float screenWidth = (bottomLeft - bottomRight).magnitude; float screenHeight = (topLeft - bottomLeft).magnitude; float cellWidth = cellBounds.size.x; float cellHeight = cellBounds.size.y; float gridWidth = cellWidth * width; float gridHeight = cellHeight * height; //scale cells down if grid width is greater than screen width if (gridWidth > screenWidth) { cellScale *= (screenWidth / gridWidth); cellWidth = cellBounds.size.x * cellScale.x; cellHeight = cellBounds.size.y * cellScale.y; gridWidth = cellWidth * width; gridHeight = cellHeight * height; } //scale cells down if grid height is greater than screen height after first reduction if (gridHeight > screenHeight) { cellScale *= (screenHeight / gridHeight); cellWidth = cellBounds.size.x * cellScale.x; cellHeight = cellBounds.size.y * cellScale.y; gridWidth = cellWidth * width; gridHeight = cellHeight * height; } //scale cells up if the screen size is bigger than the grid size if (gridWidth < screenWidth && gridHeight < screenHeight) { //use lowest percentage of width and height to scale cells up cellScale *= Mathf.Min((screenHeight / gridHeight), (screenWidth / gridWidth)); cellWidth = cellBounds.size.x * cellScale.x; cellHeight = cellBounds.size.y * cellScale.y; gridWidth = cellWidth * width; gridHeight = cellHeight * height; } //assign left and bottom margin values based on leftover space between screen and grid float left = (screenWidth - gridWidth) * 0.5f; float bottom = (screenHeight - gridHeight) * 0.5f; //set origin of grid at bottom left, including compensation for pivot of cell being at the center Vector3 origin = bottomLeft + Vector3.Scale(cellBounds.extents, cellScale); //create the grid for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { float xPos = left + x * cellWidth; float yPos = bottom + y * cellWidth; Cells[x, y] = CreateMazeCell(origin.x + xPos, origin.y + yPos, x, y, cellScale); } } }
/// <summary> /// Returns the neighbour to the right of given cell. Will return null if the neighbour was outside of the grid /// </summary> /// <param name="cell"></param> /// <returns></returns> public MazeCell GetRightNeighbour(MazeCell cell) { //only returns the top maze cell relative to given x,y coordinates if x coordinate is inside the maze x dimension return(cell.MazeX + 1 < Cells.GetLength(0) ? Cells[cell.MazeX + 1, cell.MazeY] : null); }
/// <summary> /// Returns the neighbour below given cell. Will return null if the neighbour was outside of the grid /// </summary> /// <param name="cell"></param> /// <returns></returns> public MazeCell GetBottomNeighbour(MazeCell cell) { //only returns the top maze cell relative to given x,y coordinates if y coordinate is inside the maze y dimension return(cell.MazeY - 1 >= 0 ? Cells[cell.MazeX, cell.MazeY - 1] : null); }
/// <summary> /// Returns the neighbour to the left of given cell. Will return null if the neighbour was outside of the grid /// </summary> /// <param name="cell"></param> /// <returns></returns> public MazeCell GetLeftNeighbour(MazeCell cell) { //only returns the top maze cell relative to given x,y coordinates if x coordinate is inside the maze x dimension return(cell.MazeX - 1 >= 0 ? Cells[cell.MazeX - 1, cell.MazeY] : null); }
/// <summary> /// Returns the neighbour above given cell. Will return null if the neighbour was outside of the grid /// </summary> /// <param name="cell"></param> /// <returns></returns> public MazeCell GetTopNeighbour(MazeCell cell) { //only returns the top maze cell relative to given x,y coordinates if y coordinate is inside the maze y dimension return(cell.MazeY + 1 < Cells.GetLength(1) ? Cells[cell.MazeX, cell.MazeY + 1] : null); }