Let’s talk about how our level generation algorithm works.
We are going to separate the program into two parts:
Doing so allows us to test and debug both parts separately. It can also make the generator work with any kind of assets, not just tile maps. For instance, you could use a complete scene for each room.
Our algorithm is going to start at the top of the level and walk horizontally and downwards. On each step, we update a state data container that constructs a valid path for the player. After that, we copy rooms from the Rooms scene on this path and move on to filling the remaining unused grid cells.
Let’s break this down a little more.
We start with a grid of a given size, say Vector2(8, 6)
. With this, our random walker is first going to pick a starting position on the top row, a Vector2(r, 0)
where r
is a number in the range [0, grid_size.x - 1]
inclusive.
Then, the walker generates the main level path in a loop. While we haven’t reached a y
position greater than grid_size.y
, we:
Once we are at the bottom of the grid, we have a guaranteed path from the level’s start to its end. Then, in another loop, we build the actual level: