Add PlayerDeadException to avoid a segfault

# To do:
- Upgrade des tours
- Equilibrage
- Dasagne
- Easter egg
- I'm a teapot
This commit is contained in:
2024-05-01 17:00:25 +02:00
parent e66c4adbca
commit 890fd479e1
7 changed files with 53 additions and 17 deletions

View File

@@ -33,6 +33,8 @@ add_executable(Poulpes_de_l_espace_La_derniere_ligne_de_defense ${sourceCode}
src/Gameover.cpp
src/Gameover.h
src/Tower.cpp
src/Tower.h)
src/Tower.h
src/PlayerDeadException.cpp
src/PlayerDeadException.h)
target_link_libraries(Poulpes_de_l_espace_La_derniere_ligne_de_defense PRIVATE Qt6::Widgets Qt6::Sql)

View File

@@ -66,23 +66,27 @@ Tile* Enemy::getCurrentTile() {
}
void Enemy::moveEnemy() {
// Move the enemy to the next path tile
nextStep = getNextPathTile();
if (nextStep != nullptr) {
x = nextStep->gridX();
y = nextStep->gridY();
graphics->setPos(x * 50, y * 50);
// Check if the enemy is on the end tile and deal damage
if (getCurrentTile() == gameMap.getEndTile()) {
game.player->takeDamage(getDamage());
game.removeEnemy(this);
}
// Check if the player is on the same tile as the enemy and deal damage
if (game.player->getX() == x && game.player->getY() == y) {
game.player->takeDamage(getDamage());
game.userGold += coinDrop;
game.removeEnemy(this);
try {
// Move the enemy to the next path tile
nextStep = getNextPathTile();
if (nextStep != nullptr) {
x = nextStep->gridX();
y = nextStep->gridY();
graphics->setPos(x * 50, y * 50);
// Check if the enemy is on the end tile and deal damage
if (getCurrentTile() == gameMap.getEndTile()) {
game.player->takeDamage(getDamage());
game.removeEnemy(this);
}
// Check if the player is on the same tile as the enemy and deal damage
if (game.player->getX() == x && game.player->getY() == y) {
game.player->takeDamage(getDamage());
game.userGold += coinDrop;
game.removeEnemy(this);
}
}
} catch (PlayerDeadException& e) {
return;
}
}

View File

@@ -9,6 +9,7 @@
#include "Mob.h"
#include "Map.h"
#include "Game.h"
#include "PlayerDeadException.h"
class Game;

View File

@@ -90,6 +90,8 @@ void Player::takeDamage(int damage) {
// Game over
game.gameOver();
health = 0;
game.updateDisplay();
throw PlayerDeadException();
}
}

View File

@@ -9,6 +9,7 @@
#include "Map.h"
#include "Enemy.h"
#include "Game.h"
#include "PlayerDeadException.h"
#include <QGraphicsEllipseItem>
#include <QGraphicsPixmapItem>
#include <QBrush>

View File

@@ -0,0 +1,9 @@
//
// Created by breizhhardware on 01/05/24.
//
#include "PlayerDeadException.h"
const char* PlayerDeadException::what() const noexcept {
return "Player is dead";
}

17
src/PlayerDeadException.h Normal file
View File

@@ -0,0 +1,17 @@
//
// Created by breizhhardware on 01/05/24.
//
#ifndef POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_PLAYERDEADEXCEPTION_H
#define POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_PLAYERDEADEXCEPTION_H
#include <exception>
class PlayerDeadException : public std::exception
{
public:
const char* what() const noexcept override;
};
#endif //POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_PLAYERDEADEXCEPTION_H