private void GenerateMaze()
        {
            MazeNode           startingNode = grid[0, 0];
            List <WallNode>    openSet      = new List <WallNode>();
            HashSet <MazeNode> closedSet    = new HashSet <MazeNode>();

            openSet.AddRange(startingNode.Walls);
            closedSet.Add(startingNode);
            while (openSet.Count != 0)
            {
                int      randomIndex           = UnityEngine.Random.Range(0, openSet.Count);
                WallNode currentWall           = openSet[randomIndex];
                MazeNode neighborOfCurrentWall = currentWall.Neighbor;
                if (neighborOfCurrentWall != null && !closedSet.Contains(neighborOfCurrentWall))
                {
                    currentWall.Parent.RemoveWall(currentWall);
                    int oppositeWallToRemove = -(int)currentWall.Orientation;
                    neighborOfCurrentWall.RemoveWall(oppositeWallToRemove);
                    openSet.AddRange(neighborOfCurrentWall.Walls);
                    closedSet.Add(neighborOfCurrentWall);
                }
                openSet.Remove(currentWall);
            }
        }
Beispiel #2
0
 //Two overloaded remove wall functions , one for when we already have the wall we want to remove,
 //the other for when we know only the orientation of the wall that should be removed.
 //Helps in creating connections(paths) between nodes faster.
 public void RemoveWall(WallNode toRemove)
 {
     walls.Remove(toRemove);
 }