-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.lua
More file actions
66 lines (56 loc) · 1.54 KB
/
timer.lua
File metadata and controls
66 lines (56 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
-- Gestion de timers aléatoires
-- Chaque timer peut être relancé automatiquement avec un délai aléatoire
-- situé entre le minimum et le maximum
local timers = {}
function updateTimers(dt)
for _, timer in ipairs(timers) do
timer.update(dt)
end
end
function newTimer(delayMin, delayMax, callback, restart)
local timer = {}
timer.currentTime = 0
timer.started = false
timer.minimum = delayMin
timer.maximum = delayMax
if restart == nil then
restart = true
end
timer.restart = restart
timer.update = function(dt)
if timer.started == true then
timer.currentTime = timer.currentTime + dt
if timer.expired() == true then
timer.started = false
timer.callback()
-- on redémarre le timer aléatoirement
if timer.restart == true then
timer.start()
end
end
end
end
timer.setDelays = function(min, max)
timer.minimum = min
timer.maximum = max
end
timer.expired = function()
return timer.currentTime >= timer.delay
end
timer.start = function()
timer.delay = math.random(timer.minimum, timer.maximum)
timer.currentTime = 0
timer.started = true
end
timer.callback = callback
table.insert(timers, timer)
if timer.restart == true then
timer.start()
end
return timer
end
function razTimers()
for i = #timers, 1, -1 do
table.remove(timers, i)
end
end