Skip to content

DungeonGenerator

namespace Griff.Modules.Dungeons;
public sealed class DungeonGenerator : Component, IHearsMessages

Fills its Tilemap with a dungeon made from a Seed, with no code: rooms of different sizes joined by corridors, walled in. The players start in the first room, the Exit waits in the room furthest from it, and the Spawns (monsters, potions, gold) are scattered through the other rooms. Reaching the exit makes the next floor. The same seed always makes the same dungeon; 0 makes a new one every game.

With GenerateOnPlay the map’s tiles are replaced by the first floor as the game starts; untick it to play the map as painted, with the floors after it generated. Corridors and rooms are FloorTile (or left empty) and the cells around them WallTile, so a grid player steps through the rooms and corridors and is stopped by the walls. Spawns go inside a “Spawns” entity in the dungeon’s, which the next floor clears out. The tiles and Floor are saved with the scene, so a rewind goes back to the floor it was on. In a network game the host makes the floors.

This is a component: its saved properties are on its component page.

public int CorridorWidth { get; set; }

Cells across a corridor.

public bool DescendOnExit { get; set; }

A player stepping onto the Exit’s cell goes down to the next floor. Untick it to go down only when told to.

public string? Exit { get; set; }

The entity put in the middle of the room furthest from the first, e.g. “Stairs”: a player stepping onto its cell goes down to the next floor (see DescendOnExit).

public ValueTuple<int, int>? ExitCell { get; }

The cell the Exit was put in, or null before a floor is made.

public bool FillWalls { get; set; }

Fill everything outside the rooms and corridors with wall. Untick it to wall them in with a single row and leave the rest empty.

public int Floor { get; set; }

The floor the players are on, from 1 once the game has started; 0 before. Saved with the scene, so a rewind takes it back.

public string? FloorMessage { get; set; }

A message sent with 1 as its data as each floor after the first is made, e.g. for a sound player. (A Counter counting its Output “Floor” shows the floor.)

public string? FloorSound { get; set; }

A sound played as the players go down to the next floor, e.g. “Sounds/stairs.wav”.

public int FloorTile { get; set; }

The tile for the floors of rooms and corridors; -1 leaves them empty. List it in the tilemap’s Background Tiles so nothing collides with it.

public bool GenerateOnPlay { get; set; }

Make the first floor when the game starts. Untick it to play the map as painted; the floors after it are still made.

public int Height { get; set; }

Cells down.

public int Loops { get; set; }

Corridors added between rooms that are already joined, so there’s more than one way round. 0 makes every way the only one.

public int MaxRoomSize { get; set; }

The most cells across or down a room’s floor can be.

public int MinRoomSize { get; set; }

The fewest cells across or down a room’s floor can be.

public string? NextFloorMessage { get; set; }

A message that makes the next floor when it’s sent, e.g. “Next Floor” from a Button or a script. Empty for none.

public string? Players { get; set; }

Entities put in the middle of the first room as each floor is made, by name, separated by commas, e.g. “Player”.

public IReadOnlyList<DungeonRoom> RoomList { get; }

The rooms of the floor last made here, the first first (where the players start). Empty before one is made.

public int Rooms { get; set; }

How many rooms to make. Fewer fit in a small map: as many as there’s room for.

public int Seed { get; set; }

Picks the dungeon: the same number always makes the same one (each floor after the first its own). 0 makes a new one each game.

public IReadOnlyList<string> Spawns { get; set; }

Prefabs scattered through the rooms (never the first) as each floor is made, one per line: its name and how many, e.g. “Enemy 4”, “Potion 1-2” (a number picked between them), “Gold” (one).

public int UsedSeed { get; }

The seed the floor was last made from: the one picked when Seed is 0, then one of its own for each floor.

public int WallTile { get; set; }

The tile the walls are made of.

public int Width { get; set; }

Cells across.

public void Generate()

Makes this floor again, at the start of the next frame: a different one when Seed is 0.

public void NextFloor()

Goes down to the next floor, at the start of the next frame: a new dungeon, the players in its first room.

public DungeonRoom? RoomAt(int x, int y)

The room cell (x, y) is in, or null in a corridor, a wall or outside.

public event Action<int>? Generated

Raised as each floor is made, with its number.

public const string SpawnsName

The name of the entity inside the dungeon’s that its spawns go in.