-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·79 lines (62 loc) · 1.59 KB
/
index.js
File metadata and controls
executable file
·79 lines (62 loc) · 1.59 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
const {EventEmitter} = require('events');
module.exports = class Game extends EventEmitter {
constructor(options = {}) {
super();
this.framesPerSecond = options.framesPerSecond || 60;
this.maxFrameSkip = options.maxFrameSkip || 10;
this.waitTime = options.waitTime || 0;
this._nextTick = Date.now();
this._lastTick = 0;
this._updateTicks = 0;
this._running = false;
}
get isRunning() {
return this._running;
}
get updateTicks() {
return this._updateTicks;
}
get currentTick() {
return this._updateTicks % this._framesPerSecond;
}
set framesPerSecond(framesPerSecond) {
this._framesPerSecond = framesPerSecond;
this._skipTicks = Math.floor(1000 / framesPerSecond);
}
get framesPerSecond() {
return this._framesPerSecond;
}
set maxFrameSkip(maxFrameSkip) {
this._maxFrameSkip = maxFrameSkip;
}
get maxFrameSkip() {
return this._maxFrameSkip;
}
set waitTime(waitTime) {
this._waitTime = waitTime;
}
get waitTime() {
return this._waitTime;
}
start() {
this.emit('start');
this._running = true;
this._lastTick = this._nextTick;
this._run();
}
stop() {
this._running = false;
this.emit('stop');
}
_run() {
const next = this._nextTick = Date.now();
const max = this._updateTicks + this._maxFrameSkip;
const skip = this._skipTicks;
while (this._lastTick <= next && this._updateTicks < max) {
this.emit('update');
this._lastTick += skip;
this._updateTicks++;
}
this._running && setTimeout(() => this._run(), this._waitTime);
}
};