private void BuildLevelGraph() { List <GraphWall> wallList = new List <GraphWall>(); GraphNode startingNode = levelGraph[0, 0]; foreach (WallDirection wall in startingNode.WallsRemaining) { wallList.Add(new GraphWall(startingNode, wall)); } while (wallList.Count > 0) { GraphWall currentWall = wallList[Random.Range(0, wallList.Count)]; GraphNode sourceNode = currentWall.SourceNode; GraphNode destinationNode = GetDestinationNode(currentWall); if (sourceNode.TimesVisited < 2 || destinationNode.TimesVisited < 2) { sourceNode.WallsRemaining.Remove(currentWall.Direction); destinationNode.WallsRemaining.Remove(currentWall.OppositeDirection()); if (destinationNode.TimesVisited == 0) { foreach (WallDirection direction in destinationNode.WallsRemaining) { wallList.Add(new GraphWall(destinationNode, direction)); } } sourceNode.TimesVisited++; destinationNode.TimesVisited++; } wallList.Remove(currentWall); } }
public GraphNode GetDestinationNode(GraphWall wall) { GraphNode sourceNode = wall.SourceNode; Vector2Int coordinates = sourceNode.Coordinates; switch (wall.Direction) { case WallDirection.NORTH: return(levelGraph[coordinates.x, coordinates.y + 1]); case WallDirection.SOUTH: return(levelGraph[coordinates.x, coordinates.y - 1]); case WallDirection.EAST: return(levelGraph[coordinates.x + 1, coordinates.y]); case WallDirection.WEST: return(levelGraph[coordinates.x - 1, coordinates.y]); default: throw new KeyNotFoundException("Destination node called with illegal direction enum: " + wall.Direction); } }