This week, work on the creation of rooms was done. The first step in this process was to work on the StructureHelper class.

The StructureHelper class provides various methods to assist with graph traversal and spatial calculations.

The method TraverseGraphToExtractLowestLeaves uses breadth-first search (BFS) to traverse a hierarchical graph structure starting from a given parent node. Its purpose is to identify and return all the leaf nodes. If the parent node itself is a leaf, it is immediately returned. The traversal uses a queue to manage nodes that need to be checked.

For spatial calculations, GenerateBottomLeftCornerBetween and GenerateTopRightCornerBetween are designed to create random points within specified boundaries. These methods accept two boundary points that define a rectangular area, along with a point modifier and offset for additional control. The bottom-left and top-right methods differ in their treatment of these boundaries, allowing for specific placements suited to their respective corners.

The CalculateMiddlePoint method determines the midpoint between two given points in space. By averaging their coordinates and converting the result to integers, this method returns a midpoint in integer form.

Finally, the RelativePosition enum defines spatial relationships such as Up, Down, Left, and Right.

Following this, a RoomGenerator class was made.

The RoomGenerator class is responsible for generating rooms within a the bounds of the dungeon and adjusting the position of them by modifying their corner points.

The constructor of the RoomGenerator class initializes the room generation process, taking in three parameters: maxIterations, roomLengthMin, and roomWidthMin. These values can be used for controlling the number of iterations and the minimum dimensions of rooms during the generation process.

The method GenerateRoomsInGivenSpaces takes a list of Node objects representing available room spaces and modifies their corner positions to generate rooms. The method uses two modifiers (roomBottomCornerModifier and roomTopCornerModifier) to control how far the room’s corners are adjusted from the original space’s boundaries, along with an offset to prevent the room from being placed too close to the edges of the space.

Once the new bottom left and top right corners are generated, the method recalculates the other two corners based on the new corner positions. These adjustments ensure that the room’s boundaries are correctly defined in all directions. After updating the corner positions, the method casts the space to a RoomNode and adds it to a list, which will be returned once all spaces have been processed.

A new method was then made in the RoomGenerator class.

It divides a large area into smaller rooms or spaces, generates rooms within these spaces, and returns a list of nodes representing the rooms. This method combines the use of BSP for space division and a custom room generation algorithm to populate the dungeon layout with rooms. The final output is a list of Node objects that can be used for further processing.

Finally, a method in the DungeonCreator class was made.

This method constructs a 3D mesh representing a rectangular floor or area, using two 2D corner points. The method then creates a GameObject to hold the mesh and assigns a material and collider to it.

Here is the final result:


Reference List

Sunny Valley Studio. (2019). Unity 3d procedural dungeon generator. [YouTube playlist]. Available from: https://www.youtube.com/playlist?list=PLcRSafycjWFfEPbSSjGMNY-goOZTuBPMW [accessed 13 November 2024].

Updated: