Skip to content

Commit

Permalink
Added a timer module
Browse files Browse the repository at this point in the history
  • Loading branch information
Björn Ritzl committed Nov 3, 2016
1 parent b0d2619 commit d5e93df
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 0 deletions.
37 changes: 37 additions & 0 deletions examples/timer/timer.collection
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: "timer"
scale_along_z: 0
embedded_instances {
id: "go"
data: "components {\n"
" id: \"script\"\n"
" component: \"/examples/timer/timer.script\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}
19 changes: 19 additions & 0 deletions examples/timer/timer.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
local timer = require "ludobits.m.timer"

function init(self)
self.t1 = timer.once(3, function()
print("t1 has fired")
end)
self.t2 = timer.every(1, function()
print("t2 has fired")
end)
self.t3 = timer(5, function()
print("t3 has fired")
end)
end

function update(self, dt)
self.t1.update(dt)
self.t2.update(dt)
self.t3.update(dt)
end
74 changes: 74 additions & 0 deletions ludobits/m/timer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
--- Simple timer module to delay a function call or
-- repeatedly call a function
--
-- @usage
-- function init(self)
-- self.t1 = timer.once(3, function()
-- print("t1 has fired")
-- end)
-- self.t2 = timer.every(1, function()
-- print("t2 has fired")
-- end)
-- self.t3 = timer(5, function()
-- print("t3 has fired")
-- end)
-- end
--
-- function update(self, dt)
-- self.t1.update(dt)
-- self.t2.update(dt)
-- self.t3.update(dt)
-- end

local M = {}


--- Create a timer that will call a function after a certain time
-- has elapsed.
-- @param seconds The number of seconds before the callback should be invoked
-- @param callback The function to invoke when the time has elapsed
-- @return The timer instance
function M.once(seconds, callback)
assert(seconds and type(seconds) == "number" and seconds >= 0, "You must provide a positive number")
assert(callback, "You must provide a callback")

local timer = {
seconds = seconds,
}

function timer.update(dt)
assert(dt and type(dt) == "number", "You must provide a dt")
if timer.seconds <= 0 then
return
end
timer.seconds = timer.seconds - dt
if timer.seconds <= 0 then
callback(timer)
end
end

function timer.reset()
timer.seconds = seconds
end

return timer
end

--- Create a timer that will call a function with a regular interval
-- @param seconds The number of seconds between callbacks
-- @param callback The function to invoke
-- @return The timer instance
function M.every(seconds, callback)
local timer = M.once(seconds, function(timer)
callback(timer)
timer.seconds = seconds
end)
return timer
end


return setmetatable(M, {
__call = function(self, ...)
return M.once(...)
end
})
2 changes: 2 additions & 0 deletions test/test.script
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
local test_input = require "test.test_input"
local test_broadcast = require "test.test_broadcast"
local test_flow = require "test.test_flow"
local test_timer = require "test.test_timer"

local deftest = require "deftest.deftest"

function init(self)
deftest.add(test_input)
deftest.add(test_broadcast)
deftest.add(test_flow)
deftest.add(test_timer)
deftest.run()
end
58 changes: 58 additions & 0 deletions test/test_timer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
local mock = require "deftest.mock"

return function()
local timer = require "ludobits.m.timer"

local o = {
cb = function() end
}

describe("timer", function()
before(function()
mock.mock(o)
end)

after(function()
mock.unmock(o)
end)

it("should call a function after a certain time has elapsed", function()
local t = timer.once(3, o.cb)
t.update(2)
assert(o.cb.calls == 0)
t.update(1)
assert(o.cb.calls == 1)
t.update(3)
assert(o.cb.calls == 1)
end)

it("should be possible to reset a timer", function()
local t = timer.once(3, o.cb)
t.update(3)
assert(o.cb.calls == 1)
t.reset()
t.update(3)
assert(o.cb.calls == 2)
end)

it("should be possible to directly manipulate timer delay", function()
local t = timer.once(3, o.cb)
assert(t.seconds)
t.seconds = 5
t.update(3)
assert(o.cb.calls == 0)
t.update(2)
assert(o.cb.calls == 1)
end)

it("should be possible to repeat a callback with a regular interval", function()
local t = timer.every(3, o.cb)
t.update(3)
assert(o.cb.calls == 1)
t.update(3)
assert(o.cb.calls == 2)
t.update(3)
assert(o.cb.calls == 3)
end)
end)
end

0 comments on commit d5e93df

Please sign in to comment.