So, I've been working on a framework for Gosu this summer, Chingu. It can be found @
http://github.com/ippa/chingu/tree/masterWhile Chingu the framework isn't release-ready just yet parts of it are.
One of those part is a basic but decently powerfull stack-based game state system which is usable even in very small games.
I've made it "stand alone" so it should it plugs nicely into any Gosu game.
INSTALL:
=======
gem sources -a
http://gems.github.comgem install ippa-chingu
BASIC USAGE:
===========
class Game < Gosu::Window
attr_reader :font
def initialize
$window = super(800,600,false)
# Create our game state manager and start out with State1
@manager = Chingu::GameStateManager.new
@manager.switch_game_state(State1)
end
def button_down(id)
@manager.button_down(id)
end
def button_up(id)
@manager.button_up(id)
end
def draw
@manager.draw
end
def update
@manager.update
end
end
class State1 < Chingu::GameState
def setup
@spinner = ["|", "/", "-", "\\", "|", "/", "-", "\\"]
@spinner_index = 0.0
end
def update(dt)
@spinner_index += 0.1
@spinner_index = 0 if @spinner_index >= @spinner.size
end
def draw
$window.font.draw("Inside State1: #{@spinner[@spinner_index.to_i]}", 100, 100, 0)
end
end
DOCS:
=====
A GameState is just a class with the following instance methods:
* setup() - called each time the game state becomes active.
* button_down(id) - Called when a button is down
* button_up(id) - Called when a button is released
* update() - just as in your normal game loop, put your game logic here.
* draw() - just as in your normal game loop, put your screen manipulation here.
* finalize() - called when a game state de-activated (for example by pushing a new one on top with push_game_state)
GameStateManager has the following public instance methods to work with states:
* push_game_state(state) - adds a new gamestate on top of the stack, which then becomes the active one
* pop_game_state - removes active gamestate and activates the previous one
* switch_game_state(state) - replaces current game state with a new one
* current_game_state - returns the current game state
* previous_game_state - returns the previous game state (useful for pausing and dialog boxes, see example4.rb)
* pop_until_game_state(state) - pop game states until given state is found
* clear_game_states - removes all game states from stack
There's some more documentation @
http://rdoc.info/rdoc/ippa/chingu/blob/407e5bb9b3cead4d02d7d07c1da1d0a2fad8cbfe/Chingu/GameStateManager.htmlChingu docs @
http://rdoc.info/projects/ippa/chinguMORE CODE:
===========
If you install the gem you get an examples-dir:
* example4.rb shows Chingu / game states
* example5.rb shows game states with Only Gosu.
I'm using this in 2 games, and those games have been a driving force of getting a decent system in place.
While Chingu very much still is work in progress I feedback is always cool, on the game state system and in Chingu in general. I'll probably make a full Chingu-release-statement within a couple of weeks once it has matured and good documentation is in place.