示例#1
0
    /// <summary>
    /// Generate a maze by Prim
    /// </summary>
    private void PrimMaze(Graph g, GraphNode s)
    {
        Dictionary <GraphNode, bool> NodeIsVisited = new Dictionary <GraphNode, bool>();

        foreach (GraphNode n in g.nodes)
        {
            NodeIsVisited.Add(n, false);
        }
        NodeIsVisited[s] = true;

        List <MazeEdge> walls = new List <MazeEdge>();

        foreach (GraphEdge e in s.edges)
        {
            walls.Add((MazeEdge)e);
        }

        while (walls.Count > 0)
        {
            MazeEdge e = walls[Random.Range(0, walls.Count)];
            walls.Remove(e);

            if (NodeIsVisited[e.to] == false)
            {
                NodeIsVisited[e.to] = true;
                e.isWall            = false;
                ((MazeEdge)g.GetEdge(e.to, e.from)).isWall = false;

                foreach (MazeEdge me in e.to.edges)
                {
                    walls.Add(me);
                }
            }
        }
    }
示例#2
0
 private bool needToDrawWall(MazeEdge mazeEdge)
 {
     if (mazeEdge == null)
     {
         return(true);
     }
     return(mazeEdge.Wall);
 }
示例#3
0
            private static int tallyEdgeCount(MazeEdge edge)
            {
                if (edge != null)
                {
                    return(edge.Wall ? 1 : 0);
                }

                return(0);
            }
示例#4
0
 private bool needToDrawCorner(MazeEdge edge1, MazeEdge edge2)
 {
     return(needToDrawWall(edge1) && needToDrawWall(edge2));
 }
示例#5
0
 private void DoNextGenerationStep(List<MazeCell> activeCells)
 {
     int currentIndex = activeCells.Count - 1;
     MazeCell currentCell = activeCells[currentIndex];
     if (currentCell.IsFullyInitialized) {
         activeCells.RemoveAt(currentIndex);
         return;
     }
     MazeDirection direction = currentCell.RandomUninitializedDirection;
     IntVector2 coordinates = currentCell.coordinates + direction.ToIntVector2();
     if (ContainsCoordinates(coordinates)) {
         MazeCell neighbor = GetCell(coordinates);
         if (neighbor == null) {
             neighbor = CreateCell(coordinates);
             CreatePassage(currentCell, neighbor, direction);
             activeCells.Add(neighbor);
         }
         else if (currentCell.room == neighbor.room)
         {
             CreatePassageInSameRoom(currentCell, neighbor, direction);
             MazeEdge newEdge = new MazeEdge(currentCell, neighbor, 0);
             edgeList.Add(newEdge);
         }
         else {
             CreateWall(currentCell, neighbor, direction);
             MazeEdge newEdge = new MazeEdge(currentCell, neighbor, 1);
             edgeList.Add(newEdge);
         }
     }
     else {
         CreateWall(currentCell, null, direction);
     }
 }
示例#6
0
 private void CreatePassage(MazeCell cell, MazeCell otherCell, MazeDirection direction)
 {
     MazePassage prefab = Random.value < doorProbability ? doorPrefab : passagePrefab;
     MazePassage passage = Instantiate(prefab) as MazePassage;
     passage.Initialize(cell, otherCell, direction);
     passage = Instantiate(prefab) as MazePassage;
     if (passage is MazeDoor)
     {
         otherCell.Initialize(CreateRoom(cell.room.settingsIndex));
         MazeEdge newEdge = new MazeEdge(cell, otherCell, 2);
         edgeList.Add(newEdge);
     }
     else
     {
         otherCell.Initialize(cell.room);
         MazeEdge newEdge = new MazeEdge(cell, otherCell, 0);
         edgeList.Add(newEdge);
     }
     passage.Initialize(otherCell, cell, direction.GetOpposite());
 }