Class: RedBird::Controller
- Inherits:
-
Object
- Object
- RedBird::Controller
- 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.
Instance Method Summary collapse
-
#add_command(command, &function) ⇒ Object
Use this method to create an association between a command and a code block.
-
#call_command(command) ⇒ Object
Execute a code block associated with a command.
-
#initialize ⇒ Controller
constructor
A new instance of Controller.
Constructor Details
#initialize ⇒ Controller
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.
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.
40 41 42 |
# File 'lib/red_bird/controller.rb', line 40 def call_command(command) @commands[command].call unless @commands[command].nil? end |