Dungeon generation
- Game Development course
- Advanced
- Lesson 47/71
- 4 min
Learn a simple dungeon generation algorithm.
Dungeon Generation Logic
Foundational to roguelikes, dungeons consist of rooms connected by corridors. The generator first scatters non-overlapping rooms of varying sizes across the grid.
public struct Room {
public int x, y, width, height;
public bool Intersects(Room other) {
return !(x + width < other.x || other.x + other.width < x);
}
}Intersection checks ensure rooms maintain separation rather than colliding into messy overlaps.
Connecting Rooms with Corridors
After computing room centers, L-shaped horizontal and vertical corridors are carved. A loop converts wall cells into traversable floor tiles along the path.
void CreateCorridor(Vector2Int from, Vector2Int to) {
// Carves horizontal then vertical corridor tiles
for (int x = from.x; x != to.x; x += (to.x > from.x ? 1 : -1)) {
map[x, from.y] = 1;
}
}Connecting consecutive rooms guarantees an unbroken dungeon traversal network.
Practice Dungeon room count check
Define a Start method. Inside, declare int odaSayisi = 6; and print "Olusturulan oda: " + odaSayisi to the console.
Starter code
using UnityEngine;
public class DungeonTester : MonoBehaviour
{
// Start icinde odaSayisi tanimla ve yazdir
}Sample solution
using UnityEngine;
public class DungeonTester : MonoBehaviour
{
void Start()
{
int odaSayisi = 6;
Debug.Log("Olusturulan oda: " + odaSayisi);
}
}Expected output
Olusturulan oda: 6
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the second essential step in a dungeon generator after scattering rooms across the map?
- Saving shader codes to disk
- Deleting all rooms from the map
- Carving corridors to connect the rooms to each other Correct answer
- Setting the game engine graphics to minimum
Explanation: Once rooms are placed, corridors must be carved to connect them into a walkable network.
2 Instantiate new room
Room newRoom = new ____(x, y, w, h);
Room newRoom = new Room(x, y, w, h);
Explanation: The constructor of the 'Room' struct is called to define a new dungeon room.
3 Dungeon generation terms
| Term | Meaning |
|---|---|
Room |
Enclosed room space within the dungeon |
Corridor |
Narrow passageway connecting rooms |
Intersects |
Checks whether rooms overlap |
Center |
Room midpoint targeted by corridors |