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
-
#holding?(*keys) ⇒ Boolean
Returns true or false depending if any of the given keys are pressed.
- #input ⇒ Object
-
#input=(input_map) ⇒ Object
Input-map writer.
Instance Method Details
#holding?(*keys) ⇒ Boolean
Returns true or false depending if any of the given keys are pressed
Example:
@player.go_left if holding?(:left, :a)
53 54 55 56 57 58 |
# File 'lib/chingu/helpers/input_client.rb', line 53 def holding?(*keys) Array(keys).each do |key| return true if $window.(Chingu::Input::SYMBOL_TO_CONSTANT[key]) end return false end |
#input ⇒ Object
99 100 101 |
# File 'lib/chingu/helpers/input_client.rb', line 99 def input @input end |
#input=(input_map) ⇒ Object
Input-map writer
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/chingu/helpers/input_client.rb', line 63 def input=(input_map) @input ||= Hash.new 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 |