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.
-
#pop_scene ⇒ Object
Pops the last scene.
-
#pop_scene_until {|scene| ... } ⇒ Object
Pops scenes until a condition is met.
-
#pop_scene_while {|scene| ... } ⇒ Object
Pops scenes while a condition is true.
-
#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 ⇒ Ray::SceneList
- #scenes=(list) ⇒ Object
- #title ⇒ Ray::String
- #window ⇒ Ray::Window
Methods included from Helper
#create_event_runner, #disable_event_group, effect_generator, #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
230 231 232 233 234 235 236 |
# File 'lib/ray/game.rb', line 230 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
195 196 197 |
# File 'lib/ray/game.rb', line 195 def exit @game_scenes.exit_current end |
#exit! ⇒ Object
Kills the game, removing all the scenes it contains.
200 201 202 203 204 205 |
# File 'lib/ray/game.rb', line 200 def exit! @game_exited = true @game_scenes.exit_current @game_scenes.clear 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 |
#pop_scene_until {|scene| ... } ⇒ Object
Pops scenes until a condition is met
144 145 146 |
# File 'lib/ray/game.rb', line 144 def pop_scene_until pop_scene_while { |o| !yield(o) } end |
#pop_scene_while {|scene| ... } ⇒ Object
Pops scenes while a condition is true
133 134 135 136 137 |
# File 'lib/ray/game.rb', line 133 def pop_scene_while while yield scenes.current scenes.current.pop_scene # ensure exit is set to false end 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.
186 187 188 189 190 191 192 |
# File 'lib/ray/game.rb', line 186 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.
163 164 165 |
# File 'lib/ray/game.rb', line 163 def registered_scene(name) @game_registered_scenes[name] end |
#run ⇒ Object
Runs the game until the last scene gets popped. Will call Ray.stop.
169 170 171 172 173 174 175 176 |
# File 'lib/ray/game.rb', line 169 def run while running? event_runner.clear register @game_scenes.run_current end end |
#running? ⇒ Boolean
180 181 182 |
# File 'lib/ray/game.rb', line 180 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.
153 154 155 156 157 158 159 160 |
# File 'lib/ray/game.rb', line 153 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 scene.name = name end |
#scenes=(list) ⇒ Object
222 223 224 225 226 227 228 |
# File 'lib/ray/game.rb', line 222 def scenes=(list) @game_scenes = list unless running? @game_scenes.exit_current end end |
#title ⇒ Ray::String
208 209 210 |
# File 'lib/ray/game.rb', line 208 def title @game_title end |