Class: InputMapper

Inherits:
Object show all
Extended by:
Publisher
Defined in:
lib/gamebox/core/input_mapper.rb

Instance Method Summary collapse

Constructor Details

#initializeInputMapper

Returns a new instance of InputMapper.



6
7
8
# File 'lib/gamebox/core/input_mapper.rb', line 6

def initialize
  @action_ids = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gamebox/core/input_mapper.rb', line 41

def method_missing(name, *args)
  if name.to_s.end_with? '?'
    button_syms = @action_ids[name[0..-2].to_sym]
    if button_syms
      return button_syms.any? do |button_sym| 
        input_manager.down_ids.include? BUTTON_SYM_TO_ID[button_sym]
      end
    end
  end

  false
end

Instance Method Details

#clearObject

unsubscribes for all input



37
38
39
# File 'lib/gamebox/core/input_mapper.rb', line 37

def clear
  input_manager.unsubscribe_all self
end

#map_controls(input_hash) ⇒ Object

map_controls is to be setup outside of an actor and passed in at construction time ie:

mapper.map_controls

'+a' => :jump,   # emit jump when 'a' is _pressed_
'-b' => :duck,   # emit duck when 'b' is _released_
'c'  => :block   # emit block when 'c' is _pressed_ AND _released_

all keys will have a <key_name>? defined for checking state at anytime create_actor :fighter, input: mapper

all of the actor’s behaviors can / should use the input mapper instead of raw key bindings



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gamebox/core/input_mapper.rb', line 23

def map_controls(input_hash)
  input_hash.each do |input, actions|
    if input.start_with? '-'
      register_key_released(input[1..-1], actions)
    elsif input.start_with? '+'
      register_key_pressed(input[1..-1], actions)
    else
      register_key_released(input, actions)
      register_key_pressed(input, actions)
    end
  end
end