managed expired state inside the timer rather than just checking if the time has expired; makes sure the callback fires even for zero length timers

This commit is contained in:
Max Cahill 2022-06-07 14:09:35 +10:00
parent 795f2587d2
commit dbce7e6083

View File

@ -22,6 +22,7 @@ function timer:new(time, on_progress, on_finish)
self.timer = 0
self.on_progress = on_progress
self.on_finish = on_finish
self.has_expired = false
self:reset(time)
end
@ -30,7 +31,8 @@ function timer:update(dt)
if not self:expired() then
self.timer = self.timer + dt
--get the relevant callback
--set the expired state and get the relevant callback
self.has_expired = self.timer >= self.time
local cb = self:expired()
and self.on_finish
or self.on_progress
@ -43,7 +45,7 @@ end
--check if the timer has expired
function timer:expired()
return self.timer >= self.time
return self.has_expired
end
--get the timer's progress from 0 to 1