Module: Nimo::InputListener

Included in:
ObjectRepresentation, Screen
Defined in:
lib/nimo/listeners/input_listener.rb

Overview

Listen for inputs (key presses) and execute the corresponding actions on update.

Defined Under Namespace

Classes: KeyAction

Instance Method Summary collapse

Instance Method Details

#any_key(&action) ⇒ Object

Register an action to be executed everytime a key is pressed.



17
18
19
# File 'lib/nimo/listeners/input_listener.rb', line 17

def any_key(&action)
  @any_key_action = action
end

#button_down(id) ⇒ Object

Gosu hook invoked anytime a button is pressed



23
24
25
# File 'lib/nimo/listeners/input_listener.rb', line 23

def button_down(id) #:nodoc:
   act_upon.instance_eval(&@any_key_action) if @any_key_action
end

#process_inputs(game_window) ⇒ Object



27
28
29
30
31
# File 'lib/nimo/listeners/input_listener.rb', line 27

def process_inputs(game_window)
  key_actions.each do |key, key_action|
    act_upon.instance_eval(&key_action.action) if key_action.should_execute?(game_window.button_down?(key))
  end
end

#when_key(key, options = {}, &action) ⇒ Object

Register an action that will execute when the key is pressed. An options hash can be specified to customise the behavior. The options are:

  • :repeatable (defaults to true) - execute the action every update regardless if the key was already pressed in the previous update.



11
12
13
# File 'lib/nimo/listeners/input_listener.rb', line 11

def when_key(key, options = {}, &action)
  key_actions[key] = KeyAction.new(action, options)
end