Short update today, but I’m still plugging away at the Hex based dungeon crawler I started recently. I haven’t had a lot of time to work on the project since the last update… but in that time I’ve managed to make what I think is meaningful progress.
I continued work on trying to randomly place rooms and hallways on the hex grid, and I started to run into some issues around placing geometry and being able to tell where the walls go around the floor space. It got especially complicated around the seams between rooms/halls/etc. When I first started trying to build hallways, I created a method that would start at a given cell and then loop based on the desired length. At each step it would modify the cell as desired and then increment one of the coordinates. It worked… but was only really useful if I wanted to only do things parallel to the axes. That might be fine for now, but it just didn’t feel elegant.
I decided to step back and do a little rethinking and restructuring. I revisited the Red Blob article on hexes and wouldn’t you know it, there’s a whole section about drawing lines. I built some infrastructure, more or less following the methods they described, and, unsurprisingly, it worked out well. It was much more elegant than my previous approach, and it would work in arbitrary directions. Better still, it would simply return a collection of cells. Once I had the collection, I could perform checks on it, say, to look for collisions with existing geometry, or I could loop through and make whatever changes I want. Set it all to floors, set it all to walls, whatever.
To thoroughly exercise my new tools, I set up a quick experiment that would draw 30 random lines on my hex grid. Both endpoints were chosen randomly on the grid. Once I had each collection of cells, I’d check for collisions, and then set the cells on a few criteria. No collisions – everything is set to floor tiles. Any collisions – everything is set to wall tiles except the colliding cells – those are set to door tiles. The particular tiles aren’t important… just that they’re visually distinct so I can see what’s happening. This worked well, except it all happened more or less instantly. Great in that it indicates there aren’t any completely egregious performance problems… but it makes it somewhat hard to tell exactly what’s happening. Especially with 30 lines crammed on to a relatively small grid.
So, I spent a little time experimenting with Unity’s coroutines. After a little bit of work and a little bit of head scratching, I got it set up so that it would draw each cell one at a time and then wait for a short period of time. Because I was using coroutines, it would return control back to the main Unity loop and keep rendering new frames throughout the process. Voila! I could watch the lines draw out, including the different colors for the different things.
Ultimately, it really doesn’t seem like much, but it’s progress and I’m happy with it. Next up I’m going to rewrite the room generator to create them from collections of lines, and then get back to trying to connect hallways to them.