On december the 6th 2011 I had an idea for *another* new game, that I call Dark Mines. I wanted to make a simple 2d maze exploring game where you play as a dwarf who explores the Dark Mines and collects valuables. I have some ideas to make it intersting, and I’ll get to those later.
But first I was curious how fast I could implement a very simple 2D maze crawler. So I headed out to trusty old Google.
First step: find a 2D games engine in Java or Flash. I examined a few projects and finally stumbeled upon JGame. Wow, that is exactly what I need. A 2D Java engine that has support for tiles and sprites. It has lots of sample games to allow you to easilly understand the code. And it is completely open source, so I can edit/extend the engine as I see fit. One of the games included with the JGame library is Dungeons of Hack. A simple Gauntlet style game that contained a very good starting point for my game, namely: walls and a walking main character.
So I removed all the code relating to actual game mechanics of Dungeons of Hack and was left with the ultimate basic setup: my hero in an empty room. (Note that I did not change any graphics from Dungeons of Hack, so my ‘dwarf’ looks more like an elf for now. )
Next step is to make the map interesting. Dark Mines is situated in caves, so I needed convincing 2D cave maps for this. I have played a lot of D&D so I know I could draw these and convert them into a square pattern. But then I thought that it would be much more fun if the maps were generated. This way I could enjoy the game as wel. But how do you generate a 2D cave structure? Off to google once again.
After some searching through dry algorithms I found someone who had not only implemented it, but also found parameters to generate nice caves.
Cellular_Automata_Method_for_Generating_Random_Cave-Like_Levels
And before the night was over, my characters was walking through a generated cave.
Now the idea is that you explore the Dark Mines, so I needed to limit the field of vision. JGame does not support this natively, so I got my hands dirty and started hacking the JGame source code. My initial result showed that I indeed understood what I was doing.
And now for the big one. Dark Mines is not called Dark for nothing. It has everything to do with what you can and cannot see. So I needed to add lighting to the game. This is a serious extension of the JGame engine code, and I started simple. Manually craft images that get progressively darker and use those to make tiles close to you appear lighter than those far away. This is where the real work started, but the resulting imagery is much more atmospheric. Good.
At this point I started wondering about performance. The current version of the game moved very fluidly, but I had no idea how much computational power I had ‘to spare’. Java is not generally used for video games and has an undeserved bad reputation for performance. Since I was already thinking about wild things you can do with light, darkness and multiple light sources, I had to know if I could get away with ignoring performance for now. So I added a few log lines to tell me how many milliseconds it took to render frame. To give you the short answer: it took 0 milliseconds to render a frame.
0?
That can’t be right. And indeed, it was not right, but near enough to be useful. I added nanosecond timing, and found that it took 0.2 milliseconds to reneder a frame once the Java Hotspot compiler had done its work. I also discovered that the millisecond timer of Java is just not very accurate, as it jumps in steps of 16 or sometimes even 30 milliseconds.
Anyway… 134 tiles rendered in 0.2 milliseconds. Wow! I asked several collegues of mine (who are professional Java developers) how many milliseconds they thought it would take, and their guesses were between 20 and 200 ms. It’s nice to be surprised in such a positive way. So no worries about performance for now, just concentrate on game content.My next step was to have a proper starting point for my hero. Up until now I just dumped him somewhere in the cave and hope that he was not instantly stuck in a wall. To solve this I decided to create a corridor that starts at the center of the left side of the map and would go inwards until it met a free space of the cave. I cannot easilly explain why I get so much joy out of writing such simple algorithms. The fact that the cave generation code realy does just what I described, gives me great pleasure. Maybe its because how it works in my mind is matched very closely to how it works in code… Anyway: add a stairs icon and go.
Time to add some actual “game” elements. First up: valuables. Since it is a mining game, the valuables come in the form of gold ore that is stored in cave walls. I wrote a level generator that placed gold ore tiles in places that a player could reach. The result clearly shows the resulting ore tiles.
In fact: the resulting ore tiles show up a bit too clearly. I did not make multiple versions of this graphic for different lighting intensities. As a result the ore tile will always show up bright, regardless of how much light is calculated to be at that tile. At some point I was going to have the game engine calculate different versions of each tile for different lighting levels. So no point in doing manual labour now.
Speaking of labour: the ore does not collect itself. I considered several options and decided to go for the most simple and intuitive one. Just walk into the ore to collect it. And to make it a bit more like you’re hacking it out of the rock: you need to hit into it several time. In terms of code this was my first experiment with collisions between tiles and the main sprite. JGame solved most of the problems for me, but this is where the JGame code starts to get complicated. Anyway: the results are still good.
Now in order to complete the ‘minimal level’ approach, I added detection for when you reach the stairs.