mirror of
https://github.com/BreizhHardware/Poulpes-de-l-Espace-La-derniere-ligne-de-Defense.git
synced 2026-01-18 16:27:20 +01:00
Add PlayerDeadException to avoid a segfault
# To do: - Upgrade des tours - Equilibrage - Dasagne - Easter egg - I'm a teapot
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "Mob.h"
|
||||
#include "Map.h"
|
||||
#include "Game.h"
|
||||
#include "PlayerDeadException.h"
|
||||
|
||||
class Game;
|
||||
|
||||
|
||||
@@ -90,6 +90,8 @@ void Player::takeDamage(int damage) {
|
||||
// Game over
|
||||
game.gameOver();
|
||||
health = 0;
|
||||
game.updateDisplay();
|
||||
throw PlayerDeadException();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "Map.h"
|
||||
#include "Enemy.h"
|
||||
#include "Game.h"
|
||||
#include "PlayerDeadException.h"
|
||||
#include <QGraphicsEllipseItem>
|
||||
#include <QGraphicsPixmapItem>
|
||||
#include <QBrush>
|
||||
|
||||
9
src/PlayerDeadException.cpp
Normal file
9
src/PlayerDeadException.cpp
Normal 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
17
src/PlayerDeadException.h
Normal 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
|
||||
Reference in New Issue
Block a user