Class: Ray::Game

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/ray/game.rb

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

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

key, key_mod, where

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.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :window (Ray::Window) — default: nil

    If set, this window will be used instead of creating a new one

  • :size (Ray::Vector2, #to_vector2) — default: [640, 480]

    Size of the window.

  • :fullscreen (true, false) — default: false

    True to make the window fullscreen.

  • :resizable (true, false) — default: false

    True to make the window resizable.

  • :no_frame (true, false) — default: false

    True to create a window with no decorations.

Yields:

  • If a block is given, it is instance evaluated. #run will then be called.



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]}
  options = defaults.merge(opts)

  unless options[:window]
    size = options[:size].to_vector2

    @game_window = Ray::Window.new
    @game_window.open(title, size, options)
  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

#exitObject

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_sceneObject

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

Yields:

  • (scene)

    To determine if a scene must be popped.

Yield Parameters:

  • scene (Ray::Scene)

    The scene that will be popped

Yield Returns:

  • (Boolean)

    False to pop the next scene



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

Yields:

  • (scene)

    To determine if a scene must be popped.

Yield Parameters:

  • scene (Ray::Scene)

    The scene that will be popped

Yield Returns:

  • (Boolean)

    True to pop the next scene



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)

Parameters:

  • scene_name (Symbol)

    The name of the scene which should be pushed

  • *args

    Arguments passed to the scene



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.

Returns:

  • (Ray::Scene)

    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

#runObject

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

Parameters:

  • True (true, false)

    if the scene list isn’t empty and the user hasn’t exited from the game.

Returns:

  • (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.

Parameters:

  • name (Symbol)

    the name of the new scene

  • klass (Class) (defaults to: Scene)

    the class of the scene.



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

#scenesRay::SceneList

Returns:



213
214
215
# File 'lib/ray/game.rb', line 213

def scenes
  @game_scenes
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

#titleRay::String

Returns:

  • (Ray::String)


208
209
210
# File 'lib/ray/game.rb', line 208

def title
  @game_title
end

#windowRay::Window

Returns:



218
219
220
# File 'lib/ray/game.rb', line 218

def window
  @game_window
end