Native Javascript, no third party libraries
Part-3 of the tutorial is about “Generating the Dungeon”.
yield functionsplayer intelligently in a roomFirst, a confession: I do not like Rogue’s dungeon layout. I don’t like the 1970s and 80s style D&D dungeon maps that influenced it, either. So, I won’t be carving hollow rooms out to generate this map. I am still following the tutorial closely, but I will instead create rectangular buildings and connect them with open-air paths. It is, by most measures, identical.
Setting up the rectangular room generator isn’t hard, but there are a few things I don’t understand from looking at the Python. It appears to be taking slices of an array, but I don’t see where the array is created. I assume that I need to create arrays of tiles to represent the rooms, and maetl carries me through with a good example on how to do it.
Some time later, I learn that the Pythond slice() function is not the same as an array .slice method, at all.
This is my implementation of creating two rooms.
According to the tutorial, this next bit would be a great place to learn how generator or “yield” functions work. I didn’t do that, and I didn’t implement Bresenham Lines. I did add a new tile type for the paths, I imagined these would be like paving stones between buildings.
Refreshing the page at this point will have the path alternate between horizontal-first and vertical-first path drawing, as the tutorial intended. I offset the test rooms so this would be visible, unlike in the tutorial example. This is my attempt at creating a path between rooms.
The final step of Part-3 is to rewrite the level generator to randomize room size, quantity, and position. I need to create a new randomRange() function in utility.js as there is no native way in JS to generate a number in a specific interger range, and I am able to follow the tutorial mostly as designed.
There are two minor differences in my approach: First, I continue using the sub-functions I created previously to place rooms and path between them, instead of rewriting them into a more monolithic block. Second, I create the array of rooms before performing operations on it (e.g., placing them, pathing between them, and locating the player).
I think the tutorial’s approach is probably more performant, as it reduces the number of times the array needs to be accessed. But I am able to more easily read and follow the code when each step is separate.
The final change in my implementation of multiple rooms with random size and location is cosmetic: I connect the first and last rooms to each other with paths, instead of leaving them as stubs.