Class: Ray::Game
Overview
Games are used to manage different scenes. They also init Ray by creating a window.
Creating a Game
There are several ways of doing this. Using a block:
Ray::Game.new("my game") do
...
end
Using the instance directly:
game = Ray::Game.new("my game")
...
game.run
Subclassing:
class Game < Ray::Game
def initialize
super("my game")
...
end
end
Game.new.run
Registring scenes to a Game
Games need the scenes they use to be registered. The most obvious way to do it is to use scene with a block:
scene :game do
# See Ray::Scene
end
You may also call it to register a subclass of Ray::Scene
scene(:game, GameScene)
Which is the same as:
GameScene.bind(self) # Assuming GameScene's scene_name is set to :game
Managing the scene stack
You can push a scene to the game:
push_scene :game
When #run will be called, it will show the scene :game. Notice that, if you push more than one scene, only the last one will be seen directly. However, if you remove it later, the previous scene will be shown.
You can thus also remove a scene from your stack:
pop_scene # Removes the last scene
exit is not exactly the same: it will ask the scene to quit before doing this. exit! will do something totally different: completely kill the game.
Handling events
Games can listen to events just like scenes. Since the event runner will change often, it needs to register every time it changes it. You can pass a block to the register method:
register do
on :some_event do some_stuff end
end
You may also want to override register in suclasses:
def register
on :some_event do some_stuff end
end
Instance Method Summary collapse
- #event_runner=(runner) ⇒ Object
-
#exit ⇒ Object
Removes the current scene of this game.
-
#exit! ⇒ Object
Kills the game, removing all the scenes it contains.
-
#initialize(title, opts = {}) { ... } ⇒ Game
constructor
Creates a new game.
- #inspect ⇒ Object
-
#pop_scene ⇒ Object
Pops the last scene.
-
#push_scene(scene_name, *args) ⇒ Object
Adds a scene to the stack using its name.
-
#register(&block) ⇒ Object
Registers a block to listen to events Subclasses can also overrid this method to register for events.
-
#registered_scene(name) ⇒ Ray::Scene
Scene register for a given name.
-
#run ⇒ Object
Runs the game until the last scene gets popped.
- #running? ⇒ Boolean
-
#scene(name, klass = Scene, &block) ⇒ Object
Registers a new scene with a given name.
- #scenes ⇒ Object
- #scenes=(list) ⇒ Object
- #title ⇒ Object
- #window ⇒ Object
Methods included from Helper
#create_event_runner, #disable_event_group, #enable_event_group, #event_runner, font, holding?, image, image_target, mouse_pos, music, #remove_event_group, #rotation, #scale_variation, sound, sound_buffer, sprite, text, #translation
Methods included from Matchers
Methods included from DSL::EventListener
#add_hook, #current_event_group, #current_event_group=, #event_group, #listener_runner, #listener_runner=, #on
Methods included from DSL::EventRaiser
#raise_event, #raiser_runner, #raiser_runner=
Constructor Details
#initialize(title, opts = {}) { ... } ⇒ Game
Creates a new game.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/ray/game.rb', line 83 def initialize(title, opts = {}, &block) @game_registered_scenes = {} @game_scenes = SceneList.new(self) @game_register_block = nil @game_exited = false @game_title = title create_event_runner defaults = {:size => [640, 480]} = defaults.merge(opts) unless [:window] size = [:size].to_vector2 @game_window = Ray::Window.new @game_window.open(title, size, ) else @game_window = opts[:window] end if block instance_eval(&block) run end end |
Instance Method Details
#event_runner=(runner) ⇒ Object
206 207 208 209 210 211 212 |
# File 'lib/ray/game.rb', line 206 def event_runner=(runner) super @game_registered_scenes.each do |name, scene| scene.event_runner = runner end end |
#exit ⇒ Object
Removes the current scene of this game
174 175 176 |
# File 'lib/ray/game.rb', line 174 def exit @game_scenes.exit_current end |
#exit! ⇒ Object
Kills the game, removing all the scenes it contains.
179 180 181 182 183 184 |
# File 'lib/ray/game.rb', line 179 def exit! @game_exited = true @game_scenes.exit_current @game_scenes.clear end |
#inspect ⇒ Object
214 215 216 |
# File 'lib/ray/game.rb', line 214 def inspect "game(#{title.inspect})" end |
#pop_scene ⇒ Object
Pops the last scene.
124 125 126 |
# File 'lib/ray/game.rb', line 124 def pop_scene @game_scenes.pop end |
#push_scene(scene_name, *args) ⇒ Object
Adds a scene to the stack using its name.
You must call Game#scene before this. If you subclassed scene, then call bind to register it:
scene :something, SomeClass
SomeClass.bind(self)
119 120 121 |
# File 'lib/ray/game.rb', line 119 def push_scene(scene_name, *args) @game_scenes.push(scene_name, *args) end |
#register(&block) ⇒ Object
Registers a block to listen to events Subclasses can also overrid this method to register for events.
165 166 167 168 169 170 171 |
# File 'lib/ray/game.rb', line 165 def register(&block) if block_given? @game_register_block = block else @game_register_block.call if @game_register_block end end |
#registered_scene(name) ⇒ Ray::Scene
Returns scene register for a given name.
142 143 144 |
# File 'lib/ray/game.rb', line 142 def registered_scene(name) @game_registered_scenes[name] end |
#run ⇒ Object
Runs the game until the last scene gets popped. Will call Ray.stop.
148 149 150 151 152 153 154 155 |
# File 'lib/ray/game.rb', line 148 def run while running? event_runner.clear register @game_scenes.run_current end end |
#running? ⇒ Boolean
159 160 161 |
# File 'lib/ray/game.rb', line 159 def running? !@game_exited && !@game_scenes.empty? end |
#scene(name, klass = Scene, &block) ⇒ Object
Registers a new scene with a given name. the block will be passed to klass.new.
133 134 135 136 137 138 139 |
# File 'lib/ray/game.rb', line 133 def scene(name, klass = Scene, &block) scene = @game_registered_scenes[name] = klass.new(&block) scene.game = self scene.event_runner = event_runner scene.window = @game_window end |
#scenes ⇒ Object
190 191 192 |
# File 'lib/ray/game.rb', line 190 def scenes @game_scenes end |
#scenes=(list) ⇒ Object
198 199 200 201 202 203 204 |
# File 'lib/ray/game.rb', line 198 def scenes=(list) @game_scenes = list unless running? @game_scenes.exit_current end end |
#title ⇒ Object
186 187 188 |
# File 'lib/ray/game.rb', line 186 def title @game_title end |
#window ⇒ Object
194 195 196 |
# File 'lib/ray/game.rb', line 194 def window @game_window end |