forked from arne/asdf-games
Added listen variable to controls
This makes it possible to send controls over socket.io
This commit is contained in:
parent
e8cf095cec
commit
9da289ea62
@ -1,7 +1,8 @@
|
||||
class KeyControls {
|
||||
|
||||
constructor() {
|
||||
constructor(listen = true) {
|
||||
this.keys = {};
|
||||
if (listen) {
|
||||
// Bind event handlers
|
||||
document.addEventListener("keydown", e => {
|
||||
if ([37, 38, 39, 40].indexOf(e.which) >= 0) {
|
||||
@ -13,6 +14,7 @@ class KeyControls {
|
||||
this.keys[e.which] = false;
|
||||
}, false);
|
||||
}
|
||||
}
|
||||
|
||||
get action() {
|
||||
// Spacebar
|
||||
|
@ -1,17 +1,21 @@
|
||||
class MouseControls {
|
||||
constructor(container) {
|
||||
constructor(container, listen = true) {
|
||||
this.el = container || document.body;
|
||||
// State
|
||||
this.pos = { x: 0, y: 0 };
|
||||
this.isDown = false;
|
||||
this.pressed = false;
|
||||
this.released = false;
|
||||
|
||||
if (listen) {
|
||||
// Handlers
|
||||
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 }) {
|
||||
const { el, pos } = this;
|
||||
const rect = el.getBoundingClientRect();
|
||||
|
Loading…
Reference in New Issue
Block a user