Creating mazes procedurally
This is a completely new recipe oriented toward having fun while creating maps and levels procedurally. The main recipe works by creating a maze completely procedurally. Furthermore, we will explore a gray area, where both level design and procedurally generated content meet.
Getting ready
In this recipe, it is important to understand the concepts of Binary Space Partitioning and the Breadth-first Search algorithm learned in Chapter 2, Navigation.
How to do it…
We will implement two classes, one for the nodes to be partitioned and one for holding all the nodes and the maze representation, as follows:
Create the
BSPNode
class and its members:using UnityEngine; [System.Serializable] public class BSPNode { public Rect rect; public BSPNode nodeA; public BSPNode nodeB; }
Implement the class constructor:
public BSPNode(Rect rect) { this.rect = rect; nodeA = null; nodeB = null; }
Define the function for splitting the node into two subregions:
public void...