Exemplo n.º 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();
    }
Exemplo n.º 2
0
        private static MazeCell GetRandomEmptyCell()
        {
            MazeCell cell = null;                 // no item is spawned on the start and end positions

            while ((cell == null || cell.HasItem) || ((cell.GetY() == 0 && cell.GetX() == 0) ||
                                                      ((cell.GetY() == maze.Size.y - 1 && cell.GetX() == maze.Size.x - 1))))
            {
                cell = maze.GetRandomCell();
            }
            return(cell);
        }