FrameworkZ 10.8.3
Provides a framework for Project Zomboid with various systems.
Loading...
Searching...
No Matches
Timers.lua
Go to the documentation of this file.
2
3--! \brief Timers module for FrameworkZ. Allows for the creation of timers for delaying code executions.
4--! \class FrameworkZ.Timers
5FrameworkZ.Timers = {}
6--FrameworkZ.Timers.__index = FrameworkZ.Timers
7FrameworkZ.Timers = FrameworkZ.Foundation:NewModule(FrameworkZ.Timers, "Timers")
8FrameworkZ.Timers.AdvancedTimers = {}
9FrameworkZ.Timers.SimpleTimers = {}
10
11local os_time = os.time
12local table_insert = table.insert
13local table_remove = table.remove
14local assert = assert
15local type = type
16local pairs = pairs
18--! \brief Creates a simple timer that executes a function after a delay.
19--! \param \integer delay The delay in seconds before the function is executed.
20--! \param \function func The function to execute after the delay.
21function FrameworkZ.Timers:Simple(delay, func)
22 assert(type(delay) == "number", "Delay of timer should be a number type")
23 assert(type(func) == "function", "Func of timer should be a function type (lol)")
24
25 table_insert(self.SimpleTimers, {
26 EndTime = os_time() + delay,
27 Func = func
28 })
29end
31function FrameworkZ.Timers:Create(name, delay, repetitions, func)
33 assert(type(name) == "string", "ID of timer should be a string type")
34 assert(type(delay) == "number", "Delay of timer should be a number type")
35 assert(type(repetitions) == "number", "Repetitions of timer should be a number type")
36 assert(type(func) == "function", "Func of timer should be a function type (lol)")
37
38 self.AdvancedTimers[name] = {
39 Delay = delay,
40 StartRepetitions = repetitions,
41 Repetitions = repetitions,
42 Infinity = repetitions == 0,
43 LastFuncTime = os_time(),
44 Func = func,
45 Paused = false,
46 }
47
48end
49
50local function timerUpdate()
52 local cur_time = os_time()
54 local advanced_timers = FrameworkZ.Timers.AdvancedTimers
56 for k, v in pairs(advanced_timers) do
57
58 if not v.Paused then
60 if cur_time >= v.LastFuncTime + v.Delay then
62 v.Func()
63
64 v.LastFuncTime = cur_time
65
66 if not v.Infinity then
67
68 v.Repetitions = v.Repetitions - 1
69
70 if v.Repetitions <= 0 then
71
72 FrameworkZ.Timers.AdvancedTimers[k] = nil
73
74 end
75
76 end
77
78 end
79
80 end
81
82 end
83
84 local simple_timers = FrameworkZ.Timers.SimpleTimers
85
86 for i = #simple_timers, 1, -1 do
87
88 local t = simple_timers[i]
89
90 if t.EndTime <= cur_time then
91
92 t.Func()
93
94 table_remove(simple_timers, i)
95
96 end
97
98 end
99
100end
101Events.OnTickEvenPaused.Add(timerUpdate)
102
103function FrameworkZ.Timers:Remove(name)
104
105 local t = self.AdvancedTimers[name]
106
107 if not t then return false end
108
109 self.AdvancedTimers[name] = nil
110
111 return true
112
113end
114
115function FrameworkZ.Timers:Exists(name)
116
117 return self.AdvancedTimers[name] and true or false
118
119end
120
121function FrameworkZ.Timers:Start(name)
122
123 local t = self.AdvancedTimers[name]
124
125 if not t then return false end
126
127 t.Repetitions = t.StartRepetitions
128 t.LastFuncTime = os_time()
129 t.Paused = false
130 t.PausedTime = nil
131
132 return true
133
134end
135
136function FrameworkZ.Timers:Pause(name)
137
138 local t = self.AdvancedTimers[name]
139
140 if not t then return false end
141
142 if t.Paused then return false end
143
144 t.Paused = true
145 t.PausedTime = os_time()
146
147 return true
148
149end
150
151function FrameworkZ.Timers:UnPause(name)
152
153 local t = self.AdvancedTimers[name]
154
155 if not t then return false end
156
157 if not t.Paused then return false end
158
159 t.Paused = false
160
161 return true
162
163end
164FrameworkZ.Timers.Resume = FrameworkZ.Timers.UnPause
165
166function FrameworkZ.Timers:Toggle(name)
167
168 local t = self.AdvancedTimers[name]
169
170 if not t then return false end
171
172 t.Paused = not t.Paused
173
174 return true
175
176end
177
178function FrameworkZ.Timers:TimeLeft(name)
179
180 local t = self.AdvancedTimers[name]
181
182 if not t then return end
183
184 if t.Paused then
185
186 return (t.Repetitions - 1) * t.Delay + (t.LastFuncTime + t.Delay - t.PausedTime)
187
188 else
189
190 return (t.Repetitions - 1) * t.Delay + (t.LastFuncTime + t.Delay - os_time())
191
192 end
193
194end
195
196function FrameworkZ.Timers:NextTimeLeft(name)
197
198 local t = self.AdvancedTimers[name]
199
200 if not t then return end
201
202 if t.Paused then
203
204 return t.LastFuncTime + t.Delay - t.PausedTime
205
206 else
207
208 return t.LastFuncTime + t.Delay - os_time()
209
210 end
211
212end
213
214function FrameworkZ.Timers:RepsLeft(name)
215
216 local t = self.AdvancedTimers[name]
217
218 return t and t.Repetitions
219
220end
void local advanced_timers()
void local cur_time()
void FrameworkZ()
void local name()
void if type v()
Timers module for FrameworkZ. Allows for the creation of timers for delaying code executions.
Definition Timers.lua:5
FrameworkZ Timers SimpleTimers
Definition Timers.lua:11
void UnPause(name)
void RepsLeft(name)
void NextTimeLeft(name)
void Simple(delay, func)
Creates a simple timer that executes a function after a delay.
FrameworkZ Timers AdvancedTimers
Definition Timers.lua:9
void Create(name, delay, repetitions, func)
void TimeLeft(name)
void for i()