Sha256: 7805a866e7c94106460d5bd2f2369cc1d77029eebc0cbf3f5cfd7630db6a7fab

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

# Directors manage actors.
class Director
  attr_accessor :actors, :update_slots

  def initialize
    @update_slots = [ :pre_update, :update, :post_update ]
    clear_subscriptions
  end

  def clear_subscriptions
    @subscriptions = Hash[@update_slots.map { |slot| [slot, []] }]
  end

  def pause
    @paused_subscriptions = @subscriptions
    clear_subscriptions
  end

  def unpause
    unless @paused_subscriptions.nil?
      @subscriptions = @paused_subscriptions
      @paused_subscriptions = nil
    end
  end

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

  def update(time)
    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

  def unsubscribe_all(listener)
    if @subscriptions
      for slot in @subscriptions.keys
        @subscriptions[slot].delete_if do |block|
          eval('self',block.binding).equal?(listener)
        end
      end
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gamebox-0.4.0 lib/gamebox/core/director.rb
gamebox-0.4.0.rc11 lib/gamebox/core/director.rb