Module: Chingu::Helpers::InputClient
- Included in:
- GameObject, GameState, Window
- Defined in:
- lib/chingu/helpers/input_client.rb
Overview
Input-handler mixin that adds #input= and #input
#input= does 2 things: 1) Initialized an inputmap 2) notifies the parent (could be main Window or a GameState) that the object wants input
In Chingu this is mixed into Window, GameState and GameObject.
You can specify input in 3 different natural formats, the bellow 3 lines does the same thing:
The normal way, this makes left_arrow key call method “left”, and the same thing for right.
self.input = {:left => :left, :right => :right}
The shortened way, does exaclty as the above.
self.input = [:left, :right]
The multi-way, adds :a as trigger for method left, and :d as trigger for method :right
self.input = {[:a, :left] => :left, [:right, :d] => :right}
Instance Method Summary collapse
Instance Method Details
#input ⇒ Object
84 85 86 |
# File 'lib/chingu/helpers/input_client.rb', line 84 def input @input end |
#input=(input_map) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/chingu/helpers/input_client.rb', line 47 def input=(input_map) @input ||= Hash.new #@input = input_map if input_map.is_a? Array # # Un-nest input_map [:left, :right, :space] # Into: { :left => :left, :right => :right, :space => :space} # input_map.each do |symbol| @input[symbol] = symbol end elsif input_map.is_a? Hash # # Un-nest input: { [:pad_left, :arrow_left, :a] => :move_left } # Into: { :pad_left => :move_left, :arrow_left => :move_left, :a => :move_left } # input_map.each_pair do |possible_array, action| if possible_array.is_a? Array possible_array.each do |symbol| @input[symbol] = action end elsif possible_array.is_a? Symbol @input[possible_array] = action end end end if @parent if (@input == nil || @input == {}) @parent.remove_input_client(self) else @parent.add_input_client(self) end end end |