Début du shark en network

This commit is contained in:
2024-12-12 08:39:28 +01:00
parent 98e9fedd71
commit f57318d739
6 changed files with 6475 additions and 5 deletions

6461
img/shark.pdn Normal file

File diff suppressed because one or more lines are too long

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.6 MiB

After

Width:  |  Height:  |  Size: 1.1 MiB

View File

@@ -204,4 +204,10 @@ void handleServerMessages() {
}
}
}
}
}
void sendSharkPosition(TCPsocket socket, int sharkId, int x, int y) {
std::string message = std::to_string(sharkId) + ";shark_moved;" + std::to_string(x) + "," + std::to_string(y);
sendMessage(socket, message);
SDL_Delay(16);
}

View File

@@ -23,5 +23,6 @@ void updateKeepAlive(int clientId);
void checkClientAlive();
void closeServer();
void handleServerMessages();
void sendSharkPosition(TCPsocket socket, int sharkId, int x, int y);
#endif //NETWORKING_H

View File

@@ -5,6 +5,8 @@
#define sharkIMG "../img/shark.png"
#include "shark.h"
#include "network/networking.h"
Shark::Shark(const int x, const int y, const float vx, const float vy, const int id, const int width, const int height, SDL_Renderer* renderer, std::vector<Player> &players_list)
: x(x), y(y), vx(vx), vy(vy), id(id), width(width), height(height), players_list(players_list) {
SDL_Surface* sharkSurface = IMG_Load(sharkIMG);
@@ -34,7 +36,6 @@ void Shark::draw(SDL_Renderer *renderer) {
}
bool Shark::isInView(Player& player) {
std::cout << "Player x: " << player.getUnifiedX() << " y: " << player.getUnifiedY() << std::endl;
std::cout.flush();
return player.getUnifiedX() >= x - VISUAL_RANGE && player.getUnifiedX() <= x + VISUAL_RANGE && player.getUnifiedY() >= y - VISUAL_RANGE && player.getUnifiedY() <= y + VISUAL_RANGE;
}
@@ -53,10 +54,8 @@ void Shark::checkNeighborhood(Player& player, float &xpos_avg, float &ypos_avg,
void Shark::cycle() {
int neighboring_player = 0;
float xvel_avg = 0, yvel_avg = 0, xpos_avg = 0, ypos_avg = 0;
std::cout << "Shark x: " << x << " y: " << y << std::endl;
for (auto& player : players_list) {
if (isInView(player)) {
std::cout << "Player x: " << player.getUnifiedX() << " y: " << player.getUnifiedY() << std::endl;
checkNeighborhood(player, xpos_avg, ypos_avg, xvel_avg, yvel_avg, neighboring_player);
}
}
@@ -80,4 +79,7 @@ void Shark::cycle() {
x += vx;
y += vy;
if (isPlayingOnline && isHost) {
sendSharkPosition(client, id, x, y);
}
}

View File

@@ -16,7 +16,7 @@ private:
const float MATCHING_FACTOR = 0.05;
const float CENTERING_FACTOR = 0.005;
const float TURN_FACTOR = 0.3;
const float MAX_SPEED = 8;
const float MAX_SPEED = 3;
const float MIN_SPEED = 0.6;
const float BIASVALUE = 0.001;