Class: Lotu::InputSystem
- Inherits:
-
Object
- Object
- Lotu::InputSystem
- Defined in:
- lib/lotu/systems/input_system.rb
Defined Under Namespace
Modules: UserMethods
Instance Method Summary collapse
- #button_down(key) ⇒ Object
- #button_up(key) ⇒ Object
- #draw ⇒ Object
-
#initialize(user, opts = {}) ⇒ InputSystem
constructor
A new instance of InputSystem.
- #set_keys(client, keys) ⇒ Object
-
#update ⇒ Object
If there are some actions currently going on, dispatch them.
Constructor Details
#initialize(user, opts = {}) ⇒ InputSystem
Returns a new instance of InputSystem.
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/lotu/systems/input_system.rb', line 4 def initialize(user, opts={}) user.extend UserMethods # Current ongoing actions (button is being pushed) @actions = [] @unique_actions = [] # Last time an action was executed (so we can implement some # rate of fire) @last_time_fired = Hash.new{|h,k| h[k] = Hash.new{|h,k| h[k] = 0}} # Think of it like a reverse proxy @reverse_register = Hash.new{|h,k| h[k] = []} end |
Instance Method Details
#button_down(key) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/lotu/systems/input_system.rb', line 41 def (key) @reverse_register[key].each do |client, action| action_name, rate = action if rate == false @unique_actions << [client, action_name, rate] else @actions << [client, action_name, rate] end end end |
#button_up(key) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/lotu/systems/input_system.rb', line 52 def (key) @reverse_register[key].each do |client, action| action_name, rate = action @actions.delete [client, action_name, rate] end end |
#draw ⇒ Object
59 |
# File 'lib/lotu/systems/input_system.rb', line 59 def draw;end |
#set_keys(client, keys) ⇒ Object
35 36 37 38 39 |
# File 'lib/lotu/systems/input_system.rb', line 35 def set_keys(client, keys) keys.each do |key, action| @reverse_register[key] << [client, action] end end |
#update ⇒ Object
If there are some actions currently going on, dispatch them
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/lotu/systems/input_system.rb', line 19 def update @unique_actions.each do |client, action_name, rate| client.send(action_name) end.clear @actions.each do |client, action_name, rate| # action usually is a [:action_name, rate_of_fire] pair, for # example: [:fire, 50] will call #fire every 50ms time_now = Gosu.milliseconds if @last_time_fired[client][action_name] + (rate || 0) < time_now client.send(action_name) @last_time_fired[client][action_name] = time_now end end end |