-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectionInput.js
36 lines (33 loc) · 1.2 KB
/
DirectionInput.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
class DirectionInput {
constructor() {
this.heldDirection = []; // Which direction movements were held in before
this.map = {
"ArrowUp": "up",
"KeyW": "up",
"ArrowDown": "down",
"KeyS": "down",
"ArrowLeft": "left",
"KeyA": "left",
"ArrowRight": "right",
"KeyD": "right",
}
}
get direction() {
return this.heldDirection[0]; // Letting other objects asks for which direction we are moving in
}
init() {
document.addEventListener("keydown", e => {
const dir = this.map[e.code];
if(dir && this.heldDirection.indexOf(dir) === -1) { // dir is found in map (valid input), and its not already held (not in heldDirection)
this.heldDirection.unshift(dir); // Put in the BEGINNING of the array
}
});
document.addEventListener("keyup", e => {
const dir = this.map[e.code];
const index = this.heldDirection.indexOf(dir);
if (index > -1) { // If key was already being held
this.heldDirection.splice(index, 1); // Remove it from list
}
})
}
}