Module: Chingu::Helpers::InputDispatcher

Included in:
GameState, Window
Defined in:
lib/chingu/helpers/input_dispatcher.rb

Overview

Methods for parsing and dispatching Chingus input-maps Mixed into Chingu::Window and Chingu::GameState

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#input_clientsObject (readonly)

Returns the value of attribute input_clients.



31
32
33
# File 'lib/chingu/helpers/input_dispatcher.rb', line 31

def input_clients
  @input_clients
end

Instance Method Details

#add_input_client(object) ⇒ Object



33
34
35
# File 'lib/chingu/helpers/input_dispatcher.rb', line 33

def add_input_client(object)
  @input_clients << object    unless @input_clients.include?(object)
end

#dispatch_action(action, object) ⇒ Object

For a given object, dispatch “action”. An action can be:

  • Symbol (:p, :space), translates into a method-call

  • Proc/Lambda, call() it

  • GameState-instance, push it on top of stack

  • GameState-inherited class, create a new instance, cache it and push it on top of stack



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/chingu/helpers/input_dispatcher.rb', line 89

def dispatch_action(action, object)
  # puts "Dispatch Action: #{action} - Objects class: #{object.class.to_s}"
  if action.is_a? Symbol
    object.send(action)
  elsif action.is_a? Proc
    action.call
  elsif action.is_a? Chingu::GameState
    push_game_state(action)
  elsif action.superclass == Chingu::GameState
    push_game_state(action)
  end
end

#dispatch_button_down(id, object) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/chingu/helpers/input_dispatcher.rb', line 41

def dispatch_button_down(id, object)
  return if(object.nil? || object.input.nil?)
  
  object.input.each do |symbol, action|
    if Input::SYMBOL_TO_CONSTANT[symbol] == id
      dispatch_action(action, object)
    end        
  end
end

#dispatch_button_up(id, object) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/chingu/helpers/input_dispatcher.rb', line 51

def dispatch_button_up(id, object)
  return if object.nil? || object.input.nil?
  
  object.input.each do |symbol, action|
    if symbol.to_s.include? "released_"
      symbol = symbol.to_s.sub("released_", "").to_sym
      if Input::SYMBOL_TO_CONSTANT[symbol] == id
        dispatch_action(action, object)
      end
    end
  end
end

#dispatch_input_for(object, prefix = "holding_") ⇒ Object

Dispatches input-mapper for any given object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/chingu/helpers/input_dispatcher.rb', line 67

def dispatch_input_for(object, prefix = "holding_")
  return if object.nil? || object.input.nil?
  
  object.input.each do |symbol, action|
    if symbol.to_s.include? prefix
      symbol = symbol.to_s.sub(prefix, "").to_sym
      if $window.button_down?(Input::SYMBOL_TO_CONSTANT[symbol])
        dispatch_action(action, object)
      end
    end
  end
end

#remove_input_client(object) ⇒ Object



37
38
39
# File 'lib/chingu/helpers/input_dispatcher.rb', line 37

def remove_input_client(object)
  @input_clients.delete(object)
end