Beispiel #1
0
    /// <summary>
    /// Generates the random maze, using Recursive Backtracking algorithm
    /// </summary>
    /// <returns>The current state of the maze</returns>
    public IEnumerator CreateMaze()
    {
        XY  c = PickStartPoint();
        int x = c.x;
        int y = c.y;

        _dungeon.AddToTilesAndTexture(x, y, Tile.CORRIDOR, _mazeColor);
        AddToStack(x, y);

        while (_walls.Count > 0)
        {
            string possiblesDirs = GetPossibleDirections(x, y);
            if (possiblesDirs.Length > 0)
            {
                int dir = Random.Range(0, possiblesDirs.Length);

                switch (possiblesDirs[dir])
                {
                case 'N':
                    _dungeon.AddToTilesAndTexture(x, y + 2, Tile.CORRIDOR, _mazeColor);
                    _dungeon.AddToTilesAndTexture(x, y + 1, Tile.CORRIDOR, _mazeColor);
                    y += 2;
                    break;

                case 'S':
                    _dungeon.AddToTilesAndTexture(x, y - 2, Tile.CORRIDOR, _mazeColor);
                    _dungeon.AddToTilesAndTexture(x, y - 1, Tile.CORRIDOR, _mazeColor);
                    y -= 2;
                    break;

                case 'E':
                    _dungeon.AddToTilesAndTexture(x + 2, y, Tile.CORRIDOR, _mazeColor);
                    _dungeon.AddToTilesAndTexture(x + 1, y, Tile.CORRIDOR, _mazeColor);
                    x += 2;
                    break;

                case 'W':
                    _dungeon.AddToTilesAndTexture(x - 2, y, Tile.CORRIDOR, _mazeColor);
                    _dungeon.AddToTilesAndTexture(x - 1, y, Tile.CORRIDOR, _mazeColor);
                    x -= 2;
                    break;

                default: break;
                }
                AddToStack(x, y);
            }
            else
            {
                PopStack(out x, out y);
            }
        }
        yield return(new WaitForSeconds(0));
    }