Saturday, March 26, 2016

Dungeon Crawler in Java

I have begun building a dungeon crawler game. The premise of the game that I came up with is a little odd for a dungeon crawler but hopefully it will be interesting and you'll like it.

So the world is going to be made up of dungeons, yeah obviously. The dungeons will have been created from a simulation of AI dwarves starting from just two in a random area. I plan on creating some unique monsters that spawn in order to slow or stop the dwarves quest to build meaningful dungeons. Build in some magical items and of course hopefully some humorous history generated for NPCs, books, spells, and items in the dungeon crawl.

The world currently is all dirt except for a 5x5 area of grass where the dwarves will start out, need to add in minerals and rare metals etc. Also little hazards ;)

Tools I'm using:

Heres a snippet from the Dwarves current behaviortree

    public static AICreature createMaleDwarf(final World world) {
        InOrderSequence root = new InOrderSequence<>();
        SelectorNode isHealthy = new SelectorNode<>();
        SelectorNode isDanger = new SelectorNode<>();
        isDanger.addChild(context -> {
            return world.findHostiles(context, context.getAwarenessDistance()).length > 0;
        }, isHealthy);
        Action attack = context -> {
            return NodeStatus.FAILURE;
        isHealthy.addChild(context -> {
            return context.getHealth() > 80;
        }, new LeafNode(attack));

        LeafNode isBored = new LeafNode<>(context -> {
            if (world.every(context, 0.3)) {
            return NodeStatus.RUNNING;

        return new Dwarf(root,;


Thursday, March 10, 2016

JRuby for game development

So I have been pretty active in using Ruby for the past several years. I have found it a great resource for writing less code and producing more results. So why not use it for game development. Build some DSLs or better syntax for writing game logic code. So in this post I am going to run through a couple of ways to accomplish writing a game in ruby by using java for graphics and additional functionality.

So first we start off by needing to package everything up nicely in a single jar file so it appears as though we're just using java. Unfortunately this comes with a bit of a space cost. And that space is that of jruby the complete jar is ~20mb you can find them at so no way to avoid this although you can do something like write an install script that downloads it and then puts your source into it afterwards.

Ok once you have the jar file you will add your code to it and it will become your executable from java.

Make a copy of the complete jar.

$ cp jruby-complete- mygame.jar

Create a file that jruby will launch called jar-bootstrap.rb

#!/usr/bin/env ruby
puts "Hello, I am jar-bootstrap.rb from inside the jar file"

Update the jar (-u) by first modifying the manifest to run a specific java class using option -e to override the Main-Class entry in the manifest already inside the jar.

$ jar ufe mygame.jar org.jruby.JarBootstrapMain

Then you can use the -u option to add files to the jar file or update existing ones.

$ jar uf mygame.jar jar-bootstrap.rb

Then viola:

 $ java -jar mygame.jar
Hello, I am jar-bootstrap.rb from inside the jar file.

Ok we've entered the ruby~java land woot woot.

Now for lets rubify java to do our game-logics bidding!!!

Lets switch the jar-bootstrap.rb to load our game which we will start in a game.rb file:

jar-boostrap.rb becomes
#!/usr/bin/env ruby
require './game.rb'

Then we make our game.rb file with a simple example of loading a java swing window and rendering loop

game.rb is
#!/usr/bin/env ruby
include Java

import javax.swing.JFrame
import javax.swing.JPanel
import java.awt.BorderLayout
import java.awt.Dimension

class MyDrawPane < JPanel
  def initialize
    setDoubleBuffered true

  def paintComponent(g)
    g = g.create
    g.drawString 'Hello from ruby', 0, self.height / 2

class MyGame < JFrame

  def initialize
    super "My Game Window Title"

    self.setDefaultCloseOperation JFrame::EXIT_ON_CLOSE
    self.setLocationRelativeTo nil
    self.setVisible true


And with all this we have a simple window to start from but now we can use the power of ruby to rule the world. Uh I mean we'll use the power of ruby to make games... rule the world...

So this is all great but maybe we want easier iterations with our game development because updating the jar all the time isn't that fast. So for development just execute your game.rb file from jruby directly. You can use the original one you downloaded.

$ java -jar ../jruby-complete- game.rb

Or if you use rvm or rbenv then just execute the game script. And Q.E.D.