Turret placement working, fire not work segfault when trying to copy game.currentEnemies in Tower.cpp for no reason

This commit is contained in:
2024-04-30 16:36:37 +02:00
parent 9469bc99cd
commit bc4fa94d86
16 changed files with 342 additions and 23 deletions

View File

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

BIN
ressources/Laser_Tower.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

View File

@@ -5,9 +5,10 @@
#include "Enemy.h"
#include <QDebug>
Enemy::Enemy(int health, int shield, int damage, float regenerationRate, int speed, std::string avatarPath,
Enemy::Enemy(int health, int shield, int damage, int regenerationRate, int speed, std::string avatarPath,
int x, int y, int coinDrop, int weight, Map& gameMap, int id, Game& game)
: Mob(health, shield, damage, regenerationRate, speed, avatarPath, x, y), gameMap(gameMap), game(game) {
: Mob(health, shield, damage, regenerationRate, speed, avatarPath, x, y),
gameMap(gameMap), game(game), initialShield(shield) {
this->coinDrop = coinDrop;
this->weight = weight;
this->id = id;
@@ -23,6 +24,18 @@ Enemy::Enemy(int health, int shield, int damage, float regenerationRate, int spe
}
connect(moveTimer, SIGNAL(timeout()), this, SLOT(onMoveTimerTimeout()));
moveTimer->start(1000 / speed);
healthText = new QGraphicsTextItem(QString::number(health), graphics);
healthText->setDefaultTextColor(Qt::red);
healthText->setPos(graphics->boundingRect().width()/2 - healthText->boundingRect().width()/2, -healthText->boundingRect().height());
shieldText = new QGraphicsTextItem(QString::number(shield), graphics);
shieldText->setDefaultTextColor(Qt::blue);
shieldText->setPos(graphics->boundingRect().width()/2 - shieldText->boundingRect().width()/2, -shieldText->boundingRect().height() - healthText->boundingRect().height());
shieldRegenTimer = new QTimer();
connect(shieldRegenTimer, &QTimer::timeout, this, &Enemy::regenerateShield);
shieldRegenTimer->start(1000);
}
int Enemy::getWeight() {
@@ -68,4 +81,31 @@ void Enemy::moveEnemy() {
void Enemy::onMoveTimerTimeout() {
moveEnemy();
}
void Enemy::regenerateShield() {
if (shield < initialShield) {
shield += regenerationRate;
if (shield > initialShield) {
shield = initialShield;
}
shieldText->setPlainText(QString::number(shield));
}
}
void Enemy::takeDamage(int damage) {
if (shield > 0) {
shield -= damage;
if (shield < 0) {
health += shield;
shield = 0;
}
shieldText->setPlainText(QString::number(shield));
} else {
health -= damage;
}
healthText->setPlainText(QString::number(health));
if (health <= 0) {
game.removeEnemy(this);
}
}

View File

@@ -16,7 +16,7 @@ class Enemy : public Mob
{
Q_OBJECT
public:
Enemy(int health, int shield, int damage, float regenerationRate, int speed, std::string avatarPath,
Enemy(int health, int shield, int damage, int regenerationRate, int speed, std::string avatarPath,
int x, int y, int coinDrop, int weight, Map& gameMap, int id, Game& game);
int getWeight();
int getCoinDrop();
@@ -24,6 +24,7 @@ public:
void moveEnemy();
Tile* getNextPathTile();
Tile* getCurrentTile();
void takeDamage(int damage);
private slots:
void onMoveTimerTimeout();
@@ -37,6 +38,13 @@ private:
Tile* nextStep;
int id;
Game& game;
QGraphicsTextItem* healthText;
QGraphicsTextItem* shieldText;
QTimer* shieldRegenTimer;
int initialShield;
public slots:
void regenerateShield();
};

View File

@@ -18,7 +18,7 @@ Game::Game(Menu* menu) : menu(menu)
this->setFocusPolicy(Qt::StrongFocus);
// Create the player object
player = new Player(1, 0, 10, 10, 1, "../ressources/player.png", 0, 0, gameMap, *this);
player = new Player(100, 0, 10, 10, 1, "../ressources/player.png", 0, 0, gameMap, *this);
// Create the text items for the health, gold and wave number
healthDisplay = new QGraphicsTextItem();
@@ -110,7 +110,7 @@ void Game::keyPressEvent(QKeyEvent *event) {
}
}
void Game::updateDisplay() {
void Game::updateDisplay() const{
healthDisplay->setPlainText("Health: " + QString::number(player->getHealth()));
goldDisplay->setPlainText("Gold: " + QString::number(userGold));
waveDisplay->setPlainText("Wave: " + QString::number(waveNumber));
@@ -118,10 +118,10 @@ void Game::updateDisplay() {
void Game::spawnEnemies(int waveNumber) {
totalWeight = 0;
targetWeight = waveNumber * waveNumber;
targetWeight = waveNumber * 2;
int enemyId = 0;
QTimer* spawnTimer = new QTimer();
auto* spawnTimer = new QTimer();
connect(spawnTimer, &QTimer::timeout, [this, waveNumber, &enemyId, spawnTimer](){
if(totalWeight < targetWeight){
// Create a new enemy on the start tile
@@ -152,8 +152,8 @@ void Game::removeEnemy(Enemy* enemy) {
auto it = std::find(currentEnemies.begin(), currentEnemies.end(), enemy);
if (it != currentEnemies.end()) {
currentEnemies.erase(it);
delete enemy;
}
delete enemy;
}
void Game::gameOver() {
@@ -161,13 +161,14 @@ void Game::gameOver() {
enemyCheckTimer.stop();
// Remove all the enemies from the game
for (auto* enemy : currentEnemies) {
while (!currentEnemies.empty()) {
Enemy* enemy = currentEnemies.back();
if (enemy->getGraphics()->scene() == &gameMap) {
gameMap.removeItem(enemy->getGraphics());
}
delete enemy;
currentEnemies.pop_back();
delete enemy; // Delete the enemy after it has been removed from currentEnemies
}
currentEnemies.clear();
// Remove the player from the game
if (player->getGraphics()->scene() == &gameMap) {
@@ -189,6 +190,56 @@ void Game::gameOver() {
void Game::resetGame() {
// Recreate the player
player = new Player(1, 0, 10, 10, 1, "../ressources/player.png", 0, 0, gameMap, *this);
player = new Player(100, 0, 10, 10, 1, "../ressources/player.png", 0, 0, gameMap, *this);
gameMap.addItem(player->getGraphics());
}
void Game::placeTower(QMouseEvent* event) {
// Check if the click is a left click
if (event->button() != Qt::LeftButton) {
return;
}
// Convert the mouse position to scene coordinates
int gridX = event->pos().x() / 50;
int gridY = event->pos().y() / 50;
// Check if the Tile is a other tile
if (gameMap.getTile(gridX, gridY)->getType() != Tile::Other) {
return;
}
// Create a menu to select the tower type
QMenu towerMenu;
QAction* laserTower = towerMenu.addAction("Laser Tower - 50 gold");
QAction* balisticTower = towerMenu.addAction("Balistic Tower - 100 gold");
QAction* distorsionTower = towerMenu.addAction("Distorsion Tower - 75 gold");
// Display the menu and wait for the user to select an action
QAction* selectedAction = towerMenu.exec(event->globalPos());
// Create the selected tower and add it to the list of towers
if (selectedAction == laserTower && userGold >= 50) {
userGold -= 50;
Tile* tile = gameMap.getTile(gridX, gridY);
tile->setType(Tile::Tower);
auto* tower = new LaserTower(QPointF(gridX, gridY));
gameMap.addItem(tower->getGraphics());
} else if (selectedAction == balisticTower && userGold >= 100) {
userGold -= 100;
Tile* tile = gameMap.getTile(gridX, gridY);
tile->setType(Tile::Tower);
auto* tower = new BalisticTower(QPointF(gridX, gridY));
gameMap.addItem(tower->getGraphics());
} else if (selectedAction == distorsionTower && userGold >= 75) {
userGold -= 75;
Tile* tile = gameMap.getTile(gridX, gridY);
tile->setType(Tile::Tower);
auto* tower = new DistorionTower(QPointF(gridX, gridY));
gameMap.addItem(tower->getGraphics());
}
}
void Game::mousePressEvent(QMouseEvent* event) {
placeTower(event);
}

View File

@@ -14,6 +14,7 @@
#include "Enemy.h"
#include "Menu.h"
#include "Gameover.h"
#include "Tower.h"
class Player;
@@ -28,7 +29,7 @@ public:
Game(Menu* menu);
void start();
Map gameMap;
void updateDisplay();
void updateDisplay() const;
void spawnEnemies(int waveNumber);
int userGold;
std::vector<Enemy*> currentEnemies;
@@ -37,6 +38,7 @@ public:
Player* player;
void gameOver();
void resetGame();
void placeTower(QMouseEvent* event);
private:
QTimer gameTimer;
@@ -53,6 +55,7 @@ private:
protected:
void keyPressEvent(QKeyEvent* event) override;
void mousePressEvent(QMouseEvent* event) override;
};

View File

@@ -4,7 +4,7 @@
#include "Mob.h"
Mob::Mob(int health, int shield, int damage, float regenerationRate, int speed, std::string avatarPath, int x, int y){
Mob::Mob(int health, int shield, int damage, int regenerationRate, int speed, std::string avatarPath, int x, int y){
this->health = health;
this->shield = shield;
this->damage = damage;

View File

@@ -14,14 +14,14 @@ protected:
int health;
int shield;
int damage;
float regenerationRate;
int regenerationRate;
int speed;
std::string avatarPath;
int x;
int y;
public:
Mob(int health, int shield, int damage, float regenerationRate, int speed, std::string avatarPath, int x, int y);
Mob(int health, int shield, int damage, int regenerationRate, int speed, std::string avatarPath, int x, int y);
int getHealth();
int getShield();
int getDamage();

View File

@@ -5,7 +5,7 @@
#include "Player.h"
#include <iostream>
Player::Player(int health, int shield, int damage, float regenerationRate, int speed, const std::string& avatarPath, int x, int y, Map& gameMap, Game& game)
Player::Player(int health, int shield, int damage, int regenerationRate, int speed, const std::string& avatarPath, int x, int y, Map& gameMap, Game& game)
: Mob(health, shield, damage, regenerationRate, speed, avatarPath, x, y), gameMap(gameMap), game(game) {
this->x = x;
this->y = y;
@@ -28,6 +28,9 @@ void Player::setPosition(Tile* tile) {
if (enemy->getX() == x && enemy->getY() == y) {
// If there is an enemy, call the touchEnemy method
touchEnemy(enemy);
if(health <= 0) {
return;
}
break;
}
}
@@ -53,11 +56,11 @@ void Player::setPosition(int x, int y) {
}
}
void Player::getPlayerPosition() {
void Player::getPlayerPosition() const{
std::cout << "Player position: x: " << x << " y: " << y << std::endl;
}
QGraphicsPixmapItem* Player::getGraphics() {
QGraphicsPixmapItem* Player::getGraphics() const{
return graphics;
}
@@ -85,6 +88,7 @@ void Player::takeDamage(int damage) {
if (health <= 0) {
// Game over
game.gameOver();
health = 0;
}
}

View File

@@ -21,11 +21,11 @@ class Player : public Mob
{
Game& game;
public:
Player(int health, int shield, int damage, float regenerationRate, int speed, const std::string& avatarPath, int x, int y, Map& gameMap, Game& game);
Player(int health, int shield, int damage, int regenerationRate, int speed, const std::string& avatarPath, int x, int y, Map& gameMap, Game& game);
void setPosition(Tile* tile);
void setPosition(int x, int y);
void getPlayerPosition();
QGraphicsPixmapItem* getGraphics();
void getPlayerPosition() const;
QGraphicsPixmapItem* getGraphics() const;
void touchEnemy(Enemy* enemy);
void takeDamage(int damage);

View File

@@ -39,4 +39,25 @@ int Tile::gridY() {
bool Tile::isPath() {
return type == Road || type == Start || type == End;
}
void Tile::setType(Tile::Type type) {
this->type = type;
switch (type) {
case Road:
setBrush(QBrush(Qt::gray));
break;
case Start:
setBrush(QBrush(Qt::green));
break;
case End:
setBrush(QBrush(Qt::red));
break;
case Tower:
setBrush(QBrush(Qt::blue));
break;
case Other:
setBrush(QBrush(Qt::yellow));
break;
}
}

View File

@@ -15,6 +15,7 @@ public:
int gridX();
int gridY();
bool isPath();
void setType(Type type);
private:
Type type;

123
src/Tower.cpp Normal file
View File

@@ -0,0 +1,123 @@
//
// Created by BreizhHardware on 30/04/2024.
//
#include <memory_resource>
#include "Tower.h"
Tower::Tower(int damage, int fireRate, int range, int level, int cost, QPointF position,
std::string avatarPath, Game& game) : game(game) {
this->damage = damage;
this->fireRate = fireRate;
this->range = range;
this->level = level;
this->cost = cost;
this->position = position;
this->avatarPath = avatarPath;
fireTimer = new QTimer();
connect(fireTimer, &QTimer::timeout, this, &Tower::fire);
fireTimer->start(fireRate * 1000);
}
void Tower::fireAtClosest(Enemy* target) const {
qDebug() << "Fire";
if (target == nullptr) {
return;
}
target->takeDamage(damage);
}
Enemy* Tower::getClosestEnemyInRange(const std::vector<Enemy*>& enemies) {
Enemy* closestEnemy = nullptr;
// Draw a circle around the tower
double minDistance = range;
for (Enemy* enemy : enemies) {
double distance = sqrt(pow(enemy->getX() - position.x(), 2) + pow(enemy->getY() - position.y(), 2));
if (distance < minDistance) {
minDistance = distance;
closestEnemy = enemy;
}
}
return closestEnemy;
}
void Tower::fire() {
std::vector<Enemy*> enemies = game.currentEnemies;
Enemy* target = getClosestEnemyInRange(enemies);
fireAtClosest(target);
}
LaserTower::LaserTower(QPointF position) : Tower(5, 1, 10, 1, 50, position,
"../ressources/Laser_Tower.png", game) {
QPixmap pixmap(QString::fromStdString(avatarPath));
// Check if the pixmap is null
if(pixmap.isNull()) {
} else {
// Get x and y from the position
int x = position.x();
int y = position.y();
graphics = new QGraphicsPixmapItem();
QPixmap scaledPixmap = pixmap.scaled(50, 50, Qt::KeepAspectRatio, Qt::SmoothTransformation); // Scale the pixmap to 50x50 pixels
graphics->setPixmap(scaledPixmap);
graphics->setPos(x * 50, y * 50);
graphics->setZValue(1);
}
}
QGraphicsPixmapItem* Tower::getGraphics() {
return graphics;
}
void LaserTower::upgrade() {
damage += 5;
level += 1;
cost += 50;
}
BalisticTower::BalisticTower(QPointF position) : Tower(15, 2, 5, 1, 100, position,
"../ressources/Balistic_Tower.png", game) {
QPixmap pixmap(QString::fromStdString(avatarPath));
// Check if the pixmap is null
if(pixmap.isNull()) {
} else {
// Get x and y from the position
int x = position.x();
int y = position.y();
graphics = new QGraphicsPixmapItem();
QPixmap scaledPixmap = pixmap.scaled(50, 50, Qt::KeepAspectRatio, Qt::SmoothTransformation); // Scale the pixmap to 50x50 pixels
graphics->setPixmap(scaledPixmap);
graphics->setPos(x * 50, y * 50);
graphics->setZValue(1);
}
}
void BalisticTower::upgrade() {
damage += 10;
level += 1;
cost += 100;
}
DistorionTower::DistorionTower(QPointF position) : Tower(10, 1, 7, 1, 75, position,
"../ressources/Distortion_Tower.png", game) {
QPixmap pixmap(QString::fromStdString(avatarPath));
// Check if the pixmap is null
if(pixmap.isNull()) {
} else {
// Get x and y from the position
int x = position.x();
int y = position.y();
graphics = new QGraphicsPixmapItem();
QPixmap scaledPixmap = pixmap.scaled(50, 50, Qt::KeepAspectRatio, Qt::SmoothTransformation); // Scale the pixmap to 50x50 pixels
graphics->setPixmap(scaledPixmap);
graphics->setPos(x * 50, y * 50);
graphics->setZValue(1);
}
}
void DistorionTower::upgrade() {
damage += 5;
level += 1;
cost += 75;
}

66
src/Tower.h Normal file
View File

@@ -0,0 +1,66 @@
//
// Created by BreizhHardware on 30/04/2024.
//
#ifndef TOWER_H
#define TOWER_H
#include <QObject>
#include <QPointF>
#include <vector>
#include <QGraphicsPixmapItem>
#include <QTimer>
#include "Enemy.h"
#include "Game.h"
class Enemy;
class Game;
class Tower : public QObject
{
QOBJECT_H
protected:
int damage;
int fireRate;
int range;
int level;
int cost;
QPointF position;
std::string avatarPath;
QGraphicsPixmapItem* graphics{};
Game& game;
QTimer* fireTimer;
public:
Tower(int damage, int fireRate, int range, int level, int cost, QPointF position, std::string avatarPath, Game& game);
virtual void upgrade() = 0;
virtual ~Tower() = default;
QGraphicsPixmapItem* getGraphics();
void fireAtClosest(Enemy* target = nullptr) const;
Enemy* getClosestEnemyInRange(const std::vector<Enemy*>& enemies);
public slots:
void fire();
};
class LaserTower : public Tower {
public:
explicit LaserTower(QPointF position);
void upgrade() override;
};
class BalisticTower : public Tower {
public:
explicit BalisticTower(QPointF position);
void upgrade() override;
};
class DistorionTower : public Tower {
public:
explicit DistorionTower(QPointF position);
void upgrade() override;
};
#endif //TOWER_H