Tales from the Dump #2 - Room Generation
Have a second devlog, hot on the heels of the first! This time I wanted to talk a bit about how rooms are generated in Garbageland. Please note that none of this will be revolutionary game design, but I hope it's interesting to you nonetheless!!
When I was first building the concept stage I just plopped each bit of wall and crate manually into the level designer in GMS2. Suffice it to say that is NOT a sustainable way of handling things if I plan to have many many rooms per dungeon. It also had some weird offset issues because GMS2 wants to snap everything to the 0,0 coordinate and I needed it to be slightly offset to center the stage.
First Pass at Walls
With a for loop, and taking into account the width of the room and the individual blocks that make up the wall, I just put in each of the 8 wall segments in red. Then I had code that would selectively close the doorways as necessary. This was a huge chunk of code and I thought, "Surely there is a better way."
My initial thought was "Well, I guess I can make the 4 walls, and then carve out the doorways as necessary instead. That reduces the code to only 8 segments... WAIT." Why not just carve out everything.
Second Pass at Walls
Let's start by filling the whole room with walls in a simple 2 coordinate for loop.
Lookin' good. Sorry Worm, we'll get you out of there in a second! Let's quickly carve out the middle with a for loop that is slightly smaller than the size of the room.
Great! We've now cleared the room in 2 much smaller for loops than were needed to create the 4 individual walls. All that's left is to carve out all four doors as necessary.
We've now reduced the number of operations by half, and everything looks exactly like it did in the concept stage!
Now Let's Add Some Blocks
All that's left is adding blocks to the rooms in various fun patterns. Once again I could do this manually in the room editor, but this seems like a big hassle once we scale up enough. I could create a single string that represents what's in each empty spot in the room, but that's bad to look at, and doesn't help me lay out the rooms quickly!
Ultimately if it's super concise code, but doesn't let me be creative, what's the point? So I turned to my good friend, arrays.
The empty space can be represented by a 12 x 17 set of coordinates, so I split each row into a 24 character string (12 pairs of 2 digits). This will let me have up to 99 different blocks, which surely is enough different kinds of blocks, right??? (I could also add letters if I get desperate, I guess...) I made block 00 a default solid rectangle that I can easily use for debugging; block 01 is the crate that you can see in the concept stage. The fun part is that I can change these later if I want, what these IDs actually mean is defined in the block object.
Anyway, I then have another for loop cycle through all the coordinates in the array and generate blocks where the value isn't -1. Let's see the result!
Wow looks great!!! Except... those "-1"s are still kind of bad to look at. But, if we think about it, we're reading segments of a string. It doesn't HAVE to be -1. That's just what I arbitrarily assigned as the null value in this case. Let's just pick something that makes it good to look at!!
Hell yea! Look at that. Much better. Now I can get all kinds of creative!!!
That's all for now, hope you found this interesting and or maybe entertaining! I don't know!!! BYE!!!!
Leave a comment
Log in with itch.io to leave a comment.