2D Tile Based Random Map Generator



Disclaimer:

This is a humble approach for creating a random map generator for my game. Please notice that I did not try my best here and this could definitely be way better...I just wanted to have some fun while creating a random map generator. I will provide links to the code used here if you want to take a look. Also, feedback is really appreciated :).

My approach for this tool was the following:

  1. Create an X times Y grid.
  2. Fill the grid with random objects(obstacles).
  3. Based on how many obstacles you want, remove the extra ones.
  4. Shuffle the grid.


Here's the code that does that:

For shuffling the grid:

https://gist.github.com/MerliMejia/26a9a61efc9cea80155ea129f83619d0#file-mapgenerator-cs-L54

For generating the random data on the grid:

https://gist.github.com/MerliMejia/26a9a61efc9cea80155ea129f83619d0#file-mapgenerator-cs-L71

You can use Unity's Gizmos system to get an early visualization:


https://gist.github.com/MerliMejia/26a9a61efc9cea80155ea129f83619d0#file-mapgenerator-cs-L175

You can use Scriptable Objects to know what to draw and how many it should draw:


https://gist.github.com/MerliMejia/26a9a61efc9cea80155ea129f83619d0#file-obstacles-cs-L7

After that, you just need to draw the tiles based on the random grid data and that's it!

https://gist.github.com/MerliMejia/26a9a61efc9cea80155ea129f83619d0#file-mapgenerator-cs-L148

Here's all the code used in this tool:

https://gist.github.com/MerliMejia/26a9a61efc9cea80155ea129f83619d0

Leave a comment

Log in with itch.io to leave a comment.