add new players finding functions

This commit is contained in:
MihailRis 2025-10-16 20:43:03 +03:00
parent 2210ba8218
commit 9cb9b095e8
3 changed files with 86 additions and 0 deletions

View file

@ -1,5 +1,8 @@
#include "Players.hpp"
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/norm.hpp>
#include "Player.hpp"
#include "items/Inventories.hpp"
#include "world/Level.hpp"
@ -20,6 +23,48 @@ Player* Players::get(int64_t id) const {
return found->second.get();
}
std::vector<Player*> Players::getAllInRadius(
const glm::vec3& center, float radius
) const {
std::vector<Player*> foundPlayers;
for (const auto& pair : players) {
auto player = pair.second.get();
auto relativePos = player->getPosition() - center;
if (!player->isSuspended() && glm::length2(relativePos) <= radius) {
foundPlayers.emplace_back(player);
}
}
return foundPlayers;
}
std::vector<Player*> Players::getAll() const {
std::vector<Player*> allPlayers;
allPlayers.reserve(players.size());
for (const auto& pair : players) {
allPlayers.emplace_back(pair.second.get());
}
return allPlayers;
}
Player* Players::getNearest(const glm::vec3& position) const {
Player* nearest = nullptr;
float nearestDist2 = std::numeric_limits<float>::max();
for (const auto& pair : players) {
auto player = pair.second.get();
if (player->isSuspended()) {
continue;
}
auto relativePos = player->getPosition() - position;
auto dist2 = glm::length2(relativePos);
if (dist2 < nearestDist2) {
nearestDist2 = dist2;
nearest = player;
}
}
return nearest;
}
Player* Players::create(int64_t id) {
int64_t& nextPlayerID = level.getWorld()->getInfo().nextPlayerId;
if (id == NONE) {