-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.lua
More file actions
108 lines (101 loc) · 2.42 KB
/
timer.lua
File metadata and controls
108 lines (101 loc) · 2.42 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
--[[
local timer=require("timer")
local function test_timer(timer,...)
print(os.time(),...)
end
timer.new_timer(1,1,test_timer,"my timer A")
timer.new_timer(2,1,test_timer,"my timer B")
timer.new_timer(4,1,test_timer,"my timer D")
timer.new_timer(3,1,test_timer,"my timer C")
timer.new_timer(1,timer.infinite,test_timer,"my timer loop")
local lt=os.time()
while(timer.ct<5) do
local ct=os.time()
timer.process(ct-lt)
lt=ct
end
]]--
local t={
use_system_time=false,
ct=0,
infinite=-1,
}
t.timers={}
t.count=0
local function _get_ct()
return t.use_system_time and os.time() or t.ct
end
local function add_timer(timer,loop_offset)
if(type(timer.loop_times)~="number") then
timer.loop_times=0
elseif(loop_offset and timer.loop_times>=0) then
timer.loop_times=math.max(timer.loop_times+loop_offset,0)
end
if(t.count==0) then
t.count=t.count+1
table.insert(t.timers,timer)
return timer
else
t.count=t.count+1
for i,v in ipairs(t.timers) do
if(timer.tt>v.tt) then
table.insert(t.timers,i,timer)
return timer
end
end
table.insert(t.timers,timer)
return timer
end
end
function t.new_timer(t_seconds,loop_times,func,...)
if(type(t_seconds)=="table") then
t_seconds.tt=_get_ct()+t_seconds.period
return add_timer(t_seconds,-1)
else
return add_timer({tt=_get_ct()+t_seconds,period=t_seconds,loop_times=loop_times,func=func,arg={...}},-1)
end
end
function t.remove_timer(timer)
for i,v in ipairs(t.timers) do
if(timer==v) then
table.remove(t.timers,i)
t.count=t.count-1
return timer
end
end
end
function t.process(tm)
if(tm) then
t.ct=t.ct+tm
end
if(not next(t.timers)) then return end
local ct=_get_ct()
if(ct<t.timers[t.count].tt) then return end
local tl={}
local nt
for index=t.count,1,-1 do
nt=t.timers[index]
if(ct<nt.tt) then break end
if(nt.func) then
local temp=nt.func(nt,table.unpack(nt.arg))
if(nt.loop_times~=0 and temp~=false) then
if(t.continue) then
nt.tt=nt.tt+nt.period
else
nt.tt=ct+nt.period
end
table.insert(tl,nt)
if(nt.loop_times>0) then
nt.loop_times=nt.loop_times-1
if(nt.loop_times<0) then nt.loop_times=0 end
end
end
end
table.remove(t.timers)
t.count=t.count-1
end
for i,v in ipairs(tl) do
add_timer(v)
end
end
return t