To follow along, create a new Godot project if you haven’t done so yet and copy the following folders from our Procedural Generation demos project: Common
and RandomWalker
. Remember that the file structure is important.
Before moving on you might want to change the default background color to something lighter because our tile set has a dark purple color. You can do so by going to Project > Project Settings… > Rendering > Environment and changing the Default Clear Color option. We’re using the bright cream color from the Pear36 palette: #ffffeb
.
In this lesson we’ll go over the room assets. They’re the building blocks for our randomly generated levels.
In the default empty scene, select 2D Scene in the Scene docker. Save your scene as Rooms.tscn
and remember to save frequently.
Rename your root node to Rooms. We’re going to populate this scene with five different room types. Four of them are non-blocking. Non-blocking rooms are connected to form the path from the start of the level to the end goal.
The fifth room type corresponds to any room that may be a dead-end and stop the player’s progression. These rooms can take any form other than the four listed above. We call them Side rooms.
We use the non-blocking rooms to generate the unobstructed path of a level first. Then, we can mix all room types to fill the rest of the level around that.
We’re going to use a TileMap node for the levels and split it into a grid of rooms. Each room itself is a TileMap, and all rooms have the same pre-defined size.
Here is an example of a room’s skeleton.
In this example, the room tile dimensions are 17x10 but you can have other dimensions too.
With the Rooms scene open, create five new Node2D nodes. We’re going to use them to group our rooms based on the types we described above. The order of the nodes is more important than their names, as we’re going to use their index in the scene tree to assign room types in code.
They should be in this order:
The Side, LR, LRB, LRT, and LRTB nodes act as groups from which to select rooms of different types randomly. With this variation in place, we guarantee that our levels have a wide range of designs. There’s no limit to the number of rooms you can use.
To create your first room, select the Side node in the scene docker and add a TileMap child with Ctrl A. From the FileSystem dock, drag and drop the tileset-prototype.tres
file onto the Tile Set property in the Inspector.
Be sure to also alter the x and y values of the cell Size to 60 pixels to match our assets.
Create your rectangular room using the only available tile. Since it’s a Side room, it can take any shape. It won’t affect the player’s movement through the valid path.
Create a handful of rooms like this, and prepare different types of rooms, including LR, LRB, LRT, and LRTB types. Each time, place the new TileMap node in the corresponding group.
You must have at least one room of each type. Otherwise, the algorithm cannot work. Keep in mind that all these rooms must have the same size, although that size can be anything you want.
At the end of this process, you should end up with a scene tree structure like the one above. Note that the TileMap node names don’t matter as we’re going to select them randomly in the code.