-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.js
63 lines (51 loc) · 1.44 KB
/
input.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// https://gablaxian.com/articles/creating-a-game-with-javascript/handling-user-input
class Input {
constructor() {
this.UP_ARROW = 38;
this.RT_ARROW = 39;
this.LT_ARROW = 37;
this.DN_ARROW = 40;
this.SPACE = 32;
this.ESC = 27;
this.keys = [];
window.addEventListener('keydown', (e) => {
this.keys[e.keyCode] = true;
});
window.addEventListener('keyup', (e) => {
this.keys[e.keyCode] = false;
});
}
check(key) {
return this.keys[key];
}
getHorizontal() {
let r = this.check(this.RT_ARROW);
let l = this.check(this.LT_ARROW);
if(r && l) {
return 0;
} else if(r) {
return 1;
} else if(l) {
return -1;
} else {
return 0;
}
//return r && l ? 0 : r ? 1 : l ? -1 : 0;
}
getVertical() {
let u = this.check(this.UP_ARROW);
let d = this.check(this.DN_ARROW);
if(u && d) {
return 0;
} else if(u) {
return -1;
} else if(d) {
return 1;
} else {
return 0;
}
//return u && d ? 0 : u ? 1 : d ? -1 : 0;
//return this.check(this.UP_ARROW) || this.check(this.SPACE);
}
}
export default Input;