add collisions

This commit is contained in:
Clément Fouché
2024-12-12 14:21:32 +01:00
parent 4745f6fb19
commit 100a6b91d1
3 changed files with 13 additions and 0 deletions

View File

@@ -41,6 +41,7 @@ class Player {
bool onlineMovement();
int unifiedX = 0;
int unifiedY = 0;
int HITBOX = 50;
public:
Player(int x, int y, int playerSpeed, SDL_Renderer* renderer, int playerId) : x(x), y(y), playerBaseX(x), playerBaseY(y), playerSpeed(playerSpeed), playerId(playerId) {
playerPosForRender.x = x;
@@ -75,6 +76,8 @@ class Player {
return playerTexture;
}
int getHITBOX() const {return HITBOX;}
};
#endif

View File

@@ -55,6 +55,12 @@ void Shark::checkNeighborhood(Player& player, float &xpos_avg, float &ypos_avg,
}
}
void Shark::checkCollision(Player& player) {
if (player.getUnifiedX() + player.getHITBOX() >= x - HITBOX && player.getUnifiedX() - player.getHITBOX() <= x + HITBOX && player.getUnifiedY() + player.getHITBOX() >= y - HITBOX && player.getUnifiedY() - player.getHITBOX() <= y + HITBOX) {
game_running = false;
}
}
void Shark::cycle() {
int neighboring_player = 0;
@@ -62,6 +68,7 @@ void Shark::cycle() {
for (auto& player : players_list) {
if (isInView(player)) {
checkNeighborhood(player, xpos_avg, ypos_avg, xvel_avg, yvel_avg, neighboring_player);
checkCollision(player);
}
}
if (neighboring_player > 0) {

View File

@@ -21,6 +21,7 @@ private:
const float MAX_SPEED = 3;
const float MIN_SPEED = 0.6;
const float BIASVALUE = 0.001;
const int HITBOX = 50;
float x, y;
float vx, vy;
@@ -42,6 +43,7 @@ public:
float getY() const { return y; };
float getVx() const { return vx; };
float getVy() const { return vy; };
int getHITBOX() const { return HITBOX; };
@@ -52,6 +54,7 @@ public:
bool isInView(Player& player);
void checkNeighborhood(Player& player, float &xpos_avg, float &ypos_avg, float &xvel_avg, float &yvel_avg, int &neighboring_player);
void updatePosition(int newX, int newY);
void checkCollision(Player& player);
};