-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.js
44 lines (44 loc) · 1.38 KB
/
clock.js
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
import { DebugView } from "./debug_view.js";
export class Timestamp {
constructor(milliseconds) {
this.milliseconds = milliseconds;
}
get seconds() {
return this.milliseconds / 1000.0;
}
}
export class Clock {
constructor() {
this._timeScale = 1.0;
this._timestamp = new Timestamp(performance.now());
this._wallTime = new Timestamp(performance.now());
}
get() {
return this._timestamp;
}
pause() {
this._timeScale = 0.0;
}
unpause() {
this._timeScale = 1.0;
}
isPaused() {
return this._timeScale === 0.0;
}
update() {
// TODO: this won't work for anything but paused and unpaused.
// if (this._timeScale !== 0.0) {
// const now = performance.now();
// this._timestamp = new Timestamp(now);
// }
const elapsedWallTime = performance.now() - this._wallTime.milliseconds;
const elapsedGameTime = elapsedWallTime * this._timeScale;
this._wallTime = new Timestamp(performance.now());
this._timestamp = new Timestamp(this._timestamp.milliseconds + elapsedGameTime);
DebugView.getView('clock').textContent = `${elapsedWallTime} / ${elapsedGameTime}`;
}
secondsAgo(seconds) {
return new Timestamp(this._timestamp.milliseconds - (seconds * 1000.0));
}
}
//# sourceMappingURL=clock.js.map