add new players finding functions

This commit is contained in:
MihailRis
2025-10-16 22:00:00 +03:00
parent 2210ba8218
commit 9cb9b095e8
3 changed files with 86 additions and 0 deletions
+45
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) {
+6
View File
@@ -1,7 +1,9 @@
#pragma once
#include <memory>
#include <vector>
#include <unordered_map>
#include <glm/vec3.hpp>
#include "typedefs.hpp"
#include "interfaces/Serializable.hpp"
@@ -34,6 +36,10 @@ public:
void remove(int64_t id);
std::vector<Player*> getAllInRadius(const glm::vec3& center, float radius) const;
std::vector<Player*> getAll() const;
Player* getNearest(const glm::vec3& position) const;
dv::value serialize() const override;
void deserialize(const dv::value& src) override;