Class: RedBird::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/red_bird/controller.rb

Overview

A Controller receives commands from a InputDevice and executes codes associated with those commands. Every InputDevice instance has a variable called @controller that must point to an instance of this class.

The purpose of this class is to allow you to change the effect of user input by merely changing the currently active controller; you would do so, for example, when the user pauses the game, or when the user’s character enters a vehicle.

See Also:

Author:

  • Frederico Linhares

Instance Method Summary collapse

Constructor Details

#initializeController

Returns a new instance of Controller.



16
17
18
19
20
21
22
23
24
# File 'lib/red_bird/controller.rb', line 16

def initialize
  @commands = {}

  # By default, quit game when window is closed or another signal to end
  # the game is given.
  self.add_command(:quit_game) do
    Engine::quit_game
  end
end

Instance Method Details

#add_command(command, &function) ⇒ Object

Use this method to create an association between a command and a code block.

Parameters:

  • command (Symbol)
  • function

Author:

  • Frederico Linhares



32
33
34
# File 'lib/red_bird/controller.rb', line 32

def add_command(command, &function)
  @commands[command] = function
end

#call_command(command) ⇒ Object

Execute a code block associated with a command.

Parameters:

  • command (Symbol)

Author:

  • Frederico Linhares



40
41
42
# File 'lib/red_bird/controller.rb', line 40

def call_command(command)
  @commands[command].call unless @commands[command].nil?
end