コード例 #1
0
    public void Generate(float shiftX, float shiftY)
    {
        cells = new MazeCell[size_x, size_y];
        walls = new MazeWall[size_x, size_y, 4];

        this.shiftX = shiftX;
        this.shiftY = shiftY;

        List <MazeCell> initializedCells = new List <MazeCell>();

        initializedCells.Add(InitializeCell(0, 7));

        while (initializedCells.Count > 0)
        {
            int      currentIndex = initializedCells.Count - 1;
            MazeCell currentCell  = initializedCells[currentIndex];

            if (!currentCell.hasBeenVisited)
            {
                currentCell.hasBeenVisited = true;
            }

            if (currentCell.allEdgesAssigned)
            {
                initializedCells.RemoveAt(currentIndex);
                continue;
            }

            int dir  = currentCell.GetRandomUnassignedEdge();
            int newX = currentCell.x + Direction[dir, 0];
            int newY = currentCell.y + Direction[dir, 1];

            //Outer Wall or Preventing Loop
            if (newX < 0 || newX >= size_x || newY < 0 || newY >= size_y)
            {
                currentCell.SetEdge(dir, -1);
                if (!(currentCell.x == 0 && currentCell.y == 7) && !(currentCell.x == 14 && currentCell.y == 7))
                {
                    walls[currentCell.x, currentCell.y, dir]      = Instantiate(mazeWallPrefab) as MazeWall;
                    walls[currentCell.x, currentCell.y, dir].name = DirName[dir];
                    walls[currentCell.x, currentCell.y, dir].Initialize(currentCell, null, rotations[dir]);
                }
                else if (currentCell.x == 0 && currentCell.y == 7)
                {
                }
            }
            else if (cells[newX, newY] == null)
            {
                currentCell.SetEdge(dir, 1);
                initializedCells.Add(InitializeCell(newX, newY));
                cells[newX, newY].SetEdge(inverseDirection[dir], 1);
            }
            else if (cells[newX, newY].hasBeenVisited)
            {
                currentCell.SetEdge(dir, -1);
                cells[newX, newY].SetEdge(inverseDirection[dir], -1);
            }
        }
        for (int i = 0; i < size_x; i++)
        {
            for (int j = 0; j < size_y; j++)
            {
                cells[i, j].hasBeenVisited = false;
            }
        }
        Solver(14, 7);
        for (int i = 0; i < size_x; i++)
        {
            for (int j = 0; j < size_y; j++)
            {
                cells[i, j].hasBeenVisited = false;
            }
        }

        for (int i = 0; i < size_x; i++)
        {
            for (int j = 0; j < size_y; j++)
            {
                for (int k = 0; k < 4; k++)
                {
                    if (cells[i, j].CellEdges[k] == -1 && !(i == 0 && k == 3) && !(i == size_x - 1 && k == 1) && !(j == 0 && k == 2) && !(j == size_y - 1 && k == 0))
                    {
                        walls[i, j, k]      = Instantiate(mazeWallPrefab) as MazeWall;
                        walls[i, j, k].name = DirName[k];
                        walls[i, j, k].Initialize(cells[i, j], cells[i + Direction[k, 0], j + Direction[k, 1]], rotations[k]);
                    }
                }
            }
        }

        boundaryN = Instantiate(boundaryNorthPrefab as Boundary);
        boundaryN.Generate((shiftX * size_x * 3), (shiftY * size_y * 3), this.transform);
        boundaryE = Instantiate(boundaryEastPrefab as Boundary);
        boundaryE.Generate((shiftX * size_x * 3), (shiftY * size_y * 3), this.transform);
        boundaryS = Instantiate(boundarySouthPrefab as Boundary);
        boundaryS.Generate((shiftX * size_x * 3), (shiftY * size_y * 3), this.transform);
        boundaryW = Instantiate(boundaryWestPrefab as Boundary);
        boundaryW.Generate((shiftX * size_x * 3), (shiftY * size_y * 3), this.transform);
    }
