Introducing ShinyCocos
Last few weeks I've been hard at work trying to get our first game out. In order to do that, we had to create a technology base from zero.
Long story short: I love ruby so much, that I created ShinyCocos, a simple ruby binding for cocos2d-iphone, a great game framework written by Ricardo Quesada, based on a popular python game framework of the same name (minus the "iphone" part :-P)
So, this is the first of a couple of articles about making games for the iphone using ruby (aka ShinyCocos).
Note
Please note that ShinyCocos is a work in progress and not yet complete by any means. I expect to release a stable version the next weeks.
Getting ShinyCocos
First things first, you need to get yourself a copy of ShinyCocos.
Here are the 4 steps you need to do in order to get ShinyCocos running
Get shinycocos. For that, you have two choices: The recommended one, and the lazy one. The recommended one is to use git and clone shinycocos:
git clone git://github.com/funkaster/shinycocos.git
The other way is to click on the Download link on the github page of the project.
Get the dependencies. That means, get the latest cocos2d-iphone (v0.8 as of this writing) and place it inside the ShinyCocos directory.
Open the cocos2d-iphone project and select the cocos2d-iphone target. Get info on it and set the
Build Products Pathto../build. Do the same for the Chipmunk target.Open the TestShinyCocos project and run. Enjoy!
Now to the fun
Ok. Now that you have ShinyCocos running, let's analyse the source.
First there's the main.rb, which should be just the entry point of your
game, much like the main.m. All your classes and other ruby sources
should live in the vendor directory.
require 'tiled_test_scene' Cocos2D::Director.set_orientation Cocos2D::Director::ORIENTATION_LANDSCAPE_LEFT Cocos2D::Director.set_animation_interval 1/60.0 Cocos2D::Director.display_fps false test = TiledTest.new Cocos2D::Director.run_scene test
As you can see, there's no need to require Cocos2D, since it's already
loaded by ShinyCocos for you. The first line requires the test scene,
which we'll look at soon.
All Cocos2D classes live under the Cocos2D module.
The Cocos2D Director is exposed in the ruby world as a Module with some convenience methods. It's here mainly to configure Cocos2d, like device orientation, animation interval, etc. and also to run your scenes.
On line 7 we create the new scene, and on line 8 we run it.
I tried to create the ruby api as close as I could to the Objective-C one, but also rubifying it as much as possible on the way.
Now, let's look at the fun part: tiled_test_scene.rb.
require 'map_reader'
class TiledTest < Cocos2D::Scene
include Cocos2D
def initialize
@map = ::TiledMapReader.new("TestTiled.tmx")
@map.layers.each { |layer|
node = TiledMap.new(@map.properties.merge(:tiles => "tiles.png", :data => layer[:data]))
add_child node
}
end
end
On the first line, we require map_reader, which is a ShinyCocos
library that will load a Tiled map file (tmx).
So, for each layer in the map, we create a new TiledMap node, with the
info from the map, and using the tiles.png file as the tile map.
@map.properties holds all the necessary information needed by the
TiledMap, except for the tiles file and the data, that's why we merge
it.
After creating the new node, we add that "layer" to the scene.
Ok, what's next?
I recomend you to take a look at the documentation, included in ShinyCocos
cd shinycocos rake rdoc open html/index.html
I know it's really incomplete, but it's there and growing.
The next article will focus on the creation of a new project and the base of a full-featured game.