clean client code
This commit is contained in:
parent
f3619ba891
commit
21a30ad212
18 changed files with 294 additions and 268 deletions
|
@ -1,61 +1,55 @@
|
|||
function Input() {
|
||||
this.mouse = {
|
||||
x: 0,
|
||||
y: 0,
|
||||
held: {},
|
||||
pressed: {}
|
||||
};
|
||||
this.keys = {
|
||||
held: {},
|
||||
pressed: {}
|
||||
};
|
||||
class Input {
|
||||
constructor() {
|
||||
this.mouse = {
|
||||
x: 0,
|
||||
y: 0,
|
||||
held: {},
|
||||
pressed: {}
|
||||
};
|
||||
|
||||
this.mouseMove = function (el) {
|
||||
return function (event) {
|
||||
var rect = game.renderer.canvas.getBoundingClientRect();
|
||||
el.mouse.x = event.clientX - rect.left;
|
||||
el.mouse.y = event.clientY - rect.top;
|
||||
}
|
||||
};
|
||||
this.keys = {
|
||||
held: {},
|
||||
pressed: {}
|
||||
};
|
||||
|
||||
this.mouseDown = function (el) {
|
||||
return function (event) {
|
||||
el.mouse.pressed[event.which] = true;
|
||||
el.mouse.held[event.which] = true;
|
||||
}
|
||||
};
|
||||
|
||||
this.mouseUp = function (el) {
|
||||
return function (event) {
|
||||
el.mouse.held[event.which] = false;
|
||||
}
|
||||
document.addEventListener('mousemove', this.mouseMove.bind(this));
|
||||
document.addEventListener('mousedown', this.mouseDown.bind(this));
|
||||
document.addEventListener('mouseup', this.mouseUp.bind(this));
|
||||
document.addEventListener('keydown', this.keyDown.bind(this));
|
||||
document.addEventListener('keyup', this.keyUp.bind(this));
|
||||
}
|
||||
|
||||
this.keyDown = function (el) {
|
||||
return function (event) {
|
||||
if (!el.keys.held[event.which]) el.keys.pressed[event.which] = true;
|
||||
el.keys.held[event.which] = true;
|
||||
}
|
||||
mouseMove() {
|
||||
var rect = game.renderer.canvas.getBoundingClientRect();
|
||||
this.mouse.x = event.clientX - rect.left;
|
||||
this.mouse.y = event.clientY - rect.top;
|
||||
};
|
||||
|
||||
mouseDown() {
|
||||
this.mouse.pressed[event.which] = true;
|
||||
this.mouse.held[event.which] = true;
|
||||
};
|
||||
|
||||
mouseUp() {
|
||||
this.mouse.held[event.which] = false;
|
||||
}
|
||||
|
||||
this.keyUp = function (el) {
|
||||
return function (event) {
|
||||
el.keys.held[event.which] = false;
|
||||
}
|
||||
keyDown() {
|
||||
if (!this.keys.held[event.which])
|
||||
this.keys.pressed[event.which] = true;
|
||||
this.keys.held[event.which] = true;
|
||||
}
|
||||
|
||||
document.addEventListener('mousemove', this.mouseMove(this));
|
||||
document.addEventListener('mousedown', this.mouseDown(this));
|
||||
document.addEventListener('mouseup', this.mouseUp(this));
|
||||
document.addEventListener('keydown', this.keyDown(this));
|
||||
document.addEventListener('keyup', this.keyUp(this));
|
||||
keyUp() {
|
||||
this.keys.held[event.which] = false;
|
||||
}
|
||||
|
||||
this.mouseAnyPressed = function () {
|
||||
mouseAnyPressed() {
|
||||
var p = this.mouse.pressed;
|
||||
return p[1] || p[2] || p[3];
|
||||
}
|
||||
|
||||
this.clear = function () {
|
||||
clear() {
|
||||
for (var i in this.keys.pressed) this.keys.pressed[i] = false;
|
||||
for (var i in this.mouse.pressed) this.mouse.pressed[i] = false;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue