-- Scheduler

Scheduler = {}


--------------------------------------------------------------------------------
function Scheduler:new(callback)
    obj = {}
	setmetatable(obj, self)
	self.__index = self

    obj.time = 0.0
    obj.stamps = {}
    obj.callback = callback

    return obj
end


--------------------------------------------------------------------------------
function Scheduler:addStamp(timeSpan, stampName)
    table.insert(self.stamps, {time = self.time + timeSpan, stamp = stampName})
    table.sort(self.stamps, function(a, b) return a.time < b.time end)
end


--------------------------------------------------------------------------------
function Scheduler:removeStamps(stampName)
    for i = #self.stamps, 1, -1 do
        if self.stamps[i].name == stampName then
            table.remove(self.stamps, i)
        end
    end
end


--------------------------------------------------------------------------------
function Scheduler:update(elapsed)
    self.time = self.time + elapsed

    local pass = {}
    for i = 1, #self.stamps do
        if self.stamps[i].time <= self.time then
            table.insert(pass, self.stamps[i].stamp)
        end
    end

    for i = 1, #pass do
        table.remove(self.stamps, 1)
    end

    table.foreach(pass, self.callback)
end

