Implementing the constructive algorithm for dungeons and islands
One of the most-used world structures in games, besides open worlds, are dungeons. In this recipe, we will use a technique that will allow us to create both.
Getting ready
This technique works easily with a grid graph, like the one we used with the DFS maze builder. However, we will experiment with a different approach to show you how to handle space partitioning and size storage.
How to do it...
We will need to create two different classes: one for handling the nodes, and one for handling the whole tree and high-level operations.
Let's start with the node file:
- Create a new class named
DungeonNode2D
:
using UnityEngine; using System.Collections.Generic; [System.Serializable] public class DungeonNode2D { // next steps }
- Define its member variables:
public Rect area; public Rect block; public Dungeon2D root; public DungeonNode2D left; public DungeonNode2D right; protected int depth;
- Implement its constructor for initialization:
public...