Skip to content

Commit d5e93df

Browse files
author
Björn Ritzl
committed
Added a timer module
1 parent b0d2619 commit d5e93df

File tree

5 files changed

+190
-0
lines changed

5 files changed

+190
-0
lines changed

examples/timer/timer.collection

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: "timer"
2+
scale_along_z: 0
3+
embedded_instances {
4+
id: "go"
5+
data: "components {\n"
6+
" id: \"script\"\n"
7+
" component: \"/examples/timer/timer.script\"\n"
8+
" position {\n"
9+
" x: 0.0\n"
10+
" y: 0.0\n"
11+
" z: 0.0\n"
12+
" }\n"
13+
" rotation {\n"
14+
" x: 0.0\n"
15+
" y: 0.0\n"
16+
" z: 0.0\n"
17+
" w: 1.0\n"
18+
" }\n"
19+
"}\n"
20+
""
21+
position {
22+
x: 0.0
23+
y: 0.0
24+
z: 0.0
25+
}
26+
rotation {
27+
x: 0.0
28+
y: 0.0
29+
z: 0.0
30+
w: 1.0
31+
}
32+
scale3 {
33+
x: 1.0
34+
y: 1.0
35+
z: 1.0
36+
}
37+
}

examples/timer/timer.script

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
local timer = require "ludobits.m.timer"
2+
3+
function init(self)
4+
self.t1 = timer.once(3, function()
5+
print("t1 has fired")
6+
end)
7+
self.t2 = timer.every(1, function()
8+
print("t2 has fired")
9+
end)
10+
self.t3 = timer(5, function()
11+
print("t3 has fired")
12+
end)
13+
end
14+
15+
function update(self, dt)
16+
self.t1.update(dt)
17+
self.t2.update(dt)
18+
self.t3.update(dt)
19+
end

ludobits/m/timer.lua

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
--- Simple timer module to delay a function call or
2+
-- repeatedly call a function
3+
--
4+
-- @usage
5+
-- function init(self)
6+
-- self.t1 = timer.once(3, function()
7+
-- print("t1 has fired")
8+
-- end)
9+
-- self.t2 = timer.every(1, function()
10+
-- print("t2 has fired")
11+
-- end)
12+
-- self.t3 = timer(5, function()
13+
-- print("t3 has fired")
14+
-- end)
15+
-- end
16+
--
17+
-- function update(self, dt)
18+
-- self.t1.update(dt)
19+
-- self.t2.update(dt)
20+
-- self.t3.update(dt)
21+
-- end
22+
23+
local M = {}
24+
25+
26+
--- Create a timer that will call a function after a certain time
27+
-- has elapsed.
28+
-- @param seconds The number of seconds before the callback should be invoked
29+
-- @param callback The function to invoke when the time has elapsed
30+
-- @return The timer instance
31+
function M.once(seconds, callback)
32+
assert(seconds and type(seconds) == "number" and seconds >= 0, "You must provide a positive number")
33+
assert(callback, "You must provide a callback")
34+
35+
local timer = {
36+
seconds = seconds,
37+
}
38+
39+
function timer.update(dt)
40+
assert(dt and type(dt) == "number", "You must provide a dt")
41+
if timer.seconds <= 0 then
42+
return
43+
end
44+
timer.seconds = timer.seconds - dt
45+
if timer.seconds <= 0 then
46+
callback(timer)
47+
end
48+
end
49+
50+
function timer.reset()
51+
timer.seconds = seconds
52+
end
53+
54+
return timer
55+
end
56+
57+
--- Create a timer that will call a function with a regular interval
58+
-- @param seconds The number of seconds between callbacks
59+
-- @param callback The function to invoke
60+
-- @return The timer instance
61+
function M.every(seconds, callback)
62+
local timer = M.once(seconds, function(timer)
63+
callback(timer)
64+
timer.seconds = seconds
65+
end)
66+
return timer
67+
end
68+
69+
70+
return setmetatable(M, {
71+
__call = function(self, ...)
72+
return M.once(...)
73+
end
74+
})

test/test.script

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
local test_input = require "test.test_input"
22
local test_broadcast = require "test.test_broadcast"
33
local test_flow = require "test.test_flow"
4+
local test_timer = require "test.test_timer"
45

56
local deftest = require "deftest.deftest"
67

78
function init(self)
89
deftest.add(test_input)
910
deftest.add(test_broadcast)
1011
deftest.add(test_flow)
12+
deftest.add(test_timer)
1113
deftest.run()
1214
end

test/test_timer.lua

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
local mock = require "deftest.mock"
2+
3+
return function()
4+
local timer = require "ludobits.m.timer"
5+
6+
local o = {
7+
cb = function() end
8+
}
9+
10+
describe("timer", function()
11+
before(function()
12+
mock.mock(o)
13+
end)
14+
15+
after(function()
16+
mock.unmock(o)
17+
end)
18+
19+
it("should call a function after a certain time has elapsed", function()
20+
local t = timer.once(3, o.cb)
21+
t.update(2)
22+
assert(o.cb.calls == 0)
23+
t.update(1)
24+
assert(o.cb.calls == 1)
25+
t.update(3)
26+
assert(o.cb.calls == 1)
27+
end)
28+
29+
it("should be possible to reset a timer", function()
30+
local t = timer.once(3, o.cb)
31+
t.update(3)
32+
assert(o.cb.calls == 1)
33+
t.reset()
34+
t.update(3)
35+
assert(o.cb.calls == 2)
36+
end)
37+
38+
it("should be possible to directly manipulate timer delay", function()
39+
local t = timer.once(3, o.cb)
40+
assert(t.seconds)
41+
t.seconds = 5
42+
t.update(3)
43+
assert(o.cb.calls == 0)
44+
t.update(2)
45+
assert(o.cb.calls == 1)
46+
end)
47+
48+
it("should be possible to repeat a callback with a regular interval", function()
49+
local t = timer.every(3, o.cb)
50+
t.update(3)
51+
assert(o.cb.calls == 1)
52+
t.update(3)
53+
assert(o.cb.calls == 2)
54+
t.update(3)
55+
assert(o.cb.calls == 3)
56+
end)
57+
end)
58+
end

0 commit comments

Comments
 (0)