コード例 #2
0
    public IEnumerator GenerateNext(float shiftX, float shiftY, bool North, bool South, bool East, bool West, bool Treasure, string mazeKey)
    {
        WaitForSeconds delay = new WaitForSeconds(0f);

        cells = new MazeCell[size_x, size_y];
        walls = new MazeWall[size_x, size_y, 4];

        this.shiftX = shiftX;
        this.shiftY = shiftY;

        this.mazeKey = mazeKey;
        List <MazeCell> initializedCells = new List <MazeCell>();

        initializedCells.Add(InitializeCell(0, 7));

        boundaryN = Instantiate(boundaryNorthPrefab as Boundary);
        boundaryN.Generate((shiftX * size_x * 3), (shiftY * size_y * 3), this.transform);
        boundaryE = Instantiate(boundaryEastPrefab as Boundary);
        boundaryE.Generate((shiftX * size_x * 3), (shiftY * size_y * 3), this.transform);
        boundaryS = Instantiate(boundarySouthPrefab as Boundary);
        boundaryS.Generate((shiftX * size_x * 3), (shiftY * size_y * 3), this.transform);
        boundaryW = Instantiate(boundaryWestPrefab as Boundary);
        boundaryW.Generate((shiftX * size_x * 3), (shiftY * size_y * 3), this.transform);
        while (initializedCells.Count > 0)
        {
            int      currentIndex = initializedCells.Count - 1;
            MazeCell currentCell  = initializedCells[currentIndex];

            if (!currentCell.hasBeenVisited)
            {
                currentCell.hasBeenVisited = true;
            }

            if (currentCell.allEdgesAssigned)
            {
                initializedCells.RemoveAt(currentIndex);
                continue;
            }

            int dir  = currentCell.GetRandomUnassignedEdge();
            int newX = currentCell.x + Direction[dir, 0];
            int newY = currentCell.y + Direction[dir, 1];

            //Outer Wall or Preventing Loop
            if (newX < 0 || newX >= size_x || newY < 0 || newY >= size_y)
            {
                currentCell.SetEdge(dir, -1);
                if (!(currentCell.x == 0 && currentCell.y == 7 && East) && !(currentCell.x == 14 && currentCell.y == 7 && West) && !(currentCell.x == 7 && currentCell.y == 0 && South) && !(currentCell.x == 7 && currentCell.y == 14 && North))
                {
                    walls[currentCell.x, currentCell.y, dir]      = Instantiate(mazeWallPrefab) as MazeWall;
                    walls[currentCell.x, currentCell.y, dir].name = DirName[dir];
                    walls[currentCell.x, currentCell.y, dir].Initialize(currentCell, null, rotations[dir]);
                }
            }
            else if (cells[newX, newY] == null)
            {
                currentCell.SetEdge(dir, 1);

                yield return(delay);

                initializedCells.Add(InitializeCell(newX, newY));
                cells[newX, newY].SetEdge(inverseDirection[dir], 1);
            }
            else if (cells[newX, newY].hasBeenVisited)
            {
                currentCell.SetEdge(dir, -1);
                cells[newX, newY].SetEdge(inverseDirection[dir], -1);
            }
        }
        for (int i = 0; i < size_x; i++)
        {
            for (int j = 0; j < size_y; j++)
            {
                cells[i, j].hasBeenVisited = false;
            }
        }
        Solver(14, 7);
        for (int i = 0; i < size_x; i++)
        {
            for (int j = 0; j < size_y; j++)
            {
                cells[i, j].hasBeenVisited = false;
            }
        }

        for (int i = 0; i < size_x; i++)
        {
            for (int j = 0; j < size_y; j++)
            {
                for (int k = 0; k < 4; k++)
                {
                    if (cells[i, j].CellEdges[k] == -1 && !(i == 0 && k == 3) && !(i == size_x - 1 && k == 1) && !(j == 0 && k == 2) && !(j == size_y - 1 && k == 0))
                    {
                        walls[i, j, k]      = Instantiate(mazeWallPrefab) as MazeWall;
                        walls[i, j, k].name = DirName[k];
                        walls[i, j, k].Initialize(cells[i, j], cells[i + Direction[k, 0], j + Direction[k, 1]], rotations[k]);
                    }
                }
            }
        }
        for (int i = 0; i < size_x; i++)
        {
            for (int j = 0; j < size_y; j++)
            {
                for (int k = 0; k < 4; k++)
                {
                    if (cells[i, j].CellEdges[k] == 1 &&
                        !(i == 7 && j == 0 && k == 0) &&
                        !(i == 14 && j == 7 && k == 1) &&
                        !(i == 7 && j == 14 && k == 2) &&
                        !(i == 0 && j == 7 && k == 3))
                    {
                        if (k == 0)
                        {
                            cells[i, j].neighbor[k] = cells[i, j + 1];
                        }
                        else if (k == 1)
                        {
                            cells[i, j].neighbor[k] = cells[i + 1, j];
                        }
                        else if (k == 2)
                        {
                            cells[i, j].neighbor[k] = cells[i, j - 1];
                        }
                        else if (k == 3)
                        {
                            cells[i, j].neighbor[k] = cells[i - 1, j];
                        }
                    }
                }
            }
        }
        GenerateEnemy();
        if (Treasure)
        {
            GenerateTreasure();
        }
    }