RustX commited on
Commit
a4cd16a
1 Parent(s): 3b5f7bc

Create controls.js

Browse files
Files changed (1) hide show
  1. controls.js +54 -0
controls.js ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class Controls{
2
+ constructor(type){
3
+ this.forward=false;
4
+ this.left=false;
5
+ this.right=false;
6
+ this.reverse=false;
7
+
8
+ switch(type){
9
+ case "KEYS":
10
+ this.#addKeyboardListeners();
11
+ break;
12
+ case "DUMMY":
13
+ this.forward=true;
14
+ break;
15
+ }
16
+
17
+
18
+ }
19
+
20
+ #addKeyboardListeners(){
21
+ document.onkeydown=(event)=>{
22
+ switch(event.key){
23
+ case "ArrowLeft":
24
+ this.left=true;
25
+ break;
26
+ case "ArrowRight":
27
+ this.right=true;
28
+ break;
29
+ case "ArrowUp":
30
+ this.forward=true;
31
+ break;
32
+ case "ArrowDown":
33
+ this.reverse=true;
34
+ break;
35
+ }
36
+ }
37
+ document.onkeyup=(event)=>{
38
+ switch(event.key){
39
+ case "ArrowLeft":
40
+ this.left=false;
41
+ break;
42
+ case "ArrowRight":
43
+ this.right=false;
44
+ break;
45
+ case "ArrowUp":
46
+ this.forward=false;
47
+ break;
48
+ case "ArrowDown":
49
+ this.reverse=false;
50
+ break;
51
+ }
52
+ }
53
+ }
54
+ }