-
Notifications
You must be signed in to change notification settings - Fork 11
/
Nearest_Player.js
27 lines (24 loc) · 1.18 KB
/
Nearest_Player.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
function pointDistance(p1, p2) {
return Math.hypot(p1.x - p2.x, p1.y - p2.y);
}
function findNearestPlayerToBall() {
var data = JSON.parse("[" + room.getPlayerList().filter(p => room.getPlayerDiscProperties(p.id) != null)
.map(p => `{\"name\": \"${p.name}\", \"id\": \"${p.id}\", \"distance\": \"${pointDistance(p.position, room.getBallPosition())}\"}`)
.join(",") + "]")
.sort((a, b) => {
if (Number(a.distance) > Number(b.distance)) return 1;
else if (Number(a.distance) < Number(b.distance)) return -1;
else return 0;
});
room.setPlayerAvatar(data[0].id,"✅");
room.getPlayerList().filter(p => room.getPlayerDiscProperties(p.id) != null && p.id != data[0].id)
.forEach(p => {
if(p) room.setPlayerAvatar(p.id,null);
});
}
var room = HBInit({ roomName: "Find the Nearest Player to Ball", noPlayer: true, public: true, maxPlayers: 12 });
room.onGameTick = function () {
if (room.getScores() != null && room.getPlayerList().filter(p => room.getPlayerDiscProperties(p.id) != null).length > 0) {
findNearestPlayerToBall();
}
}