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

- (Director) initialize

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

- (Object) actors

Returns the value of attribute actors



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

def actors
  @actors
end

- (Object) update_slots

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

- (Object) clear_subscriptions



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

- (Object) pause



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

def pause
  @paused_subscriptions = @subscriptions
  clear_subscriptions
end

- (Object) unpause



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

- (Object) unsubscribe_all(listener)



57
58
59
# File 'lib/gamebox/core/director.rb', line 57

def unsubscribe_all(listener)
  @unsubscriptions << listener
end

- (Object) update(time)



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

- (Object) when(event = :update, &callback)



28
29
30
# File 'lib/gamebox/core/director.rb', line 28

def when(event=:update, &callback)
  @new_subscriptions << [event, callback]
end