-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTickSystem.js
More file actions
325 lines (286 loc) · 7.33 KB
/
TickSystem.js
File metadata and controls
325 lines (286 loc) · 7.33 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
"use strict";
/**
*
* Tick System for executing Tasks at a specific tick rate.
*
* @author SteffTek
* @class
* @public
*
*/
class TickSystem {
/**
*
* Creates a new Tick System.
* @since 1.0.0
* @constructor
*
* @param {number} tickRate how many times per second a tick is executed
* @var {array} callbacks all functions that should be executed on tick
* @var {number} tickRate how many ticks in a second
* @var {number} currentTick current tick of the second
* @var {number} tickLatency time in ms since last tick
* @var {number} tickDelta time in seconds since last tick
* @var {number} lastTick time stamp of last tick
* @var {number} tickTime length of tick in ms
* @var {PerformanceMonitor} performanceMonitor monitors performance of ticks
*
*/
constructor(tickRate = 64){
// STORE THE FUNCTIONS TO EXECUTE
this.callbacks = [];
// STORE TICK VARS
this.tickRate = tickRate;
this.currentTick = 0;
this.tickLatency = 0;
this.tickDelta = 0;
this.lastTick = Date.now();
this.tickTime = 1000 / this.tickRate;
// OTHERS
this.interval = null;
this.performanceMonitor = null;
// EXECUTE TICK FOR x TIMES A SECOND
this.start();
}
/**
*
* Register a function to be executed on tick.
*
* @param {function} callback register function to tick system
*
*/
onTick(callback){
// ADD CALLBACK FROM TICK
this.callbacks.push(callback);
}
/**
*
* Unregister a function that is beeing executed.
*
* @param {function} callback unregister function to tick system
*
*/
offTick(callback){
// REMOVE CALLBACK FROM TICK
const index = this.callbacks.indexOf(callback);
if (index > -1){
this.callbacks.splice(index, 1);
}
}
/**
*
* Executes callback after x seconds
* @since 1.1.0
*
* @param {number} seconds amount of seconds to wait
* @param {function} callback callback to execute
*
*/
executeAfterSeconds(seconds, callback){
// CALCULATE TICKS
const ticks = seconds * this.tickRate;
// SEND TO EXECUTION
this.executeAfter(ticks, callback);
}
/**
*
* Executes callback after x ticks
* @since 1.1.0
*
* @param {number} ticks amount of ticks to wait
* @param {function} callback callback to execute
*
*/
executeAfter(ticks, callback){
// SAVE CLASS
const tickSystem = this;
// WAIT FUNCTION
let count = 0;
const wait = function(){
// EXECUTE CALLBACK
if(count >= ticks){
// REMOVE TICK
tickSystem.offTick(wait);
// EXECUTE CALLBACK
callback();
return;
}
// INCREASE COUNT
count++;
};
// START TICKING
this.onTick(wait);
}
/**
*
* Executes a tick.
*
*/
tick(){
// EXECUTE ALL CALLBACKS
const callbacks = this.callbacks.slice(0);
callbacks.forEach((callback) => {
callback();
});
// SET CURRENT TICK
this.currentTick++;
if (this.currentTick + 1 > this.tickRate){
this.currentTick = 0;
}
// CALC TICK LATENCY
let timeStamp = Date.now();
this.tickLatency = timeStamp - this.lastTick; // TICK LATENCY IN MS
this.tickDelta = this.tickLatency / 1000; // TICK LATENCY IN SECONDS
this.lastTick = timeStamp; // SET THIS TICKS TIMESTAMP
// SEND TO MONITOR
if (this.performanceMonitor) this.performanceMonitor.capture();
}
/**
*
* Stop tick execution
*
*/
start(){
// CHECK FOR NON INTERVAL
if (this.interval){
return;
}
// START TICK
this.interval = setInterval(() => {
// EXECUTE TICK
this.tick();
}, this.tickTime);
}
/**
*
* Stop tick execution
*
*/
stop(){
// CHECK IF INTERVAL EXISTS
if (!this.interval){
return;
}
// REMOVE TICK COUNTER
clearInterval(this.interval);
// RESET INTERVAL
this.interval = null;
}
/**
*
* Monitors the performance
*
* @param {boolean} doMonitor monitor the performance per tick
*
*/
monitor(doMonitor = false){
// DISABLE MONITORING
if (!doMonitor){
this.performanceMonitor = null;
return;
}
// ENABLE MONITORING
// eslint-disable-next-line no-use-before-define
this.performanceMonitor = new PerformanceMonitor(this);
}
}
/**
*
* Performance Monitor for Tick System
* @class PerformanceMonitor
*
*/
class PerformanceMonitor {
/**
*
* Creates a new performance monitor
* @since 1.0.0
*
* @param {TickSystem} tickSystem monitored object
*
*/
constructor(tickSystem){
this.tickStore = [];
this.tickSystem = tickSystem;
}
/**
*
* Monitors the Performance
*
*/
capture(){
// ADD TIMESTAMP OF LAST TICK TO STORE STORE
this.tickStore.unshift(this.tickSystem.lastTick);
// REMOVE TOO OLD TICKS
if (this.tickStore.length > this.tickSystem.tickRate * 5){
this.tickStore.pop();
}
}
/**
*
* Monitors the Performance
*
* @return {object} performance report of last Tick, past second and past 5 seconds in percent
*
*/
report(){
// CREATE OBJECT
let report = {
tick: this.singleReport(2),
second: this.singleReport(this.tickSystem.tickRate),
interval: this.singleReport(this.tickSystem.tickRate * 5)
};
// RETURN REPORT
return report;
}
/**
*
* Monitors the Performance
*
* @param {number} length amount of ticks to analyze
*
* @return {number} performance of last x frames in percent
*
*/
singleReport(length){
// FALLBACK SIZE
let size = length;
if (size > this.tickStore.length){
size = this.tickStore.length;
}
// FALLBACK MIN SIZE
if (this.tickStore.length <= 2){
return null;
}
let percentages = [];
// CYCLE
for (let i = 0; i < size - 1; i++){
// GET TIMINGS
let tStart = this.tickStore[i];
let tEnd = this.tickStore[i + 1];
// CALCULATE PERCENT TO LAST TICK
let time = tStart - tEnd;
let percent = time / this.tickSystem.tickTime;
// PUSH PERCENT
percentages.push(percent);
}
let total = 0;
// GET AVERAGE
for (let i = 0; i < percentages.length; i++){
total += percentages[i];
}
let avg = total / percentages.length;
// CONVERT TO READABLE PERCENT
avg *= 100;
avg = Math.floor(100 - (avg - 100));
return avg;
}
}
/**
*
* Export to NodeJS if inside of NodeJS
*
*/
if (typeof module !== "undefined" && module.exports){
module.exports = TickSystem;
}