-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventListeners.js
57 lines (49 loc) · 1.22 KB
/
eventListeners.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
window.addEventListener("keydown", (e) => {
if (player.preventInput) return;
switch (e.key) {
case "w":
for (let i = 0; i < doors.length; i++) {
const door = doors[1];
if (
player.hitbox.position.x + player.hitbox.width - 10 <=
door.position.x + door.width &&
player.hitbox.position.x >= door.position.x &&
player.hitbox.position.y + player.hitbox.height >= door.position.y &&
player.hitbox.position.y <= door.position.y + door.height
) {
player.velocity.x = 0;
player.velocity.y = 0;
player.preventInput = true;
player.switchSprite("enterDoor");
door.play();
return;
}
}
if (player.velocity.y === 0) {
player.velocity.y = -15;
}
break;
case "a":
keys.a.pressed = true;
break;
case "d":
keys.d.pressed = true;
break;
case "s":
keys.s.pressed = true;
break;
}
});
window.addEventListener("keyup", (e) => {
switch (e.key) {
case "a":
keys.a.pressed = false;
break;
case "d":
keys.d.pressed = false;
break;
case "s":
keys.s.pressed = false;
break;
}
});