-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.js
58 lines (54 loc) · 1.51 KB
/
input.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
// left,right space
let input = {
down: {},
pressed: {},
init() {
// event listener set
window.addEventListener("keydown", (e) => {
this.down[e.code] = true;
// console.log(e.code)
})
window.addEventListener("keyup", (e) => {
delete this.down[e.code];
delete this.pressed[e.code];
})
}
,
update(gameObj) {
let mario = gameObj.entities.mario
if (gameObj.userControl == true) {
//left
if (this.isDown("ArrowLeft")) {
// go left
mario.posX -= mario.velX;
mario.currentDirection = "left";
mario.currentState = mario.states.walkingAnim;
}
// right
if (this.isDown("ArrowRight")) {
// go right
mario.posX += mario.velX;
mario.currentDirection = "right";
mario.currentState = mario.states.walkingAnim;
}
// space
// console.log(mario.velY);
if (this.isPressed("Space")) {
if (mario.velY == 1.1) {
mario.velY -= 14;
mario.currentState = mario.states.jumpingAnim;
}
}
}
}
, isDown(key) {
return this.down[key];
},
isPressed(key) {
if (this.pressed[key]) {
return false;
} else if (this.down[key]) {
return true;
}
}
}