Module: Shattered::Input::InstanceMethods
- Defined in:
- lib/shattered_model/input.rb
Instance Method Summary collapse
-
#controls(*args) ⇒ Object
Example Usage: game.rb:.
-
#key_event(params = {}) ⇒ Object
Simulate a key press, release or hold event.
-
#keyboard_listener ⇒ Object
Retrieve the game’s keyboard singleton.
-
#update_input ⇒ Object
update_input takes the input object, and sends the actor every key event this frame.
Instance Method Details
#controls(*args) ⇒ Object
Example Usage: game.rb:
class Game < Shattered::Game
def initialize
...
player = Player.new # Where player includes Shattered::Model
player.controls.on # this is the same as 'player.controls("player.controls").on'
end
end
player.controls:
# command => condition
levitate => hold :a
# Note that both the command and the condition are instance_eval'ed
You can turn off an actor’s controls by calling:
player.controls.off
Controls default as off.
Note 1: Controls are only expected to be ‘method => action’ pairs, they are not meant logic at all, even though they can.
Note 2: Controls are meant to be user edittable - a user interface for defining keys.
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/shattered_model/input.rb', line 61 def controls(*args) file = args[0] if(file.nil? && @controls.nil?) file = self.class.to_s.underscore end @controls = Controls.new(file) if @controls.nil? @controls = Controls.new(file) if !file.nil? && (file != @controls.file) return @controls end |
#key_event(params = {}) ⇒ Object
Simulate a key press, release or hold event
90 91 92 93 94 95 96 |
# File 'lib/shattered_model/input.rb', line 90 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 |
#keyboard_listener ⇒ Object
Retrieve the game’s keyboard singleton
85 86 87 |
# File 'lib/shattered_model/input.rb', line 85 def keyboard_listener Game.instance.keyboard_listener end |
#update_input ⇒ Object
update_input takes the input object, and sends the actor every key event this frame.
74 75 76 77 78 79 80 81 82 |
# File 'lib/shattered_model/input.rb', line 74 def update_input #:nodoc: if !@controls.nil? && @controls.active? @controls.actions.each do |condition, action| if(keyboard_listener.instance_eval(condition)) instance_eval(action) end end end end |