-
Notifications
You must be signed in to change notification settings - Fork 0
/
program.js
66 lines (50 loc) · 1.37 KB
/
program.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const { Player, Game } = require("./lib/game");
const StdInput = require('./lib/std-input')
const CliRenderer = require("./lib/cli-renderer");
const time = require("./lib/time");
function eventLoopQueue() {
return new Promise((resolve) => {
setImmediate(() => {
resolve();
});
});
}
(async function () {
const exitCalls = 0;
const game = new Game([
new Player('x'),
new Player('y')
]);
const input = new StdInput(game);
input.listen();
const renderer = new CliRenderer(game);
renderer.clearConsole();
process.on('SIGINT', () => {
if (exitCalls < 1) {
this.game.quit = true;
setTimeout(() => process.exit(0), 1000);
}
exitCalls++;
});
// https://gafferongames.com/post/fix_your_timestep/
await renderLoop(game, renderer);
process.exit(0);
})();
async function renderLoop(game, renderer) {
let t = 0.0;
let currentTime = time.seconds();
let accumulator = 0;
while (game.quit == false) {
let newTime = time.seconds();
let deltaT = newTime - currentTime;
currentTime = newTime;
t += deltaT;
accumulator += deltaT;
await eventLoopQueue();
if (accumulator >= 1 / 30.0) {
accumulator = 0;
renderer.draw();
}
}
renderer.draw();
}