-
Notifications
You must be signed in to change notification settings - Fork 0
/
controls.js
127 lines (94 loc) · 2.74 KB
/
controls.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import * as THREE from './libs/three.module.js';
export class ControlManager{
constructor(params){
this.animationManager=params.animationManager;
this.player=params.player;
this.playerPos=this.player.getCharacterPosition();
this.InitInput();
}
InitInput() {
this.keys_ = {
spacebar: false,
arrowR_d: false,
arrowL_a: false,
};
this.oldKeys = {
...this.keys_
};
document.addEventListener('keydown', (e) => this.OnKeyDown_(e), false);
document.addEventListener('keyup', (e) => this.OnKeyUp_(e), false);
}
OnKeyDown_(event) {
switch (event.keyCode) {
//spacebar
case 32:
this.keys_.spacebar = true;
break;
//right arrow or d
case 39:
case 68:
this.keys_.arrowR_d = true;
break;
//left arrow or a
case 37:
case 65:
this.keys_.arrowL_a = true;
break;
default:
break;
}
}
OnKeyUp_(event) {
switch (event.keyCode) {
//spacebar
case 32:
this.keys_.spacebar = false;
break;
//right arrow or d
case 39:
case 68:
this.keys_.arrowR_d = false;
break;
//left arrow or a
case 37:
case 65:
this.keys_.arrowL_a = false;
break;
}
}
Update(timeElapsed) {
if (!this.keys_.spacebar && !this.animationManager.getJumpingFlag()){
if(!this.animationManager.getRunnintFlag()){
this.animationManager.startRunning();
}
if (this.keys_.arrowR_d) {
switch (this.playerPos.x) {
case 0:
this.animationManager.rightDashTo2();
break;
case -2:
this.animationManager.rightDashTo0();
break;
default:
break;
}
}
else if (this.keys_.arrowL_a) {
switch (this.playerPos.x) {
case 0:
this.animationManager.leftDashToMinus2();
break;
case 2:
this.animationManager.leftDashTo0();
break;
default:
break;
}
}
}
else if (this.keys_.spacebar && !this.animationManager.getJumpingFlag()) {
this.animationManager.stopRunning();
this.animationManager.jumpAnimation();
}
}
}