Class: Director

Inherits:
Object show all
Defined in:
lib/gamebox/core/director.rb

Overview

Directors manage actors.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDirector



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

#actorsObject

Returns the value of attribute actors



3
4
5
# File 'lib/gamebox/core/director.rb', line 3

def actors
  @actors
end

#update_slotsObject

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_subscriptionsObject



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

#pauseObject



16
17
18
19
# File 'lib/gamebox/core/director.rb', line 16

def pause
  @paused_subscriptions = @subscriptions
  clear_subscriptions
end

#unpauseObject



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