-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.js
33 lines (30 loc) · 928 Bytes
/
controller.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
// Class to keep track of which keys are pressed down
class Controller {
static w = false;
static a = false;
static s = false;
static d = false;
// on instantiation, creates events for pressing down and releasing A, W, S, and D keys on the keyboard
constructor() {
document.addEventListener("keydown", (e) => {
this.keyDownHandler(e);
})
document.addEventListener("keyup", (e) => {
this.keyUpHandler(e);
})
}
// sets the property to true when the key is pressed down
// @param {Object} e - event object
keyDownHandler(e) {
if (e.key === "w" || e.key === "a" || e.key === "s" || e.key === "d") {
Controller[e.key] = true;
}
}
// sets the property to false when the key is released
// @param {Object} e - event object
keyUpHandler(e) {
if (e.key === "w" || e.key === "a" || e.key === "s" || e.key === "d") {
Controller[e.key] = false;
}
}
}