Computer Games Design

Adventures in Procedural Content Generation - Adam Speers

Computer Games Design

Adventures in Procedural Content Generation - Adam Speers

Unity - Maze

I had decided to investigate building the maze in Unity using C#, how did it go?

Just like with Unreal Engine, to begin we need a construct that will hold a logical representation of our maze, this is initially achieved through the use of an Array. An array is constructed with dimensions derived from the size of the maze we wish to generate, this is then prepopulated with a series of placeholder values. Zeros representing walls and ones representing paths. An algorithm is then applied to walk through the maze and generate paths.

Unity - C#

Unity can support 2D arrays through its use of C#, this makes life a lot easier when considering how to construct our maze. We can simply define the maze as an array of integers Maze[,] and initialise for any dimensions we like. e.g. 21 x17. Here we have our first difference, its much easier to make rectangular mazes in C#.

Maze = new int[width, height];

for (int x = 0; x < width;="" x++)="">

     for (int y = 0; y < height;="">

          Maze[x, y] = 0;

Navigation

Using a 2D array structure for our maze we can easily navigate around the grid by using an offset from our current co-ordinates.

North Cell = (x + 0, y + 1)

East Cell = (x + 1, y + 0)

South Cell = (x + 0, y - 1)

WestCell  = (x - 1, y + 0)

It is also simple to check if the cell we want to navigate to is inside the bounds of the array.

toCheck.x >= 0 && toCheck.y >= 0 && toCheck.x < width="" &&="" tocheck.y=""><>

 

Bibliography

Buck, J. (2015). Mazes for programmers : code your own twisty little passages . Dallas: The Pragmatic Bookshelf.

Next Post

Please follow this link for the next post in the series