From 0d1db6ad458302637e1e1ae9862d8c1190b9810d Mon Sep 17 00:00:00 2001 From: cboissie Date: Wed, 25 May 2011 19:26:35 +0200 Subject: [PATCH] - Turrets can be upgraded or sold - Turrets can be selectionned with the mouse to display infos - Turret range is now correctly positionned but still have problem with enemy detection (detects from too far away) --- defenser.cpp | 102 ++++++++++++++++++++++++++++++++++--------------- defenser.h | 25 +++++++++--- mainwindow.cpp | 10 ++++- mainwindow.ui | 23 +++++++---- map.cpp | 94 +++++++++++++++++++++++++++++++++++++++------ map.h | 13 ++++++- player.cpp | 43 +++++++++++++++++++-- player.h | 6 ++- tile.cpp | 5 +-- tile.h | 4 +- types.h | 3 +- 11 files changed, 259 insertions(+), 69 deletions(-) diff --git a/defenser.cpp b/defenser.cpp index 3446703..c8ada29 100644 --- a/defenser.cpp +++ b/defenser.cpp @@ -9,7 +9,47 @@ **/ Defenser::Defenser(int posx,int posy,int level,TYPE::ENTITY target,Map* map) - :Entity(posx,posy),_level(level),_target(target),_map(map) {} + :Entity(posx,posy),_level(level),_target(target),_map(map) +{ + this->setIsSelected(true); // Appel à la méthode pour rafraichir l'affichage +} + + +void Defenser::setIsSelected(bool selected) { + _isSelected = selected; + _map->update(); // Met à jour l'affichage +} + +int Defenser::getLevel(void) const { return _level; } +bool Defenser::isSelected(void) const { return _isSelected; } + +void Defenser::increaseLevel(void) { + if(_level < 3) _level++; + this->updateStats(); +} + + +void Defenser::setIsShooting(bool state) { + + _isShooting = state; + + // MODE TIR ACTIVÉ + if(state) + { + // Change l'aspect visuel + this->setOpacity(0.5f); // TEST = TIR ACTIVÉ + // Active le timer + _shootTimer.start(100*_rate); + } + // MODE TIR DESACTIVÉ + else + { + // Change l'aspect visuel + this->setOpacity(1.0f); // TEST = TIR ACTIVÉ + // Désactive le timer + _shootTimer.stop(); + } +} /** @@ -24,21 +64,37 @@ WaterGun::WaterGun(int posx,int posy,int level,TYPE::ENTITY target,Map* map) QObject::connect(&_shootTimer,SIGNAL(timeout()),this,SLOT(shootTarget())); } -int WaterGun::getCost(void) const{ +int WaterGun::getCost(int level) const{ - switch(_level) { + // Si aucune valeur en argument, alors on affiche le coût du niveau courant + if(level == -1) level = _level; + + switch(level) { case 1: return 8; case 2: return 20; default: return 45; } } +QString WaterGun::getInfos(void) const { + + QString infos = "Vous avez clique sur un pistolet a eau de niveau "; + infos+= QString::number(_level); + infos+= "\n- Amelioration : "; + infos+= QString::number(this->getCost(_level+1)); + infos+= "\n- Revente : "; + infos+= QString::number(this->getCost(_level)/2); + + return infos; +} + bool WaterGun::isShooting(void) const { return _isShooting;} +bool WaterGun::isLevelMax(void) const { return _level==3; } void WaterGun::updateStats(void) { - _range = (2 + _level/2)*50; + _range = (2.0f + _level/2.0f)*32.0f; _rate = 4 - _level/2; _power = 5 * _level * sqrt(_level); // level ^ 1.5 @@ -56,13 +112,21 @@ void WaterGun::setTarget(float targetX,float targetY) { void WaterGun::paint(QPainter *painter, const QStyleOptionGraphicsItem *,QWidget*) { - painter->setBrush(QBrush(Qt::red)); + // Si la tourelle est selectionnée, on dessine un halo autour + if(_isSelected) { + painter->setBrush(QBrush(Qt::yellow)); + painter->drawRect(-2,-2,36,36); + } + // Dessine la tourelle + painter->setBrush(QBrush(Qt::red)); painter->drawRect(0,0,32,32); // Dessine la portée de la tourelle painter->setBrush(QBrush(Qt::blue,Qt::NoBrush)); - painter->drawEllipse(-32 - 2,-32 - 2,_range,_range); + painter->drawEllipse((-_range/32)*16+16,(-_range/32)*16+16,_range,_range); + + _map->update(); } @@ -82,8 +146,8 @@ void WaterGun::advance(int phase) { float targetX = (*i)->x(); float targetY = (*i)->y(); - if(sqrt(pow(fabs(this->x() - targetX - ((*i)->getSize()-1)*16),2) - +pow(fabs(this->y() - targetY - ((*i)->getSize()-1)*16),2) + if(sqrt(pow(fabs(this->x()-16 - targetX - ((*i)->getSize()-1)*16),2) + +pow(fabs(this->y()-16 - targetY - ((*i)->getSize()-1)*16),2) ) <= _range) { // Si le défenseur ne tirait pas encore @@ -104,28 +168,6 @@ void WaterGun::advance(int phase) { } // eom -void WaterGun::setIsShooting(bool state) { - - _isShooting = state; - - // MODE TIR ACTIVÉ - if(state) - { - // Change l'aspect visuel - this->setOpacity(0.5f); // TEST = TIR ACTIVÉ - // Active le timer - _shootTimer.start(100*_rate); - } - // MODE TIR DESACTIVÉ - else - { - // Change l'aspect visuel - this->setOpacity(1.0f); // TEST = TIR ACTIVÉ - // Désactive le timer - _shootTimer.stop(); - } -} - void WaterGun::shootTarget(void) { // Créé un nouveau projectile au niveau du centre de la tourelle diff --git a/defenser.h b/defenser.h index 2710987..471835c 100644 --- a/defenser.h +++ b/defenser.h @@ -17,20 +17,34 @@ class Defenser : public Entity public: Defenser(int posx,int posy,int level,TYPE::ENTITY target,Map* map); - virtual int getCost(void) const = 0; + int getLevel(void) const; + virtual int getCost(int level = -1) const = 0; + virtual QString getInfos(void) const = 0; + void increaseLevel(void); + virtual bool isLevelMax(void) const = 0; + bool isSelected(void) const; + void setIsShooting(bool state); protected: + virtual void updateStats(void) = 0; + QTimer _shootTimer; int _level; TYPE::ENTITY _target; int _rate; - int _range; + float _range; int _power; float _targetX; float _targetY; - bool _isShooting; Map* _map; + bool _isShooting; + bool _isSelected; +public slots: + void setIsSelected(bool selected); + +signals: + void turretRemoved(Defenser*); }; @@ -45,11 +59,12 @@ class WaterGun : public Defenser public: WaterGun(int posx,int posy,int level,TYPE::ENTITY target,Map* map); - int getCost(void) const; + int getCost(int level = -1) const; + QString getInfos(void) const; bool isShooting(void) const; + bool isLevelMax(void) const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *,QWidget*); void setTarget(float targetX,float targetY); - void setIsShooting(bool state); QRectF boundingRect(void) const; static int getBasicCost(void) { return 8; } diff --git a/mainwindow.cpp b/mainwindow.cpp index 2a609b7..b5d90a9 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -12,7 +12,7 @@ MainWindow::MainWindow(QWidget *parent) : /** * Initialisation des données du joueur **/ - _player = new Player(20,10); + _player = new Player(999,10); /** * Création et chargement de la map @@ -50,10 +50,16 @@ MainWindow::MainWindow(QWidget *parent) : /// Boutons de choix de tourelle // . Pistolet à eau QObject::connect(ui->turretButton_PistoletAEau,SIGNAL(pressed()),_player,SLOT(setTurretChoice1())); + QObject::connect(_player,SIGNAL(setTurret1ButtonDisabled(bool)),this->ui->turretButton_PistoletAEau,SLOT(setDisabled(bool))); // Demande d'informations sur une tourelle - // QObject::connect(_sceneMap,SIGNAL(loadTurretInfos(QString)),ui->turretInfoLabel,SLOT(setText(QString))); + QObject::connect(_sceneMap,SIGNAL(turretInfosRequest(QString)),ui->turretInfoLabel,SLOT(setText(QString))); + // Amélioration d'une tourelle + QObject::connect(ui->upgradeTurretButton,SIGNAL(clicked()),_sceneMap,SLOT(upgradeTurret())); + // Revente d'une tourelle + QObject::connect(ui->sellTurretButton,SIGNAL(clicked()),_sceneMap,SLOT(removeTurret())); + QObject::connect(_sceneMap,SIGNAL(turretSold(int)),_player,SLOT(turretSold(int))); /// Gestions des compteurs diff --git a/mainwindow.ui b/mainwindow.ui index 3aa12cc..18fadfc 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -148,24 +148,33 @@ - 0 + 10 20 - 261 - 71 + 241 + 81 - TextLabel + Cliquez sur une tourelle pour avoir des informations, l'améliorer ou la revendre. + + + Qt::AutoText + + + false - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + Qt::AlignJustify|Qt::AlignTop + + + true 20 - 90 + 100 98 27 @@ -178,7 +187,7 @@ 140 - 90 + 100 98 27 diff --git a/map.cpp b/map.cpp index fc58e4d..b2ee921 100644 --- a/map.cpp +++ b/map.cpp @@ -40,10 +40,21 @@ void Map::mousePressEvent(QGraphicsSceneMouseEvent *event) { qreal mx = event->scenePos().x(); qreal my = event->scenePos().y(); - - // CLIC GAUCHE : POSE DE TOURELLE - if(event->button() == Qt::LeftButton) - this->addTurretAt(_player->getTurretChoice(),mx,my); + // Par défaut déselectionne toute tourelle + emit setTurretsSelected(false); + + // CLIC GAUCHE + if(event->button() == Qt::LeftButton) { + // SELECTION D'UNE TOURELLE (affichage infos) + if(Defenser* turret = this->getTurretAt((int)(mx/32)*32,(int)(my/32)*32)) { + emit turretInfosRequest(turret->getInfos()); + turret->setIsSelected(true); + } + // POSE DE TOURELLE + else{ + this->addTurretAt(_player->getTurretChoice(),mx,my); + } + } } @@ -72,9 +83,8 @@ void Map::removeEnemy(Enemy* ptr) { int i; for(i = 0; i < _enemies.length() ; ++i) { - std::cout << _enemies[i] << " " << ptr << std::endl; if(_enemies[i] == ptr){ - std::cout << "OMG un mort" << std::endl; + std::cout << "OMG je suis mort" << std::endl; _enemies.removeAt(i); // QObject::disconnect(ptr); this->removeItem(ptr); @@ -119,15 +129,14 @@ bool Map::addTurretAt(TYPE::TURRET turretType,float mx,float my) { // Ajout de la tourelle et mise à jour des informations de la map if(enoughMoney) { - // Le tile contient désormais une tourelle - this->getTileAt(mx/32,my/32).setHasTurret(true); // Ajout sur la map this->addItem(newTurret); + // Sauvegarde dans la liste des tourelles + _turrets.push_back(newTurret); // Gestion des clics sur la tourelle - // QObject::connect(newTurret,SIGNAL(turretClicked(QString)),this,SLOT(turretInfosRequest(QString))); - - // On remet à zéro le choix du joueur + QObject::connect(this,SIGNAL(setTurretsSelected(bool)),newTurret,SLOT(setIsSelected(bool))); + // On remet à zéro le choix du joueur _player->setTurretChoice(TYPE::NONE); return true; // Succès @@ -138,7 +147,68 @@ bool Map::addTurretAt(TYPE::TURRET turretType,float mx,float my) { return false; // Echec } +// Renvoie la tourelle positionnée à la position (x,y) +Defenser* Map::getTurretAt(int grid_x,int grid_y) { + + int i; + for(i = 0; i < _turrets.length() ; ++i) { + + if(_turrets[i]->x() == grid_x && _turrets[i]->y() == grid_y) + return _turrets[i]; + } + // Si aucune tourelle trouvée + return NULL; + +} + +void Map::removeTurret(void) { + + // Garbage collector de notre liste + + int i; + for(i = 0; i < _turrets.length() ; ++i) { + + if(_turrets[i]->isSelected()){ + // Arrête tout tir en cours + _turrets[i]->setIsShooting(false); + // Donne de l'argent au joueur pour la vente + emit turretSold(_turrets[i]->getCost()/2); + // Retire l'objet de la map et de la liste + this->removeItem(_turrets[i]); + _turrets.removeAt(i); + + break; + } + } +} + +void Map::upgradeTurret(void) { + + // Recherche la tourelle actuellement selectionnée et tente de l'améliorer + + int i; + for(i = 0; i < _turrets.length() ; ++i) { + + if(_turrets[i]->isSelected()) + { + // La tourelle peut être améliorée + if(_turrets[i]->isLevelMax()) + break; + + // ... et si le joueur a assez d'argent + if(_player->payMoney(_turrets[i]->getCost(_turrets[i]->getLevel()+1))) { + // Level-up de la tourelle + _turrets[i]->increaseLevel(); + // Mise à jour de la description + emit turretInfosRequest(_turrets[i]->getInfos()); + } + break; + } + } +} + + QPoint Map::getStart(void) const { return _startPos; } -Tile& Map::getTileAt(int x, int y) const { return *_mapTiles[x][y]; } +Tile& Map::getTileAt(int grid_x, int grid_y) const { return *_mapTiles[grid_x][grid_y]; } EnemyFactory* Map::getWaveGenerator(void) const { return _waveGenerator; } diff --git a/map.h b/map.h index 6738ec7..908504a 100644 --- a/map.h +++ b/map.h @@ -15,6 +15,7 @@ class EnemyFactory; class Enemy; +class Defenser; class Map : public QGraphicsScene { @@ -24,6 +25,7 @@ class Map : public QGraphicsScene EnemyFactory* _waveGenerator; Tile* _mapTiles[16][16]; QList _enemies; // OPTIMISATION DES RECHERCHES D'ENNEMI + QList _turrets; QGraphicsScene* _scene; QPoint _startPos; Player* _player; @@ -36,12 +38,21 @@ public: void addEnemy(Enemy* enemy); bool addTurretAt(TYPE::TURRET turretType,float mx,float my); QPoint getStart(void) const; - Tile& getTileAt(int x, int y) const; + Tile& getTileAt(int grid_x, int grid_y) const; + Defenser* getTurretAt(int grid_x,int grid_y); EnemyFactory* getWaveGenerator(void) const; QList getEnemyList(void) const; + public slots: void removeEnemy(Enemy* ptr); + void removeTurret(void); + void upgradeTurret(void); + +signals : + void turretInfosRequest(QString); + void setTurretsSelected(bool); + void turretSold(int); }; #endif // MAP_H diff --git a/player.cpp b/player.cpp index e3a1873..ad36ec6 100644 --- a/player.cpp +++ b/player.cpp @@ -18,12 +18,12 @@ int Player::getMoney(void) const { return _money; } int Player::getLives(void) const { return _lives; } TYPE::TURRET Player::getTurretChoice(void) const { return _turretChoice; } -void Player::earnMoney(int amount) { +void Player::earnMoney(int money) { // Si on gagne (réellement) de l'argent, celui ci est ajouté au pactole // ... et le compteur est notifié - if(amount >= 0) { - _money+=amount; + if(money >= 0) { + _money+=money; emit moneyValueChanged(_money); } } @@ -39,9 +39,44 @@ void Player::loseLives(int livesLost) { emit livesValueChanged(_lives); } +void Player::turretSold(int money) { + + if(money>0) + _money+=money; + + emit moneyValueChanged(_money); + +} + void Player::setTurretChoice(TYPE::TURRET choice) { + + // Active le bouton associé à l'ancien choix + if(choice != _turretChoice) { + + switch(_turretChoice) { + + case TYPE::PISTOLET_A_EAU : + emit setTurret1ButtonDisabled(false); + break; + default: + break; + } + } + + // Met à jour le choix courant _turretChoice = choice; - std::cout << "Le joueur a choisi " << choice << std::endl; + + // Désactive le bouton associé au nouveau choix + if(_turretChoice != TYPE::NONE) { + switch(_turretChoice) { + case TYPE::PISTOLET_A_EAU: + emit setTurret1ButtonDisabled(true); + break; + default: + break; + } + } + } void Player::setTurretChoice1(){this->setTurretChoice(TYPE::PISTOLET_A_EAU);} diff --git a/player.h b/player.h index 209b33e..4aeb0a7 100644 --- a/player.h +++ b/player.h @@ -27,13 +27,17 @@ public: public slots: - void earnMoney(int amount); + void earnMoney(int money); void loseLives(int livesLost); + void turretSold(int money); void setTurretChoice1(); + signals: void moneyValueChanged(int); void livesValueChanged(int); + void setTurret1ButtonDisabled(bool); + void turretSelected(); }; #endif // PLAYER_H diff --git a/tile.cpp b/tile.cpp index f3a2658..7d336e2 100644 --- a/tile.cpp +++ b/tile.cpp @@ -38,7 +38,7 @@ Tile::Tile(int tileNumber, int x, int y):_tileNumber(tileNumber) // Données du tile _x = x; _y = y; - _hasTurret = false; + *_pixmap = _pixmap->scaled(32,32,Qt::IgnoreAspectRatio); this->setPixmap(*_pixmap); @@ -51,7 +51,7 @@ QPointF Tile::getDirection(void) const { return _dir; } bool Tile::turretAllowed(void) const { - if(_tileNumber == 0 && !_hasTurret) + if(_tileNumber == 0) return true; return false; } @@ -95,6 +95,5 @@ QString Tile::TexName(int n){ return "grass"; } -void Tile::setHasTurret(bool){ _hasTurret = true; } Tile::~Tile(){} diff --git a/tile.h b/tile.h index 42d209b..2ed3d2d 100644 --- a/tile.h +++ b/tile.h @@ -16,8 +16,7 @@ public: int getY() const; int getTileNumber(void) const; bool turretAllowed(void) const; - void setHasTurret(bool); - QPointF getDirection(void) const; + QPointF getDirection(void) const; QString TexName(int n); @@ -26,7 +25,6 @@ private: QPixmap *_pixmap; int _x, _y; int _tileNumber; - bool _hasTurret; QPointF _dir; }; diff --git a/types.h b/types.h index de8b39d..8b1142f 100644 --- a/types.h +++ b/types.h @@ -11,6 +11,7 @@ typedef enum { T_RAMPANT = 1, T_VOLANT = 2 } ENTITY; typedef enum { CAFARD } ENEMY; typedef enum { NONE, PISTOLET_A_EAU } TURRET; -} + +}; #endif // TYPES_H -- 2.43.0