From 0a24e427bd19953d1f1675ff22ae80bec58f3ac0 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Fri, 29 Dec 2023 15:44:42 +0000 Subject: Move utility consts to the base class --- js/game.js | 18 +++++++++++------- js/mygame.js | 4 ---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/js/game.js b/js/game.js index b45e905..069618c 100644 --- a/js/game.js +++ b/js/game.js @@ -1,3 +1,7 @@ +const floor = Math.floor; +const max = Math.max; +const min = Math.min; + class Game { constructor(canvas, width, height) { this.canvas = canvas; @@ -79,14 +83,14 @@ class Game { _draw() {} resize() { - const scale = Math.floor(window.innerHeight / this.canvas.height); + const scale = floor(window.innerHeight / this.canvas.height); this.canvas.style["imageRendering"] = "pixelated"; this.canvas.style["transformOrigin"] = "top"; this.canvas.style["transform"] = `scale(${scale})`; } loop(now) { - let dt = Math.min(1 / this.minFps, now - this.then); + let dt = min(1 / this.minFps, now - this.then); this._update(dt); this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height); @@ -165,16 +169,16 @@ class Game { this.ctx.font = "8px monospace"; this.ctx.fillText( "ERROR Loading Resources", - Math.floor(this.ctx.canvas.width * 0.1), - Math.floor(this.ctx.canvas.height / 2 - h - h / 2) + floor(this.ctx.canvas.width * 0.1), + floor(this.ctx.canvas.height / 2 - h - h / 2) ); } this.ctx.fillStyle = "rgb(128, 128, 128)"; - this.ctx.fillRect(Math.floor(this.ctx.canvas.width * 0.1), Math.floor(this.ctx.canvas.height / 2) - h, this.ctx.canvas.width - Math.floor(this.ctx.canvas.width * 0.2), h); + this.ctx.fillRect(floor(this.ctx.canvas.width * 0.1), floor(this.ctx.canvas.height / 2) - h, this.ctx.canvas.width - floor(this.ctx.canvas.width * 0.2), h); this.ctx.fillStyle = "rgb(255, 255, 255)"; - this.ctx.fillRect(Math.floor(this.ctx.canvas.width * 0.1), Math.floor(this.ctx.canvas.height / 2) - h, ( - Math.floor(this.ctx.canvas.resSize * (this.ctx.canvas.width - Math.floor(this.ctx.canvas.width * 0.2)) / this.ctx.canvas.dataSize) + this.ctx.fillRect(floor(this.ctx.canvas.width * 0.1), floor(this.ctx.canvas.height / 2) - h, ( + floor(this.ctx.canvas.resSize * (this.ctx.canvas.width - floor(this.ctx.canvas.width * 0.2)) / this.ctx.canvas.dataSize) ), h); // we don't do this on update because we want diff --git a/js/mygame.js b/js/mygame.js index bd5b498..5b4a2fa 100644 --- a/js/mygame.js +++ b/js/mygame.js @@ -1,7 +1,3 @@ -const floor = Math.floor; -const max = Math.max; -const min = Math.min; - class MyGame extends Game { constructor(canvas) { super(canvas, 320, 240); -- cgit v1.2.3