Module: ShatteredController::KeyboardInput::InstanceMethods

Defined in:
lib/keyboard_input/keyboard_input.rb

Instance Method Summary collapse

Instance Method Details

#key(actions = {}) ⇒ Object

See KeyboardInput::key



64
65
66
67
# File 'lib/keyboard_input/keyboard_input.rb', line 64

def key(actions={})
  reaction, keys = reaction_from(actions)
  keys.each { |key| key_actions << [reaction, key.to_sym, actions[:action] ] }
end

#key_action(reaction, action, time_elapsed) ⇒ Object

Update_key will send each of the events to the actor object for whatever key options are defined.



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/keyboard_input/keyboard_input.rb', line 70

def key_action(reaction, action, time_elapsed) #:nodoc:
  if reaction == :held
    begin
      send(action, time_elapsed)
    rescue ArgumentError
       send(action)
    end
  else
    send(action)
  end
end

#key_actionsObject

:nodoc:



81
82
83
# File 'lib/keyboard_input/keyboard_input.rb', line 81

def key_actions #:nodoc:
  @key_actions ||= []
end

#pre_initializeObject



39
40
41
42
43
44
45
# File 'lib/keyboard_input/keyboard_input.rb', line 39

def pre_initialize
 # update our keyboard input
 timer.every(:frame) do |time_elapsed|
   update_input(time_elapsed, ShatteredController::Runner.environment[:input])
  end
 super
end

#reaction_from(options) ⇒ Object

Given an input of:

key :held=>:up, :action => :key_action
This will return [:held, :key_action]

Raises:



53
54
55
56
57
58
59
60
61
62
# File 'lib/keyboard_input/keyboard_input.rb', line 53

def reaction_from(options) #:nodoc:
  # Define the method to send the actions when the keys are pressed/clicked/released.
  reaction_types.each do |reaction_type|
    if options.has_key?(reaction_type)
      return [reaction_type, options[reaction_type]] if options[reaction_type].is_a? Array
      return [reaction_type, [options[reaction_type]]]
    end
  end
  raise Error, "Reaction to key input must be pressed, released, or held. #{options.inspect}" if options[:reaction].nil?
end

#reaction_typesObject



47
48
49
# File 'lib/keyboard_input/keyboard_input.rb', line 47

def 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.



85
86
87
88
89
# File 'lib/keyboard_input/keyboard_input.rb', line 85

def update_input(time_elapsed, input) #:nodoc:
  key_actions.each do |reaction, key, action|
    key_action( reaction, action, time_elapsed ) if input.key_event?(reaction,key)
  end
end