Sha256: 278d256bb4e5589fc4afb16b8ff519e8aa267c797bb080bbcdc61e8614172e78

Contents?: true

Size: 835 Bytes

Versions: 24

Compression:

Stored size: 835 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, &code
    @threads << [time, code]
  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

24 entries across 24 versions & 1 rubygems

Version Path
gosu_extensions-0.1.3 lib/scheduling.rb
gosu_extensions-0.1.2 lib/scheduling.rb
gosu_extensions-0.1.1 lib/scheduling.rb
gosu_extensions-0.1.0 lib/scheduling.rb