2020-02-29 13:45:56 +01:00
|
|
|
var Container = require("./Container"),
|
2020-03-21 17:22:28 +01:00
|
|
|
CanvasRenderer = require("./renderer/CanvasRenderer")
|
2020-02-29 13:45:56 +01:00
|
|
|
;
|
2019-08-20 17:59:26 +02:00
|
|
|
|
|
|
|
const STEP = 1 / 60;
|
|
|
|
const FRAME_MAX = 5 * STEP;
|
|
|
|
|
|
|
|
class Game {
|
2020-03-21 17:22:28 +01:00
|
|
|
constructor(w, h, pixelated, parent = "#board") {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
run(gameUpdate = () => { }) {
|
|
|
|
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);
|
|
|
|
}
|
2019-08-20 17:59:26 +02:00
|
|
|
}
|
|
|
|
|
2020-02-29 13:45:56 +01:00
|
|
|
module.exports = Game;
|