-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfettiHitDetection.js
66 lines (59 loc) · 1.79 KB
/
confettiHitDetection.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
const COLLIDERS = {
left: {
topLeft: [-2, -1],
bottomRight: [-1, 1],
},
right: {
topLeft: [1, -1],
bottomRight: [2, 1],
},
up: {
topLeft: [-1, -2],
bottomRight: [1, -1],
},
down: {
topLeft: [-1, 1],
bottomRight: [1, 2],
},
}
const DIRECTIONS = {
5: 'left',
6: 'left',
7: 'right',
8: 'right',
4: 'up',
3: 'up',
2: 'down',
1: 'down',
}
checkCol = (player) => {
const area = COLLIDERS[DIRECTIONS[player.direction]]
const topLeft = [player.x + area.topLeft[0], player.y + area.topLeft[1]]
const bottomRight = [
player.x + area.bottomRight[0],
player.y + area.bottomRight[1],
]
console.log(area, topLeft, bottomRight)
const isInsideArea = (somePlayer) =>
somePlayer.x >= topLeft[0] &&
somePlayer.y >= topLeft[1] &&
somePlayer.x <= bottomRight[0] &&
somePlayer.y <= bottomRight[1]
const hits = Object.values(game.players).filter(isInsideArea)
return hits.length ? hits : undefined
}
game.subscribeToEvent('playerShootsConfetti', (data, ctx) => {
//todo maybe add audio feedback when a player gets hit hehehe(cheering sounds?)
//todo maybe turn the player hit into a ghost or a zombie for a while
//idea maybe have some kind of scoring of points
//idea maybe add an HP system
//idea maybe turn this into a battle minigame which players can agree to start
const hits = checkCol(ctx.player)
console.log(hits)
if (!hits) return
game.notify(
`🎉${hits.length > 1 ? 'Players' : 'Player'} ${hits
.map((x) => x.name)
.join(', ')} got HIT with Confetti by player ${ctx.player.name}`
)
})