Class: RedBird::InputDevice

Inherits:
Object
  • Object
show all
Defined in:
lib/red_bird/input_device.rb,
ext/red_bird/input_device.c

Overview

The InputDevice receives input from the keyboard, joystick, and mouse; then, it translates the input into a meaningful command name and sends it to a Controller. For example, you can use it to translate the ‘space’ key from the keyboard and an ‘x’ button from a controller into a :jump command.

This class has an internal Hash that translates inputs into commands. For every input, it looks if there is a key associated with that input; it sends a command if such key exists; otherwise, it does nothing.

See Also:

Author:

  • Frederico Linhares

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ InputDevice

Returns a new instance of InputDevice.

Parameters:

Author:

  • Frederico Linhares



21
22
23
24
25
# File 'lib/red_bird/input_device.rb', line 21

def initialize(controller)
  @controller = controller
  @keydown_inputs = {}
  @keyup_inputs = {}
end

Instance Attribute Details

#controller=(value) ⇒ Object (writeonly)

Defines a Controller to receives commands from the input.



17
18
19
# File 'lib/red_bird/input_device.rb', line 17

def controller=(value)
  @controller = value
end

Instance Method Details

#add_keydown(keycode, command_name) ⇒ Object

Associates a keydown even to a command_name.

Parameters:

  • keycode (Integer)

    use constants under Keycode.

  • command_name (Symbol)

Author:

  • Frederico Linhares



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

def add_keydown(keycode, command_name)
  @keydown_inputs[keycode] = command_name
end

#add_keyup(keycode, command_name) ⇒ Object

Associates a keyup even to a command_name.

Parameters:

  • keycode (Integer)

    use constants under Keycode.

  • command_name (Symbol)

Author:

  • Frederico Linhares



41
42
43
# File 'lib/red_bird/input_device.rb', line 41

def add_keyup(keycode, command_name)
  @keyup_inputs[keycode] = command_name
end

#exec_events(entities) ⇒ Object

Gets all events that are in the queue, translate then into commands, and send then to the current controller.

Parameters:

  • entities (Array<RedBird::Entity>)

    entities that can receive events from mouse.

Author:

  • Frederico Linhares



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/red_bird/input_device.rb', line 51

def exec_events(entities)
  get_events do |type, data|
    case type
    when :key_down then
      @controller.call_command(@keydown_inputs[data])
    when :key_up then
      @controller.call_command(@keyup_inputs[data])
    when :mouse_motion then
      entity = cursor_hover_entity(entities, data[:x], data[:y])
      entity.cursor_hover(data[:x], data[:y]) unless entity.nil?
    when :mouse_button_down then
      entity = cursor_hover_entity(entities, data[:x], data[:y])
      entity.cursor_down(data[:x], data[:y]) unless entity.nil?
    when :mouse_button_up then
      entity = cursor_hover_entity(entities, data[:x], data[:y])
      entity.cursor_up(data[:x], data[:y]) unless entity.nil?
    when :quit_game then
      Engine::quit_game
    end
  end
end