-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameLoop.ts
89 lines (73 loc) · 1.97 KB
/
GameLoop.ts
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
import { _window } from "./globals";
import { useEffect, useRef } from "react";
interface IExecuteFn {
(deltaMs: number, timeMs: number): void;
}
interface IOptions {
pauseOnWindowBlur?: boolean;
}
export class GameLoop {
_started = false;
_executeFn: IExecuteFn;
_frequencyHz: number;
_options: IOptions;
_intervalId: any;
_tickCallbacks: IExecuteFn[] = [];
_timeMs = 0;
constructor(
executeFn: IExecuteFn,
frequencyHz: number,
options: IOptions = {}
) {
this._executeFn = executeFn;
this._frequencyHz = frequencyHz;
this._options = options;
}
start() {
this._justStart();
if (this._options.pauseOnWindowBlur) {
_window.addEventListener("blur", this.pause.bind(this));
_window.addEventListener("focus", this._justStart.bind(this));
}
}
_justStart() {
const intervalMs = 1000 / this._frequencyHz;
this._intervalId = setInterval(() => {
// TODO naively incrementing time by interval duration
// get more accuracy with performance.now()?
this._timeMs += intervalMs;
this._executeFn(intervalMs, this._timeMs);
this._tickCallbacks.forEach((cb) => {
cb(intervalMs, this._timeMs);
})
}, intervalMs);
}
pause() {
clearInterval(this._intervalId);
}
stop() {
this._timeMs = 0;
clearInterval(this._intervalId);
}
useTick(callback: IExecuteFn) {
const savedCallback = useRef<IExecuteFn>();
useEffect(() => {
if (savedCallback.current && savedCallback.current != callback) {
removeEventListener(this._tickCallbacks, savedCallback.current)
}
savedCallback.current = callback;
this._tickCallbacks.push(callback);
return () => {
removeEventListener(this._tickCallbacks, callback)
}
}, [callback]);
}
}
function removeEventListener(stack: any[], callback: any) {
for (var i = 0, l = stack.length; i < l; i++) {
if (stack[i] === callback){
stack.splice(i, 1);
return;
}
}
};