advertisement

Print

Creating Games in Ruby (Part 2)
Pages: 1, 2, 3, 4

Shattered can reduce the amount of code needed to describe a scene considerably by making assumptions about where the media files can be found in the application directory structure and what they are called, about how objects created from a View are likely to be grouped, and about the relationship between the View and the Model. Below is a code snippet from one of the sample apps that comes with Ogre.rb, followed by a code snippet that shows how the same scene directives could be expressed using Shattered.

# from an Ogre.rb demo
class SkyPlaneApplication < Application
  def create_scene 
    scene_manager.set_ambient_light ColourValue.new(0.5,
      0.5, 0.5)
	plane = Plane.new
	plane.d = 5000
	plane.normal = -Vector3.UNIT_Y
	scene_manager.set_sky_plane(true,
	plane,"SpaceSkyPlane", 10000, 3)
	light = scene_manager.create_light("MainLight")
	light.set_position(20, 80, 50)
	dragon = scene_manager.create_entity("dragon",
	  "dragon.mesh")
	scene_manager.root_scene_node.attach_object(dragon) 
  end
end
# using Shattered Ruby to define the above scene...
class SkyPlane
  light "Mainlight", 
        :position => (20,80,50)
end

class Dragon
end

But Shattered is not only about reducing code size, it's also about having fun with the code you do write and being more expressive.

Vectors are most commonly created by passing the three coordinates to v: an x coordinate, a y coordinate, and a z coordinate. I like the way Shattered provides a core extension for Symbol to add to_v support for symbols that describe changing direction. The expression :backward.to_v evaluates to v(0,0,-1)and :up.to_v evaluates to v(0,1,0).

You begin building a game by typing the command shatter and the name of your game. Shattered Ruby generates the following directory structure, which will be very familiar to anyone familiar with Ruby on Rails:

Shattered Ruby directory structure
Figure 6. Shattered Ruby directory structure

When you type script/generate actor, and the name of your Actor, View code, and Model code is generated. An Actor is made up of a Model and a View. The Model contains the game logic and the user input handlers. When a method is called on the Model, and a method with the same name exists on the View, the View's method is automatically invoked when the corresponding Model method finishes executing. If you're playing 3D checkers and crown is called on a particular checker's Model, crown will automatically be invoked on that checker's View if it's defined. Most likely it's defined to top that checker with another checker.

OGRE material scripts can be used to control textures, light settings, and positioning for 3D models. They can be difficult to format by hand, but Shattered Ruby supplies a mechanism for using erb to generate scripts with the OGRE material format.

Here's a sample Shattered template that takes care of formatting material scripts automatically:

material < %= name % >
{
   technique 
   { 
      pass 
      { 
        lighting off
        texture_unit 
        { 
            texture < %= texture % >
        } 
     }
  }
}

Martyn and Mikkel are currently at the tail end of the refactoring effort they began last year, and the most of the tutorials on the Shattered Ruby Wiki still need to be updated to reflect the changes.

One main reason for the refactoring was to make everything supported by Ogre.rb available to Shattered developers. When the framework was first written, only wrappers for the subset of Ogre.rb features that were needed for Shattered's game DSL were packaged with Shattered. On the Shattered blog, Mikkel explains this decision in terms of Ruby on Rails. He explains that Shattered and Ogre.rb are analogous to ActiveRecord and Rails. Just as Rails developers have the option of using straight SQL when there's no ActiveRecord support for something they need to do, Shattered developers can use straight Ogre.rb calls when Shattered doesn't handle something out of the box. At this point, the development branch of Shattered Ruby is fully integrated with the latest release of Ogre.rb, 0.2.

Last year it was possible to build this 3D version of Tetris by following a tutorial on the Shattered Ruby Wiki:

 Tetris built with Shattered Ruby
Figure 7. Tetris built with Shattered Ruby

The best way to find out when the Shattered Ruby tutorials have been updated and when the next release is out is to join the Shattered Ruby Google Group.

The next release of Shattered Ruby will not just be a refactored version of last year's model. New features that are already checked into the development branch include "3D sound" (volume automatically increases and decreases as figure moves forward or backwards), high level helpers for using the Chipmunk physics library, and integration with the Navi library for building GUIs with HTML syntax on panels in 3D space.

Railgun
Creator and Lead Developer: David Koontz

Like Shattered Ruby, Railgun is a 3D framework that aims to provide a Rails-like development experience. It's JRuby-based and wraps JMonkeyEngine, a 3D game framework written in Java. It is in it's beginning stages, but I think it will be interesting project to watch.

Its creator, David Koontz, also created Monkeybars, which leverages JRuby to create Swing GUIs.

Pages: 1, 2, 3, 4

Next Pagearrow