Class: Director
Overview
Directors manage actors.
Instance Attribute Summary collapse
-
#actors ⇒ Object
Returns the value of attribute actors.
-
#update_slots ⇒ Object
Returns the value of attribute update_slots.
Instance Method Summary collapse
- #clear_subscriptions ⇒ Object
-
#initialize ⇒ Director
constructor
A new instance of Director.
- #pause ⇒ Object
- #unpause ⇒ Object
- #unsubscribe_all(listener) ⇒ Object
- #update(time) ⇒ Object
- #when(event = :update, &callback) ⇒ Object
Constructor Details
#initialize ⇒ Director
Returns a new instance of Director.
5 6 7 8 |
# File 'lib/gamebox/core/director.rb', line 5 def initialize @update_slots = [ :pre_update, :update, :post_update ] clear_subscriptions end |
Instance Attribute Details
#actors ⇒ Object
Returns the value of attribute actors.
3 4 5 |
# File 'lib/gamebox/core/director.rb', line 3 def actors @actors end |
#update_slots ⇒ Object
Returns the value of attribute update_slots.
3 4 5 |
# File 'lib/gamebox/core/director.rb', line 3 def update_slots @update_slots end |
Instance Method Details
#clear_subscriptions ⇒ Object
10 11 12 13 14 |
# File 'lib/gamebox/core/director.rb', line 10 def clear_subscriptions @subscriptions = Hash[@update_slots.map { |slot| [slot, []] }] @new_subscriptions = [] @unsubscriptions = [] end |
#pause ⇒ Object
16 17 18 19 |
# File 'lib/gamebox/core/director.rb', line 16 def pause @paused_subscriptions = @subscriptions clear_subscriptions end |
#unpause ⇒ Object
21 22 23 24 25 26 |
# File 'lib/gamebox/core/director.rb', line 21 def unpause unless @paused_subscriptions.nil? @subscriptions = @paused_subscriptions @paused_subscriptions = nil end end |
#unsubscribe_all(listener) ⇒ Object
57 58 59 |
# File 'lib/gamebox/core/director.rb', line 57 def unsubscribe_all(listener) @unsubscriptions << listener end |
#update(time) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/gamebox/core/director.rb', line 32 def update(time) @new_subscriptions.each do |(event, callback)| @subscriptions[event] ||= [] @subscriptions[event] << callback end @new_subscriptions.clear @unsubscriptions.each do |listener| for slot in @subscriptions.keys @subscriptions[slot].delete_if do |block| eval('self',block.binding).equal?(listener) end end end @unsubscriptions.clear time_in_seconds = time / 1000.to_f @update_slots.each do |slot| @subscriptions[slot].each do |callback| callback.call time, time_in_seconds end end end |
#when(event = :update, &callback) ⇒ Object
28 29 30 |
# File 'lib/gamebox/core/director.rb', line 28 def when(event=:update, &callback) @new_subscriptions << [event, callback] end |