mirror of
https://github.com/BreizhHardware/bloubloulespoissons.git
synced 2026-01-18 16:47:31 +01:00
Début du shark en network
This commit is contained in:
6461
img/shark.pdn
Normal file
6461
img/shark.pdn
Normal file
File diff suppressed because one or more lines are too long
BIN
img/shark.png
BIN
img/shark.png
Binary file not shown.
|
Before Width: | Height: | Size: 1.6 MiB After Width: | Height: | Size: 1.1 MiB |
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user