Module: ShatteredPack::KeyboardInput::InstanceMethods
- Defined in:
- lib/shattered_pack/keyboard_input/keyboard_input.rb
Instance Method Summary collapse
-
#action_from(params) ⇒ Object
Given an input of: 1) key :pressed => :up, :action => => :up 2) key :pressed => :down, :action => :fall returns 1) [:jump, [:up]] 2) [:fall, []].
-
#key(actions = {}) ⇒ Object
See KeyboardInput::key.
-
#key_action(reaction, action, time_elapsed, params) ⇒ Object
Update_key will send each of the events to the actor object for whatever key options are defined.
-
#key_actions ⇒ Object
:nodoc:.
-
#key_event(params = {}) ⇒ Object
Simulate a key press, release or hold event.
- #pre_initialize(options) ⇒ Object
-
#reaction_from(options) ⇒ Object
Given an input of: key :held=>:up, :action => :key_action This will return [:held, [:up]].
- #reaction_types ⇒ Object
-
#update_input(time_elapsed, input) ⇒ Object
update_input takes the input object, and sends the actor every key event this frame.
Instance Method Details
#action_from(params) ⇒ Object
Given an input of:
1) key :pressed => :up, :action => {:jump => :up}
2) key :pressed => :down, :action => :fall
returns
1) [:jump, [:up]]
2) [:fall, []]
72 73 74 75 76 |
# File 'lib/shattered_pack/keyboard_input/keyboard_input.rb', line 72 def action_from(params) action = params[:action] return [action, []] unless action.is_a? Hash return [action.keys[0], action.values[0]] end |
#key(actions = {}) ⇒ Object
See KeyboardInput::key
79 80 81 82 83 |
# File 'lib/shattered_pack/keyboard_input/keyboard_input.rb', line 79 def key(actions={}) reaction, keys = reaction_from(actions) action, params = action_from(actions) keys.each { |key| key_actions << [reaction, key.to_sym, action, params ] } end |
#key_action(reaction, action, time_elapsed, params) ⇒ Object
Update_key will send each of the events to the actor object for whatever key options are defined.
87 88 89 |
# File 'lib/shattered_pack/keyboard_input/keyboard_input.rb', line 87 def key_action(reaction, action, time_elapsed, params) #:nodoc: send(action, *params) end |
#key_actions ⇒ Object
:nodoc:
91 92 93 |
# File 'lib/shattered_pack/keyboard_input/keyboard_input.rb', line 91 def key_actions #:nodoc: @key_actions ||= [] end |
#key_event(params = {}) ⇒ Object
Simulate a key press, release or hold event
103 104 105 106 107 108 109 |
# File 'lib/shattered_pack/keyboard_input/keyboard_input.rb', line 103 def key_event(params = {}) lookup_reaction, keys = reaction_from(params) lookup_key = keys[0] key_actions.each do |reaction, key, action, params| key_action(reaction, action, 0, params) if key == lookup_key && lookup_reaction == reaction end end |
#pre_initialize(options) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/shattered_pack/keyboard_input/keyboard_input.rb', line 39 def pre_initialize() # update our keyboard input key_converter = Shatter::GameLoader.instance.key_converter timer.every(:frame) do |time_elapsed| #TODO: the environment needs to go away - and the input needs to be grabbed from OIS. update_input(time_elapsed, key_converter) end super end |
#reaction_from(options) ⇒ Object
Given an input of:
key :held=>:up, :action => :key_action
This will return [:held, [:up]]
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/shattered_pack/keyboard_input/keyboard_input.rb', line 55 def reaction_from() #:nodoc: # Define the method to send the actions when the keys are pressed/clicked/released. reaction_types.each do |reaction_type| if .has_key?(reaction_type) return [reaction_type, [reaction_type]] if [reaction_type].is_a? Array return [reaction_type, [[reaction_type]]] end end raise Error, "Reaction to key input must be #{reaction_types.join(',')}. #{.inspect}" if [:reaction].nil? end |
#reaction_types ⇒ Object
49 50 51 |
# File 'lib/shattered_pack/keyboard_input/keyboard_input.rb', line 49 def reaction_types @reaction_types ||= [:pressed, :released, :held] end |
#update_input(time_elapsed, input) ⇒ Object
update_input takes the input object, and sends the actor every key event this frame.
96 97 98 99 100 |
# File 'lib/shattered_pack/keyboard_input/keyboard_input.rb', line 96 def update_input(time_elapsed, input) #:nodoc: key_actions.each do |reaction, key, action, params| key_action( reaction, action, time_elapsed, params) if input.key_event?(reaction,key) end end |