Example #1
0
    private IEnumerator GenerateMazeCoroutine()
    {
        bool[,] boolMaze = new bool[maze.GetLength(0), maze.GetLength(1)];
        bool isFirstRun = true;

        while (true)
        {
            // Find the first open spot
            MazeCell current = FindOpenMazeCell(boolMaze);

            // base condition:
            // no open stops found algorithm finishes
            if (current == null)
            {
                break;
            }

            // Debug.Log("Test1: Current: " + x_c + ", " + y_c);
            current.SetWallColor(Color.red);

            yield return(new WaitForSeconds(GameManager.instance.GetMapGenerationSpeed()));

            if (!isFirstRun)
            {
                List <MazeCell> closedMazeCells = FindAdjacentClosedMazeCells(boolMaze, current);
                int             closedCellIndex = Random.Range(0, closedMazeCells.Count);
                MazeCell        next            = closedMazeCells[closedCellIndex];
                MazeCell.ConnectMazeCells(current, next);
            }

            List <MazeCell> openMazeCells = FindAdjacentOpenMazeCells(boolMaze, current);
            while (openMazeCells.Count != 0)
            {
                yield return(new WaitForSeconds(GameManager.instance.GetMapGenerationSpeed()));

                int nextMazeCellIndex = Random.Range(0, openMazeCells.Count);

                MazeCell next = openMazeCells[nextMazeCellIndex];

                MazeCell.ConnectMazeCells(current, next);

                boolMaze[current.GetX(), current.GetY()] = true;
                current.SetWallColor(Color.gray);

                current = next;
                current.SetWallColor(Color.red);
                openMazeCells = FindAdjacentOpenMazeCells(boolMaze, current);
            }

            boolMaze[current.GetX(), current.GetY()] = true;
            current.DisableCenterWall();
            current.SetWallColor(Color.gray);
            isFirstRun = false;
        }

        GameManager.instance.CreatePlayer();
    }