mirror of
https://github.com/BreizhHardware/bloubloulespoissons.git
synced 2026-01-18 16:47:31 +01:00
fix: corrige la visibilité des autres joueurs en multijoueur
- Ajout de la méthode `getPlayerId` const dans `player.cpp` et `player.h`. - Ajout de la fonction `receivePlayerListFromServer` et `addPlayerToGame` dans `networking_client.cpp` et `networking_client.h`. - Correction de la fonction `sendPlayerListToNewClient` et `getAllPlayers` dans `networking.cpp` et `networking.h`. - Ajout de l'affichage des joueurs proches en mode multijoueur dans `utility.cpp` et `utility.h`.
This commit is contained in:
@@ -167,7 +167,7 @@ void Player::drawEnergyBar(SDL_Renderer* renderer) {
|
||||
SDL_RenderFillRect(renderer, &energyBarForeground);
|
||||
}
|
||||
|
||||
int Player::getPlayerId() {
|
||||
int Player::getPlayerId() const {
|
||||
return playerId;
|
||||
}
|
||||
|
||||
|
||||
@@ -73,7 +73,7 @@ class Player {
|
||||
void handlePlayerMovement(int ENV_WIDTH, int ENV_HEIGHT, int windowWidth, int windowHeight);
|
||||
void drawEnergyBar(SDL_Renderer* renderer);
|
||||
bool checkCollision(SDL_Rect fishRect);
|
||||
int getPlayerId();
|
||||
int getPlayerId() const;
|
||||
void setPlayerPos(int x, int y);
|
||||
void handleClientMessages();
|
||||
void updatePosition(int x, int y);
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
//
|
||||
|
||||
#include "networking.h"
|
||||
#include "../Entities/player.h"
|
||||
|
||||
std::unordered_map<int, std::pair<int, int>> playerPositions;
|
||||
std::unordered_map<int, std::chrono::time_point<std::chrono::steady_clock>> lastKeepAlive;
|
||||
@@ -231,4 +232,26 @@ std::string processReceivedData(const std::string& data) {
|
||||
}
|
||||
}
|
||||
return filteredData;
|
||||
}
|
||||
|
||||
std::vector<PlayerInfo> getAllPlayers() {
|
||||
std::vector<PlayerInfo> playersInfo;
|
||||
for (const auto& player : players_server) {
|
||||
auto [x, y] = player.getPlayerPos();
|
||||
playersInfo.push_back({player.getPlayerId(), x, y});
|
||||
}
|
||||
return playersInfo;
|
||||
}
|
||||
|
||||
void sendPlayerListToNewClient(int clientSocket) {
|
||||
std::vector<PlayerInfo> players = getAllPlayers();
|
||||
|
||||
int playerCount = players.size();
|
||||
std::string message = "playerList;" + std::to_string(playerCount) + ";";
|
||||
sendMessage(server, message);
|
||||
|
||||
for (const auto& player : players) {
|
||||
std::string playerInfo = std::to_string(player.id) + "," + std::to_string(player.x) + "," + std::to_string(player.y) + ";";
|
||||
sendMessage(server, playerInfo);
|
||||
}
|
||||
}
|
||||
@@ -26,5 +26,12 @@ void closeServer();
|
||||
void handleServerMessages();
|
||||
void sendSharkPosition(TCPsocket socket, int sharkId, int x, int y);
|
||||
std::string processReceivedData(const std::string& data);
|
||||
struct PlayerInfo {
|
||||
int id;
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
std::vector<PlayerInfo> getAllPlayers();
|
||||
void sendPlayerListToNewClient(int clientSocket);
|
||||
|
||||
#endif //NETWORKING_H
|
||||
@@ -145,3 +145,54 @@ void startKeepAlive(TCPsocket serverSocket) {
|
||||
}).detach();
|
||||
}
|
||||
|
||||
void receivePlayerListFromServer(TCPsocket serverSocket) {
|
||||
// Exemple de structure de données pour un joueur
|
||||
struct PlayerInfo {
|
||||
int id;
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
// Recevoir le message contenant la liste des joueurs
|
||||
std::string message = receiveMessage(serverSocket);
|
||||
if (message.empty()) {
|
||||
std::cerr << "Failed to receive player list" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
std::istringstream iss(message);
|
||||
std::string token;
|
||||
std::getline(iss, token, ';'); // Lire "playerList"
|
||||
if (token != "playerList") {
|
||||
std::cerr << "Invalid message format" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// Lire le nombre de joueurs
|
||||
std::getline(iss, token, ';');
|
||||
int playerCount = std::stoi(token);
|
||||
|
||||
// Lire les informations de chaque joueur
|
||||
for (int i = 0; i < playerCount; ++i) {
|
||||
std::getline(iss, token, ';');
|
||||
int playerId, x, y;
|
||||
sscanf(token.c_str(), "%d,%d,%d", &playerId, &x, &y);
|
||||
// Mettre à jour l'état du client avec les informations du joueur
|
||||
addPlayerToGame(playerId, x, y);
|
||||
}
|
||||
}
|
||||
|
||||
void addPlayerToGame(int playerId, int x, int y) {
|
||||
// Vérifiez si le joueur existe déjà
|
||||
auto it = std::find_if(players.begin(), players.end(), [playerId](const Player& player) {
|
||||
return player.getPlayerId() == playerId;
|
||||
});
|
||||
|
||||
if (it != players.end()) {
|
||||
// Si le joueur existe déjà, mettez à jour sa position
|
||||
it->updatePosition(x, y);
|
||||
} else {
|
||||
// Sinon, ajoutez un nouveau joueur
|
||||
players.emplace_back(Player(x, y, 5, renderer, playerId));
|
||||
}
|
||||
}
|
||||
@@ -23,5 +23,6 @@ std::string receiveMessage(TCPsocket socket);
|
||||
void handleClientMessage(Player& player);
|
||||
void sendKeepAlive(TCPsocket serverSocket);
|
||||
void startKeepAlive(TCPsocket serverSocket);
|
||||
|
||||
void receivePlayerListFromServer(int serverSocket);
|
||||
void addPlayerToGame(int playerId, int x, int y);
|
||||
#endif //NETWORKING_CLIENT_H
|
||||
|
||||
@@ -231,6 +231,10 @@ void renderScene(std::vector<Player>& players, const std::vector<Kelp>& kelps, c
|
||||
player.draw(renderer);
|
||||
}
|
||||
|
||||
if (isPlayingOnline) {
|
||||
displayNearbyPlayers(renderer, font, players[0], players_server, 500);
|
||||
}
|
||||
|
||||
shark.draw(renderer);
|
||||
|
||||
if (displayFPSFlag) {
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include "../Entities/player.h"
|
||||
#include "../Entities/shark.h"
|
||||
#include "../Game/decors.h"
|
||||
#include "../Utility/display.h"
|
||||
|
||||
extern std::mutex mtx;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user