Procedural Dungeon Generation Part1


To generate my dungeons, I use 2 classes :

public class DungeonManager : MonoBehaviour{}
static class DungeonGenerator{}
  • DungeonManager is attached to a GameObject(“__DungeonMaster”) that is only instantiated when the Dungeon scene load. This class is responsible for finding the level information, spawn the dungeon with the DungeonGenerator static class, add monsters and test for victory.
  • DungeonGenerator is a static class that randomize dungeon configuration and instantiate the GameObject

The main method of DungeonGenerator is SpawnDungeon(). This method calculate the dungeon configuration and spawn the walls as GameObject. If you look the step-by-step graphic of the precedent post, you can see each step translated in functions.

// Randomize configuration of dungeon and instantiate it
static public int[,] SpawnDungeon(int _sizeX,int _sizeZ, int _nbrSquareForGeneration)
	int[,] _dungeonMap; // 2D Array of int that will hold the dungeon map information

	_dungeonMap = CreateDungeonSquares(_sizeX,_sizeZ, _nbrSquareForGeneration); // Fill the array with different squares of various size
	_nbrRoom = FindRooms (_dungeonMap,_sizeX, _sizeZ); // Loop through the dungeon map and find the square that form rooms together
	if(_nbrRoom > 1)
		_dungeonMap = CreateDungeonHalls(_dungeonMap, _sizeX,_sizeZ,_nbrRoom);  // Add halls between the created room

	SpawnEnvironment(_dungeonMap, _sizeX, _sizeZ, _nbrRoom); // Create environment(Spider eggs for now)

	_ListOfWallsToCreate = EvaluateWallToBuild(_dungeonMap, _sizeX, _sizeZ); // Loop through the dungeon map to find all the wall that need to be created
	InstantiateDungeonWalls(_ListOfWallsToCreate); // Instantiate all the wall found by the EvaluateWallToBuild function

	return _dungeonMap;

The array int[,] _dungeonMap is the result of the SpawnDungeon() method.

Square creation
The goal of this step is to fill the array with various squares of different size/position. They are written as 1 in the array.

PDG - Step 1

Procedural Dungeon Generation – Step 1

The first function, CreateDungeonSquares(), fill the _dungeonMap with the inputted number of squares of random size at random position. It’s pretty straightforward and just make sure that all the rooms are on the map. It doesn’t matter if rooms overlap, that’s in fact what we want. This will leads to variously shaped room and more randomness.

// Create the inputted number of square on the map
static int[,] CreateDungeonSquares(int _sizeX, int _sizeZ, int _squareNbr)
	int[,] _newMap = new int[_sizeX,_sizeZ]; // Create empty map with imputted dimensions

	for(int i = 0; i < _squareNbr; i++)
		// Calculate random room size and position. Make sure the room is inside the map.
		int _roomSizeX = Random.Range (10,_sizeX/5);
		int _roomSizeZ = Random.Range (10,_sizeZ/5);
		int _roomPosX  = Random.Range (1,_sizeX-_roomSizeX);
		int _roomPosZ  = Random.Range (1,_sizeZ-_roomSizeZ);

		// Add the square to the map. Avoid making square on the external values of the map.
		for(int j = _roomPosX; j < _roomPosX + _roomSizeX; j++)
			for(int k = _roomPosZ; k < _roomPosZ + _roomSizeZ; k++)
				_newMap[j,k] = 1;
	return _newMap;

One thought on “Procedural Dungeon Generation Part1

  1. Pingback: Dungeon Grind – Procedural Dungeon Generation Tutorial | LearningGeek Blog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s