Clock.lua |
|
---|---|
A Clock takes a callback function and a delay time and calls the function at that interval. |
module(..., package.seeall)
local utils = require(_PACKAGE .. 'utils')
local List = require(_PACKAGE .. 'List')
local Clock = utils.public_class('Clock')
|
The list of all Clocks. Used by |
Clock.all = List()
|
InitializeConstructor
|
function Clock:initialize(delay, callback, ...)
assert(delay > 0, "Delay must be greater than 0 seconds")
assert(callback, "Clocks must have a callback, otherwise what's the point?")
self.delay = delay
self.callback = callback
self.elapsed = 0
self.args = {...}
Clock.all:push(self)
end
|
OneoffCreate a Clock that is called once and stops. This essentially just calls the callback after the specified delay. Arguments are the same as the normal constructor. Returns a Clock. |
function Clock.static.oneoff(delay, callback, ...)
assert(delay > 0, "Delay must be greater than 0 seconds")
assert(callback, "Clocks must have a callback, otherwise what's the point?")
local a = {...}
local c
c = Clock(delay, function()
callback(unpack(a))
c:stop()
end)
return c
end
|
UpdateInform this Clock that time has passed; this may cause it to call the callback. You shouldn't call this manually, call the static
|
function Clock:update(dt)
self.elapsed = self.elapsed + dt
if self.elapsed >= self.delay then
self.callback(unpack(self.args))
self.elapsed = 0
end
end
|
StopStop this clock from running. |
function Clock:stop()
if not Clock.all:remove(n) then
error("Clock not active!")
end
end
|
UpdateUpdates all Clocks. This is called automatically by Scene so you should only need to use it if you're not using Scenes.
|
function Clock.static.update(dt)
Clock.all:method_each('update', dt)
end
return Clock
|