diff --git a/examples/pong/index.html b/examples/pong/index.html index 11840ac..a086423 100644 --- a/examples/pong/index.html +++ b/examples/pong/index.html @@ -3,19 +3,6 @@ Pong - MomentumEngine - diff --git a/examples/pong/pong.js b/examples/pong/pong.js index 9ac6693..3f77659 100644 --- a/examples/pong/pong.js +++ b/examples/pong/pong.js @@ -167,7 +167,6 @@ class Pong extends MomentumEngine.Classes.Game { paddle.balls = this.balls; } - } diff --git a/src/classes/game.js b/src/classes/game.js index 62cd85b..116e5c7 100644 --- a/src/classes/game.js +++ b/src/classes/game.js @@ -39,6 +39,10 @@ class Game extends Entity { // Optional params this.desiredFps = config.desiredFps || 60; + this.rescaleOnFullScreen = !!config.rescaleOnFullScreen; + + this.scale = 1; + if (config.fixRatio) { let deviceRatio = window.devicePixelRatio, @@ -107,6 +111,7 @@ class Game extends Entity { this._lastFrameTimestamp = 0; this._lastFrameTotalRenders = 0; this._wantPause = true; + this._fullScreenLastFrame = false; } @@ -135,8 +140,60 @@ class Game extends Entity { this.frameCounter++; + this._frameMaintenance(); this._updateEntity(delta); this._renderEntity(); + this._updateInputs(); // NK: This happens at the end for reasons + + } + + + _updateInputs () { + + for (let input in this.inputs) { + if (this.inputs[input].update) { + this.inputs[input].update(); + } + } + + } + + + _frameMaintenance () { + + if (this.isFullScreen) { + + if (this._fullScreenLastFrame == false) { + + this.canvas.style.width = `${screen.width}px`; + this.canvas.style.height = `${screen.height}px`; + + if (this.rescaleOnFullScreen) { + this.canvas.width = screen.width * this.scale; + this.canvas.height = screen.height * this.scale; + } + + } + + this._fullScreenLastFrame = true; + + } else { + + if (this._fullScreenLastFrame == true) { + + this.canvas.style.width = `${this.width}px`; + this.canvas.style.height = `${this.height}px`; + + if (this.rescaleOnFullScreen) { + this.canvas.width = this.width * this.scale; + this.canvas.height = this.height * this.scale; + } + + } + + this._fullScreenLastFrame = false; + + } } @@ -229,7 +286,7 @@ class Game extends Entity { get isFullScreen () { - return (!document.mozFullScreen && !document.webkitFullScreen); + return document.fullScreen || document.mozFullScreen || document.webkitIsFullScreen; } diff --git a/src/classes/gamepadinput.js b/src/classes/gamepadinput.js index 11a16cc..59a8bd5 100644 --- a/src/classes/gamepadinput.js +++ b/src/classes/gamepadinput.js @@ -27,7 +27,7 @@ class Gamepad { getAxis (axisId) { - if (this._gamepadObj.axes[axisId]) { + if (typeof this._gamepadObj.axes[axisId] !== "undefined") { return this._gamepadObj.axes[axisId]; } else { throw new Error(`Axis ${axisId} not found on gamepad`); @@ -48,35 +48,7 @@ class GamepadInput { self._gamepadState = {}; self.gamepadIds = []; - if (!("ongamepadconnected" in window)) { - - let pollGamepads = function () { - - let gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads : []); - - for (var i = 0; i < gamepads.length; i++) { - - let gamepad = gamepads[i]; - - if (gamepad) { - - if (self.gamepadIds.indexOf(gamepad.index) < 0) { - self._gamepadState[gamepad.index] = new Gamepad(gamepad); - self.gamepadIds.push(gamepad.index); - - console.log(`Gamepad ${gamepad.index} connected`); - - } - - } - - } - - }; - - let interval = setInterval(pollGamepads, 5); - - } else { + if ('ongamepadconnected' in window) { window.addEventListener("gamepadconnected", (event) => { self._gamepadState[event.gamepad.index] = new Gamepad(event.gamepad); @@ -87,7 +59,7 @@ class GamepadInput { window.addEventListener("gamepaddisconnected", (event) => { delete self._gamepadState[event.gamepad.index]; self.gamepadIds.splice(self.gamepadIds.indexOf(event.gamepad.index)); - console.log(`Gamepad ${event.gamepad.index} connected`); + console.log(`Gamepad ${event.gamepad.index} disconnected`); }); } @@ -95,6 +67,72 @@ class GamepadInput { } + update () { + + if (!("ongamepadconnected" in window)) { + + let gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads : []); + + // If there are more gamepads registered than we know about, make ourselves aware of the new ones + if (gamepads.length != this.gamepadIds.length) { + + for (let i = 0; i < gamepads.length; i++) { + + let gamepad = gamepads[i]; + + if (gamepad) { + + if (this.gamepadIds.indexOf(gamepad.index) < 0) { + this._gamepadState[gamepad.index] = new Gamepad(gamepad); + this.gamepadIds.push(gamepad.index); + + console.log(`Gamepad ${gamepad.index} connected`); + + } + + } + + } + + // If there is still a mismatch, then we assume some gamepads have been disconnected, so we need to remove them + if (gamepads.length != this.gamepadIds.length) { + + for (let i = 0; i < this.gamepadIds.length; i++) { + + let found = false; + + for (let j = 0; j < gamepads.length; j++) { + + let gamepad = gamepads[i]; + + if (gamepad && gamepad.index == this.gamepadIds[i]) { + found = true; + } + + } + + if (!found) { + + console.log(`Gamepad ${this.gamepadIds[i]} disconnected`); + + delete this._gamepadState[this.gamepadIds[i]]; + this.gamepadIds.splice(this.gamepadIds.indexOf(this.gamepadIds[i])); + + i--; + + } + + } + + } + + } + + } + + } + + get numGamepads () { return this.gamepadIds.length; }