Class: Lotu::InputManagerSystem

Inherits:
BaseSystem show all
Defined in:
lib/lotu/systems/input_manager_system.rb

Defined Under Namespace

Modules: UserMethods

Instance Method Summary collapse

Methods inherited from BaseSystem

#draw, #dt

Constructor Details

#initialize(user, opts = {}) ⇒ InputManagerSystem

Returns a new instance of InputManagerSystem.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/lotu/systems/input_manager_system.rb', line 4

def initialize(user, opts={})
  super
  @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



42
43
44
45
46
47
48
49
50
51
# File 'lib/lotu/systems/input_manager_system.rb', line 42

def button_down(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



53
54
55
56
57
58
# File 'lib/lotu/systems/input_manager_system.rb', line 53

def button_up(key)
  @reverse_register[key].each do |client, action|
    action_name, rate = action
    @actions.delete [client, action_name, rate]
  end
end

#set_keys(client, keys) ⇒ Object



36
37
38
39
40
# File 'lib/lotu/systems/input_manager_system.rb', line 36

def set_keys(client, keys)
  keys.each do |key, action|
    @reverse_register[key] << [client, action]
  end
end

#updateObject

If there are some actions currently going on, dispatch them



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/lotu/systems/input_manager_system.rb', line 20

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