move xxArrowPressed globals into the Input class
This commit is contained in:
parent
d06c5a49c4
commit
6d5e92faec
39
main.js
39
main.js
@ -8,12 +8,6 @@ const Orientation = {
|
||||
RIGHT: 'right'
|
||||
}
|
||||
|
||||
// TODO: make these not global.
|
||||
let upArrowPressed = false;
|
||||
let downArrowPressed = false;
|
||||
let leftArrowPressed = false;
|
||||
let rightArrowPressed = false;
|
||||
|
||||
class Input {
|
||||
constructor() {
|
||||
this.up = false;
|
||||
@ -29,39 +23,44 @@ class Input {
|
||||
this.select = false;
|
||||
this.start = false;
|
||||
|
||||
this.upArrowPressed = false;
|
||||
this.downArrowPressed = false;
|
||||
this.leftArrowPressed = false;
|
||||
this.rightArrowPressed = false;
|
||||
|
||||
window.addEventListener('gamepadconnected', this.gamepadConnected);
|
||||
window.addEventListener('gamepaddisconnected', this.gamepadDisconnected);
|
||||
document.addEventListener('keydown', this.keyDown);
|
||||
document.addEventListener('keyup', this.keyUp);
|
||||
document.addEventListener('keydown', (e) => this.keyDown(e));
|
||||
document.addEventListener('keyup', (e) => this.keyUp(e));
|
||||
}
|
||||
|
||||
keyDown(e) {
|
||||
if (e.key == 'ArrowUp' || e.key == 'w') {
|
||||
upArrowPressed = true;
|
||||
this.upArrowPressed = true;
|
||||
}
|
||||
if (e.key == 'ArrowDown' || e.key == 's') {
|
||||
downArrowPressed = true;
|
||||
this.downArrowPressed = true;
|
||||
}
|
||||
if (e.key == 'ArrowLeft' || e.key == 'a') {
|
||||
leftArrowPressed = true;
|
||||
this.leftArrowPressed = true;
|
||||
}
|
||||
if (e.key == 'ArrowRight' || e.key == 'd') {
|
||||
rightArrowPressed = true;
|
||||
this.rightArrowPressed = true;
|
||||
}
|
||||
}
|
||||
|
||||
keyUp(e) {
|
||||
if (e.key == 'ArrowUp' || e.key == 'w') {
|
||||
upArrowPressed = false;
|
||||
this.upArrowPressed = false;
|
||||
}
|
||||
if (e.key == 'ArrowDown' || e.key == 's') {
|
||||
downArrowPressed = false;
|
||||
this.downArrowPressed = false;
|
||||
}
|
||||
if (e.key == 'ArrowLeft' || e.key == 'a') {
|
||||
leftArrowPressed = false;
|
||||
this.leftArrowPressed = false;
|
||||
}
|
||||
if (e.key == 'ArrowRight' || e.key == 'd') {
|
||||
rightArrowPressed = false;
|
||||
this.rightArrowPressed = false;
|
||||
}
|
||||
}
|
||||
|
||||
@ -69,10 +68,10 @@ class Input {
|
||||
// TODO: have a config screen instead of hard-coding the 8Bitdo SNES30 pad.
|
||||
// TODO: handle connects / disconnects more correctly.
|
||||
|
||||
this.up = upArrowPressed;
|
||||
this.down = downArrowPressed;
|
||||
this.left = leftArrowPressed;
|
||||
this.right = rightArrowPressed;
|
||||
this.up = this.upArrowPressed;
|
||||
this.down = this.downArrowPressed;
|
||||
this.left = this.leftArrowPressed;
|
||||
this.right = this.rightArrowPressed;
|
||||
|
||||
const gamepad = navigator.getGamepads()[0];
|
||||
if (gamepad == null || !gamepad.connected || gamepad.axes.length < 2 ||
|
||||
|
Loading…
Reference in New Issue
Block a user