2020-02-29 13:45:56 +01:00
|
|
|
var Container = require("./Container"),
|
|
|
|
CanvasRenderer = require('./renderer/CanvasRenderer')
|
|
|
|
;
|
2019-08-20 17:59:26 +02:00
|
|
|
|
|
|
|
const STEP = 1 / 60;
|
|
|
|
const FRAME_MAX = 5 * STEP;
|
|
|
|
|
|
|
|
class Game {
|
2020-01-17 21:11:15 +01:00
|
|
|
constructor(w, h, pixelated, parent = "#board") {
|
2019-08-20 17:59:26 +02:00
|
|
|
this.w = w;
|
|
|
|
this.h = h;
|
|
|
|
this.renderer = new CanvasRenderer(w, h);
|
|
|
|
document.querySelector(parent).appendChild(this.renderer.view);
|
|
|
|
|
|
|
|
if (pixelated) {
|
|
|
|
this.renderer.setPixelated();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.scene = new Container();
|
|
|
|
}
|
|
|
|
|
2020-01-17 21:11:15 +01:00
|
|
|
run(gameUpdate = () => { }) {
|
2019-08-20 17:59:26 +02:00
|
|
|
let dt = 0;
|
|
|
|
let last = 0;
|
|
|
|
const loop = ms => {
|
|
|
|
requestAnimationFrame(loop);
|
|
|
|
|
|
|
|
const t = ms / 1000;
|
|
|
|
dt = Math.min(t - last, FRAME_MAX);
|
|
|
|
last = t;
|
|
|
|
|
|
|
|
this.scene.update(dt, t);
|
|
|
|
gameUpdate(dt, t);
|
|
|
|
this.renderer.render(this.scene);
|
|
|
|
};
|
|
|
|
requestAnimationFrame(loop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-29 13:45:56 +01:00
|
|
|
module.exports = Game;
|