Hello,
If you have access to the current player, you can retrieve the map players and retrieve the nearby players.
import { RpgWorld, RpgMap } from '@rpgjs/server'
function near(player: RpgPlayer, distance: number = 5) {
const map = player.getCurrentMap() as RpgMap;
const players = RpgWorld.getPlayersOfMap(map.id);
const nearPlayers = players.filter(otherPlayer => {
// Don't include the current player in the list of nearby players
if (otherPlayer === player) return false;
const dx = otherPlayer.position.x - player.position.x;
const dy = otherPlayer.position.y - player.position.y;
// Use Euclidean distance to determine if other players are close
const distanceToPlayer = Math.sqrt(dx * dx + dy * dy);
return distanceToPlayer <= distance;
});
return nearPlayers;
}