Sha256: b5450ceb50d5c92ce462831eb850a1f03c4dd33c97dbd30b1c6a4ccb13497c80

Contents?: true

Size: 855 Bytes

Versions: 11

Compression:

Stored size: 855 Bytes

Contents

# "Threading"
# A hash with time => [block, block, block ...]
#
# {
#   100 => [{bla}, {blu}, {bli}],
#   1   => [{eek}]
# }
#
# When calling threading.step, eek will be executed, the others will be one step closer to zero, 99.
#
class Scheduling
  
  def initialize
    @threads = []
  end
  
  # Adds a code block at time time.
  #
  def add time = 1, proc = nil, &code
    @threads << [time, code || proc]
  end
  
  # Does two things:
  # 1. Move one step in time.
  # 2. Execute all blocks with time 0.
  #
  # TODO Rewrite to be faster.
  #
  # FIXME - threads added while threads are handled!
  #
  def step
    @threads.collect! do |time, code|
      if time == 1
        code.call
        nil
      else
        [time-1, code]
      end
    end.compact!
  end
  
  # Call all given blocks.
  #
  def execute codes
    codes.each &:[]
  end
  
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
gosu_extensions-0.2.5 lib/core/scheduling.rb
gosu_extensions-0.2.4 lib/core/scheduling.rb
gosu_extensions-0.2.3 lib/core/scheduling.rb
gosu_extensions-0.2.2 lib/core/scheduling.rb
gosu_extensions-0.2.1 lib/core/scheduling.rb
gosu_extensions-0.2.0 lib/core/scheduling.rb
gosu_extensions-0.1.28 lib/core/scheduling.rb
gosu_extensions-0.1.27 lib/core/scheduling.rb
gosu_extensions-0.1.26 lib/core/scheduling.rb
gosu_extensions-0.1.25 lib/core/scheduling.rb
gosu_extensions-0.1.24 lib/core/scheduling.rb