// Method to create the base grid; all needed cells and walls private IEnumerator GenerateGrid() { cells = new Cell[width * height]; // Definíng array storing all cell objects Vector2[] cellPositions = new Vector2[width * height]; // Array to store all cell positions in grid float cellPosX; float cellPosY; int addWidth = 0; // Variable to help indexing the cell array correctly // Nested loop to create the grid with no walls missing for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { cellPosX = i * w - (w * width / 2) + w / 2; // x coordinate of current cell to be placed cellPosY = j * w - (w * height / 2) + w / 2; // y coordinate of current cell to be placed GameObject currBox = Instantiate(blockPrefab, new Vector3(cellPosX, cellPosY, 0), Quaternion.identity); cellPositions[i + addWidth] = new Vector2(cellPosX, cellPosY); currBox.transform.parent = blockHolder.transform; // Instantiating walls for the current cell GameObject leftwall = Instantiate(wall, new Vector3(currBox.transform.position.x - w / 2, currBox.transform.position.y, -0.01f), Quaternion.identity); GameObject topwall = Instantiate(wall, new Vector3(currBox.transform.position.x, currBox.transform.position.y + w / 2, -0.01f), Quaternion.Euler(new Vector3(0, 0, 90))); GameObject rightwall = Instantiate(wall, new Vector3(currBox.transform.position.x + w / 2, currBox.transform.position.y, -0.01f), Quaternion.identity); GameObject botwall = Instantiate(wall, new Vector3(currBox.transform.position.x, currBox.transform.position.y - w / 2, -0.01f), Quaternion.Euler(new Vector3(0, 0, 90))); // Add new cell to cell array cells[i + addWidth] = new Cell(i, j, currBox); // Add walls to cell object cells[i + addWidth].leftWall = leftwall; cells[i + addWidth].topWall = topwall; cells[i + addWidth].rightWall = rightwall; cells[i + addWidth].botWall = botwall; // Parent the cell walls cells[i + addWidth].leftWall.transform.parent = wallHolder.transform; cells[i + addWidth].topWall.transform.parent = wallHolder.transform; cells[i + addWidth].rightWall.transform.parent = wallHolder.transform; cells[i + addWidth].botWall.transform.parent = wallHolder.transform; nrOfCreatedCells++; // Yields after genYieldLimit cells to not have the whole application window "freeze" too long for large mazes if (nrOfCreatedCells % genYieldLimitGrid == 0) { yield return(null); } } addWidth += width; } coinCreator.SetSpawnPoints(cellPositions); // Give coinCreator all cell positions (needed to position coins) coinCreator.GenerateCoins(); StartCoroutine(DepthFirstSearch()); // Start main algorithm }