Module: Chingu::InputDispatcher
Instance Attribute Summary collapse
-
#input_clients ⇒ Object
readonly
Returns the value of attribute input_clients.
Instance Method Summary collapse
- #add_input_client(object) ⇒ Object
-
#dispatch_action(action, object) ⇒ Object
For a given object, dispatch “action”.
- #dispatch_button_down(id, object) ⇒ Object
- #dispatch_button_up(id, object) ⇒ Object
-
#dispatch_input_for(object, prefix = "holding_") ⇒ Object
Dispatches input-mapper for any given object.
- #remove_input_client(object) ⇒ Object
Instance Attribute Details
#input_clients ⇒ Object (readonly)
Returns the value of attribute input_clients.
37 38 39 |
# File 'lib/chingu/helpers.rb', line 37 def input_clients @input_clients end |
Instance Method Details
#add_input_client(object) ⇒ Object
39 40 41 |
# File 'lib/chingu/helpers.rb', line 39 def add_input_client(object) @input_clients << 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
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/chingu/helpers.rb', line 95 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
47 48 49 50 51 52 53 54 55 |
# File 'lib/chingu/helpers.rb', line 47 def (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
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/chingu/helpers.rb', line 57 def (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
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/chingu/helpers.rb', line 73 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.(Input::SYMBOL_TO_CONSTANT[symbol]) dispatch_action(action, object) end end end end |
#remove_input_client(object) ⇒ Object
43 44 45 |
# File 'lib/chingu/helpers.rb', line 43 def remove_input_client(object) @input_clients.delete(object) end |