diff --git a/src/classes/game.js b/src/classes/game.js index 4d85701..7875f34 100644 --- a/src/classes/game.js +++ b/src/classes/game.js @@ -2,7 +2,9 @@ import Entity from "./entity.js"; import Vector2D from "./vector2d.js"; + import KeyboardInput from "./keyboardinput.js"; +import GamepadInput from "./gamepadinput.js"; class Game extends Entity { @@ -62,8 +64,8 @@ class Game extends Entity { this.canvas.width = this.width * this.scale; this.canvas.height = this.height * this.scale; - this.canvas.style.width = this.width + "px"; - this.canvas.style.height = this.height + "px"; + this.canvas.style.width = `${this.width}px`; + this.canvas.style.height = `${this.height}px`; // Call getContext last for Ejecta only. if (typeof ejecta !== "undefined") { @@ -89,11 +91,18 @@ class Game extends Entity { // Initialize defaults this.frameCounter = 0; + + // Initialize input methods this.inputs = {}; + if (config.inputs.keyboard) { this.inputs.keyboard = new KeyboardInput(this); } + if (config.inputs.gamepad) { + this.inputs.gamepad = new GamepadInput(this); + } + this._game = this; this._lastFrameTimestamp = 0; this._lastFrameTotalRenders = 0; diff --git a/src/classes/gamepadinput.js b/src/classes/gamepadinput.js new file mode 100644 index 0000000..73c7dc6 --- /dev/null +++ b/src/classes/gamepadinput.js @@ -0,0 +1,30 @@ +"use strict"; + + +class GamepadInput { + + + constructor () { + + var self = this; + + self._gamepadState = {}; + self._gamepadList = []; + + window.addEventListener("gamepadconnected", (event) => { + self._gamepadState[event.gamepad.index] = event.gamepad; + self._gamepadList.push(event.gamepad.index); + }); + + window.addEventListener("gamepaddisconnected", (event) => { + delete self._gamepadState[event.gamepad.index]; + self._gamepadList.splice(self._gamepadList.indexOf(event.gamepad.index)); + }); + + } + + +} + + +export default GamepadInput; \ No newline at end of file