I’m cutting my teeth on randomization in games and trying to figure out the trick that games like Binding Of Isacc & Sperlunky use of pre-fab level fragments arranged randomly to create a functional floor/level, something along the lines of this:
A floor will always contain the player, exit and a bonus room (yellow dot). Rooms that are connected are directly connected to each other.
I have a faint idea of how to go about it: Start by creating an multi-dimensional array that holds the tilemap & grid data:
public static var levelLayout:Array =
[[tile,grid],[tile,grid],[tile,grid],[tile,grid]
[tile, grid],[tile,grid],[tile,grid],[tile,grid] etc]
And from there, go through each grid space, roll to see if that spot is nothing, also making rooms nothing if they are isolated and then begin to assign rooms from an shuffled array that contains all the tilemaps/grids.
Am I on the right track? How should I handle the exits? Any help would be appreciated. Cheers
Start by making a perfect maze (a maze where you can get to any point in the maze from anywhere else in the maze). This helps create a network of rooms that are connected in a winnable/solvable way.
There are plenty of tutorials out there for doing this. This one is a little dirty and has a bunch of code you may not need, but it’s good to look through some relevant examples of this to understand different approaches:
http://www.emanueleferonato.com/2008/11/28/perfect-maze-generation-with-as3/
I’ve had good luck with a recursive backtracking algorithm:
http://en.wikipedia.org/wiki/Maze_generation_algorithm#Recursive_backtracker
Consider each cell in the maze to be a room. Once the maze is built, you know which rooms are connected to other rooms, so make each room have a means for either having a door to connect to the next room, or a wall. You probably would want to make like a 4×4 perfect maze.
Ways to spice it up-
You can safely remove deadends from your maze and it will still be perfect. Once the maze is generated, loop through all cells in the maze. Any cell that has 3 walls is a dead end, and you can wall it off completely. Or make it a secret room or whatever.
Add loops to the maze. Randomly connect 2 rooms in the maze that weren’t previously connected. This still keeps the maze perfect, it just makes it a little more open.
This is fun territory, enjoy it 🙂