forked from Lukas-Bloom/N-igma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.js
99 lines (90 loc) · 2.4 KB
/
actions.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
import { LEVEL_LENGTH } from "./constants.js";
import { socket } from "./socket.js";
import {
handleMovement,
gameover,
pickUpKey,
doTeleSwap,
nextLevel,
handleAllMovingObjects,
} from "./collisionEvents/collisionEvents.js";
let isDead = 0;
let isPowerUp = 0;
let nextL = 0;
let levelLength = LEVEL_LENGTH[getData("lvlIndex")] - width() / 2;
export const handleActionEvents = (p, otherPlayer, levelIndex, level) => {
//reset jumps when landing
function checkIfGrounded() {
if (p.grounded()) {
p.jumps = p.jumpsAmount;
p.isJumping = false;
}
}
function destroyAllGhostBlocks() {
if (p.currentPowerUp === "ghost") return;
const ghostblks = get("ghostblock");
for (let i = 0; i < ghostblks.length; i++) {
setTimeout(function () {
if (!otherPlayer.isTouching(ghostblks[i])) {
destroy(ghostblks[i]);
}
}, 50);
}
}
return action(() => {
socket.emit("pos", p.pos.x, p.pos.y);
socket.on("moveOtherPlayer", (x, y) => otherPlayer.moveTo(x, y));
socket.on("gameOver", () => {
isDead++;
if (isDead === 1) {
gameover(p);
}
});
isDead = 0;
socket.on("powerUp", (powerUp, obj) => {
isPowerUp++;
if (isPowerUp === 1) {
if (!powerUp && !obj) return otherPlayer.clearPowerUps();
get("powerUp").forEach((o) => {
if (o._id === obj._id) otherPlayer.changePowerUp(powerUp, o);
});
}
});
isPowerUp = 0;
socket.on("key", (obj) => {
get("key").forEach((o) => {
if (o._id === obj._id) pickUpKey(o, levelIndex, level);
});
});
socket.on("teleSwap", (obj) => {
get("teleSwap").forEach((o) => {
if (o._id === obj._id) doTeleSwap(o, p, otherPlayer);
});
});
socket.on("nextLevel", (nextLvl) => {
nextL++;
if (nextL === 1) {
setData("lvlIndex", nextLvl);
p.nextLevel = true;
nextLevel(p, otherPlayer);
}
});
nextL = 0;
camPos(
p.pos.x > levelLength
? levelLength
: p.pos.x > width() / 2 + 16
? p.pos.x
: width() / 2 + 16,
height() / 2
);
handleMovement(
otherPlayer.curPlatform()?.is("btn") || p.curPlatform()?.is("btn")
);
checkIfGrounded();
destroyAllGhostBlocks();
handleAllMovingObjects(
otherPlayer.curPlatform()?.is("player") || p.curPlatform()?.is("player")
);
});
};