Start of gamepad support - connection and disconnection

This commit is contained in:
Nathan Kunicki 2016-03-07 09:11:02 -05:00
parent 3cd618e66c
commit 3406c28a80
2 changed files with 41 additions and 2 deletions

View File

@ -2,7 +2,9 @@
import Entity from "./entity.js"; import Entity from "./entity.js";
import Vector2D from "./vector2d.js"; import Vector2D from "./vector2d.js";
import KeyboardInput from "./keyboardinput.js"; import KeyboardInput from "./keyboardinput.js";
import GamepadInput from "./gamepadinput.js";
class Game extends Entity { class Game extends Entity {
@ -62,8 +64,8 @@ class Game extends Entity {
this.canvas.width = this.width * this.scale; this.canvas.width = this.width * this.scale;
this.canvas.height = this.height * this.scale; this.canvas.height = this.height * this.scale;
this.canvas.style.width = this.width + "px"; this.canvas.style.width = `${this.width}px`;
this.canvas.style.height = this.height + "px"; this.canvas.style.height = `${this.height}px`;
// Call getContext last for Ejecta only. // Call getContext last for Ejecta only.
if (typeof ejecta !== "undefined") { if (typeof ejecta !== "undefined") {
@ -89,11 +91,18 @@ class Game extends Entity {
// Initialize defaults // Initialize defaults
this.frameCounter = 0; this.frameCounter = 0;
// Initialize input methods
this.inputs = {}; this.inputs = {};
if (config.inputs.keyboard) { if (config.inputs.keyboard) {
this.inputs.keyboard = new KeyboardInput(this); this.inputs.keyboard = new KeyboardInput(this);
} }
if (config.inputs.gamepad) {
this.inputs.gamepad = new GamepadInput(this);
}
this._game = this; this._game = this;
this._lastFrameTimestamp = 0; this._lastFrameTimestamp = 0;
this._lastFrameTotalRenders = 0; this._lastFrameTotalRenders = 0;

View File

@ -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;