Merge pull request #68 from Kingdaro/kingdaro-timer-fix

Timer now accounts for "lateness" and repetition
This commit is contained in:
vrld 2016-05-24 13:29:58 +02:00
commit 400c8ea75e
3 changed files with 108 additions and 23 deletions

73
spec/timer_spec.lua Normal file
View File

@ -0,0 +1,73 @@
local timer = require 'timer'()
describe('hump.timer', function()
it('runs a function during a specified time', function()
local delta, remaining
timer:during(10, function(...) delta, remaining = ... end)
timer:update(2)
assert.are.equal(delta, 2)
assert.are.equal(8, remaining)
timer:update(5)
assert.are.equal(delta, 5)
assert.are.equal(3, remaining)
timer:update(10)
assert.are.equal(delta, 10)
assert.are.equal(0, remaining)
end)
it('runs a function after a specified time', function()
local finished1 = false
local finished2 = false
timer:after(3, function(...) finished1 = true end)
timer:after(5, function(...) finished2 = true end)
timer:update(4)
assert.are.equal(true, finished1)
assert.are.equal(false, finished2)
timer:update(4)
assert.are.equal(true, finished1)
assert.are.equal(true, finished2)
end)
it('runs a function every so often', function()
local count = 0
timer:every(1, function(...) count = count + 1 end)
timer:update(3)
assert.are.equal(3, count)
timer:update(7)
assert.are.equal(10, count)
end)
it('can script timed events', function()
local state
timer:script(function(wait)
state = 'foo'
wait(1)
state = 'bar'
end)
assert.are.equal('foo', state)
timer:update(0.5)
assert.are.equal('foo', state)
timer:update(1)
assert.are.equal('bar', state)
end)
it('cancels and clears timer functions', function()
pending('to be tested...')
end)
it('tweens', function()
pending('to be tested...')
end)
end)

View File

@ -31,23 +31,41 @@ local function _nothing_() end
function Timer:update(dt)
local to_remove = {}
for handle, delay in pairs(self.functions) do
delay = delay - dt
if delay <= 0 then
to_remove[#to_remove+1] = handle
for handle in pairs(self.functions) do
-- handle: {
-- time = <number>,
-- after = <function>,
-- during = <function>,
-- limit = <number>,
-- count = <number>,
-- }
handle.time = handle.time + dt
handle.during(dt, math.max(handle.limit - handle.time, 0))
while handle.time >= handle.limit and handle.count > 0 do
if handle.after(handle.after) == false then
handle.count = 0
break
end
self.functions[handle] = delay
handle.func(dt, delay)
handle.time = handle.time - handle.limit
handle.count = handle.count - 1
end
for _,handle in ipairs(to_remove) do
self.functions[handle] = nil
handle.after(handle.after)
if handle.count == 0 then
table.insert(to_remove, handle)
end
end
for i = 1, #to_remove do
self.functions[to_remove[i]] = nil
end
end
function Timer:during(delay, func, after)
local handle = {func = func, after = after or _nothing_}
self.functions[handle] = delay
function Timer:during(delay, during, after)
local handle = { time = 0, during = during, after = after or _nothing_, limit = delay, count = 1 }
self.functions[handle] = true
return handle
end
@ -55,16 +73,10 @@ function Timer:after(delay, func)
return self:during(delay, _nothing_, func)
end
function Timer:every(delay, func, count)
local count, handle = count or math.huge -- exploit below: math.huge - 1 = math.huge
handle = self:after(delay, function(f)
if func(func) == false then return end
count = count - 1
if count > 0 then
self.functions[handle] = delay
end
end)
function Timer:every(delay, after, count)
local count = count or math.huge -- exploit below: math.huge - 1 = math.huge
local handle = { time = 0, during = _nothing_, after = after, limit = delay, count = count }
self.functions[handle] = true
return handle
end