PlayersWorlds.Maps is a helper library that contains algorithms to help generate dungeon and maze maps for your 2D and 3D games.
The basic workflow is:
Add the latest PlayersWorlds.Maps.dll
to your Unity assets folder.
Use the following code to generate the map:
Console.WriteLine(
new GeneratedWorld(RandomSource.CreateFromEnv())
.AddLayer(AreaType.Maze, new Vector(3, 2))
.OfMaze(MazeStructureStyle.Border, new GeneratorOptions() {
MazeAlgorithm = GeneratorOptions.Algorithms.HuntAndKill,
FillFactor = GeneratorOptions.MazeFillFactor.Full
})
.ToMap(Maze2DRendererOptions.RectCells(3, 2))
.AddLayer(area => area.ShallowCopy(cells:
area.Select(cell =>
cell.Tags.Contains(Cell.CellTag.MazeTrail) ?
new Cell(AreaType.Maze) :
new Cell(AreaType.None))))
.OfMaze(MazeStructureStyle.Border, new GeneratorOptions() {
MazeAlgorithm = GeneratorOptions.Algorithms.RecursiveBacktracker,
FillFactor = GeneratorOptions.MazeFillFactor.ThreeQuarters
})
.ToMap(Maze2DRendererOptions.RectCells(2, 1))
.Map()
.Render(new AsciiRendererFactory()));
This will generate a maze-like dungeon map that looks like the following:
▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
▓▓░░░░░░▓▓░░▓▓ ▓▓░░░░░░░░░░▓▓░░░░░░░░░░░░░░░░░░▓▓▓▓
▓▓▓▓▒▒░░▒▒░░▓▓ ▓▓░░▒▒░░▒▒░░▒▒░░▒▒▓▓▓▓▓▓▓▓▓▓▒▒░░▒▒▓▓▓▓
▓▓▓▓░░░░░░▓▓ ▓▓░░▓▓░░▓▓░░░░░░▓▓▓▓ ▓▓▓▓░░░░░░▓▓
▓▓▓▓▓▓▓▓▒▒░░▓▓ ▓▓░░▓▓░░▒▒▓▓▓▓▓▓▓▓ ▓▓▓▓▒▒░░▓▓
▓▓░░░░░░▓▓░░▓▓ ▓▓░░▓▓░░░░░░▓▓ ▓▓▓▓░░▓▓
▓▓░░▒▒░░▒▒░░▓▓ ▓▓░░▒▒▓▓▒▒░░▓▓ ▓▓▓▓▓▓▓▓▒▒░░▓▓
▓▓░░▓▓░░░░░░▓▓ ▓▓░░░░░░▓▓░░▓▓ ▓▓░░░░░░░░░░▓▓
▓▓░░▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓
▓▓░░░░░░░░░░▓▓░░░░░░░░░░▓▓░░░░░░▓▓░░▓▓
▓▓▓▓▓▓▓▓▒▒░░▒▒░░▒▒▓▓▒▒░░▒▒░░▒▒░░▒▒░░▓▓
▓▓▓▓░░░░░░▓▓▓▓▓▓░░░░░░▓▓░░░░░░▓▓
▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
The resulting map
is a Area
instance that contains cells that map to Unity cells. To render the map you
iterate over the cells of the map
and render whatever map objects you want
based on the cell tags, e.g. walls or floor tiles.
The generated maze also contains markers for dead ends that can be a good place to place loot, as well as the markers for the suggested starting and ending cells that have a guaranteed and longest path between them.
Check out the contributing guide.
mono
on Linux../.vscode/tasks.json
, so if
using VSCode derived editor, you can use tasks to build, test, and get a
coverage report.Running the example maze-gen:
mono --debug build/Debug/mazegen/maze-gen.exe
See some notes in the