-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.js
86 lines (75 loc) · 2.63 KB
/
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
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
class Controller{
constructor(){
this.keyState = {
status:[false, false, false, false], // Down, Up, Left, Right. False = Up, True = Down
direction:[0,0], //Non-normalized movement vector based on keypresses
mousePos:[0,0], //Mouse location vector, bound between 0 and 1 (both axes, regardless of aspet ratio)
mouseButton:false //Whether the left mouse is being held or not
};
}
/*
left = 37
up = 38
right = 39
down = 40
*/
//Sets the movement vector based on WSAD keys currently held
keyStateSet(event) {
if(event.repeat) { return; }
if(event.key == 'ArrowDown' || event.key.toLowerCase() == 's') {
this.keyState.status[0] = true;
}
else if(event.key == 'ArrowUp' || event.key.toLowerCase() == 'w') {
this.keyState.status[1] = true;
}
else if(event.key == 'ArrowLeft' || event.key.toLowerCase() == 'a') {
this.keyState.status[2] = true;
}
else if(event.key == 'ArrowRight' || event.key.toLowerCase() == 'd') {
this.keyState.status[3] = true;
}
this.recalcDirection()
}
keyStateUnset(event) {
if(event.key == 'ArrowDown' || event.key.toLowerCase() == 's') {
this.keyState.status[0] = false;
}
else if(event.key == 'ArrowUp' || event.key.toLowerCase() == 'w') {
this.keyState.status[1] = false;
}
else if(event.key == 'ArrowLeft' || event.key.toLowerCase() == 'a') {
this.keyState.status[2] = false;
}
else if(event.key == 'ArrowRight' || event.key.toLowerCase() == 'd') {
this.keyState.status[3] = false;
}
this.recalcDirection();
}
recalcDirection(){
this.keyState.direction[0] = 0;
this.keyState.direction[1] = 0;
if(this.keyState.status[0] == true) {
this.keyState.direction[1] +=1;
}
if(this.keyState.status[1] == true) {
this.keyState.direction[1] -=1;
}
if(this.keyState.status[2] == true) {
this.keyState.direction[0] -=1;
}
if(this.keyState.status[3] == true) {
this.keyState.direction[0] +=1;
}
}
//Obtanins the canvas mouse coordinates, range normalized to [0..1]
mouseEvent(event, x_size, y_size) {
this.keyState.mousePos[0] = event.offsetX / x_size;
this.keyState.mousePos[1] = event.offsetY / y_size;
}
mouseDownEvent(event) {
this.keyState.mouseButton = true;
}
mouseUpEvent(event) {
this.keyState.mouseButton = false;
}
}