Start adding capability to see other players

This commit is contained in:
2024-12-08 17:26:03 +01:00
parent aed1033f43
commit fb9df2ae08
4 changed files with 57 additions and 0 deletions

View File

@@ -26,6 +26,8 @@ add_executable(bloubloulespoissons main.cpp fish.cpp decors.cpp
network/networking.h
network/networking_client.cpp
network/networking_client.h
display.cpp
display.h
)
# Lier SDL2 et SDL2_image

36
display.cpp Normal file
View File

@@ -0,0 +1,36 @@
//
// Created by BreizhHardware on 08/12/2024.
//
#include "display.h"
double calculateDistance(int x1, int y1, int x2, int y2) {
return std::sqrt(std::pow(x2 - x1, 2) + std::pow(y2 - y1, 2));
}
void displayNearbyPlayers(SDL_Renderer* renderer, TTF_Font* font, Player& currentPlayer, std::vector<Player>& players, double threshold) {
int currentX = currentPlayer.getUnifiedX();
int currentY = currentPlayer.getUnifiedY();
int offsetY = 90; // Starting Y position for displaying nearby players
for (auto& player : players) {
if (&player == &currentPlayer) continue; // Skip the current player
int playerX = player.getUnifiedX();
int playerY = player.getUnifiedY();
double distance = calculateDistance(currentX, currentY, playerX, playerY);
if (distance < threshold) {
std::string nearbyPlayerText = "Nearby Player: (" + std::to_string(playerX) + ", " + std::to_string(playerY) + ")";
SDL_Color textColor = {0, 255, 0};
SDL_Surface* textSurface = TTF_RenderText_Solid(font, nearbyPlayerText.c_str(), textColor);
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_Rect textRect = {10, offsetY, textSurface->w, textSurface->h};
SDL_RenderCopy(renderer, textTexture, nullptr, &textRect);
SDL_FreeSurface(textSurface);
SDL_DestroyTexture(textTexture);
offsetY += 20; // Move down for the next player
}
}
}

17
display.h Normal file
View File

@@ -0,0 +1,17 @@
//
// Created by BreizhHardware on 08/12/2024.
//
#ifndef DISPLAY_H
#define DISPLAY_H
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <vector>
#include <cmath>
#include <string>
#include "player.h"
double calculateDistance(int x1, int y1, int x2, int y2);
void displayNearbyPlayers(SDL_Renderer* renderer, TTF_Font* font, Player& currentPlayer, std::vector<Player>& players, double threshold);
#endif //DISPLAY_H

View File

@@ -12,6 +12,7 @@
#include "fish.h"
#include "decors.h"
#include "camera.h"
#include "display.h"
#include "env.h"
#include "player.h"
#include "menu.h"
@@ -599,6 +600,7 @@ void renderScene(std::vector<Player>& players, const std::vector<Kelp>& kelps, c
int unifiedX = player.getUnifiedX();
int unifiedY = player.getUnifiedY();
displayUnifiedPlayerCoord(renderer, font, unifiedX, unifiedY);
displayNearbyPlayers(renderer, font, player, players, 200.0);
}
SDL_RenderPresent(renderer);