Added listen variable to controls

This makes it possible to send controls over socket.io
This commit is contained in:
Arne van Iterson 2020-04-23 22:30:06 +02:00
parent e8cf095cec
commit 9da289ea62
2 changed files with 22 additions and 16 deletions

View File

@ -1,17 +1,19 @@
class KeyControls { class KeyControls {
constructor() { constructor(listen = true) {
this.keys = {}; this.keys = {};
// Bind event handlers if (listen) {
document.addEventListener("keydown", e => { // Bind event handlers
if ([37, 38, 39, 40].indexOf(e.which) >= 0) { document.addEventListener("keydown", e => {
e.preventDefault(); if ([37, 38, 39, 40].indexOf(e.which) >= 0) {
} e.preventDefault();
this.keys[e.which] = true; }
}, false); this.keys[e.which] = true;
document.addEventListener("keyup", e => { }, false);
this.keys[e.which] = false; document.addEventListener("keyup", e => {
}, false); this.keys[e.which] = false;
}, false);
}
} }
get action() { get action() {

View File

@ -1,15 +1,19 @@
class MouseControls { class MouseControls {
constructor(container) { constructor(container, listen = true) {
this.el = container || document.body; this.el = container || document.body;
// State // State
this.pos = { x: 0, y: 0 }; this.pos = { x: 0, y: 0 };
this.isDown = false; this.isDown = false;
this.pressed = false; this.pressed = false;
this.released = false; this.released = false;
// Handlers
document.addEventListener("mousemove", this.move.bind(this), false); if (listen) {
document.addEventListener("mousedown", this.down.bind(this), false); // Handlers
document.addEventListener("mouseup", this.up.bind(this), false); document.addEventListener("mousemove", this.move.bind(this), false);
document.addEventListener("mousedown", this.down.bind(this), false);
document.addEventListener("mouseup", this.up.bind(this), false);
}
} }
mousePosFromEvent({ clientX, clientY }) { mousePosFromEvent({ clientX, clientY }) {