]> khamidou — GitHub mirrors - Bug_Attack.git/commitdiff
- Turrets can be upgraded or sold
authorcboissie <clemzbox@gmail.com>
Wed, 25 May 2011 17:26:35 +0000 (19:26 +0200)
committercboissie <clemzbox@gmail.com>
Wed, 25 May 2011 17:26:35 +0000 (19:26 +0200)
- 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
defenser.h
mainwindow.cpp
mainwindow.ui
map.cpp
map.h
player.cpp
player.h
tile.cpp
tile.h
types.h

index 3446703a4438b235f927e4794431c7f7d5aba2bb..c8ada29c705c16dd7bdf6628f4cd15b41c84a5b4 100644 (file)
@@ -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
index 27109876ee25d19faa89f93c3d6432ec61c160af..471835c2a13c980ff16b0d7fbe1d2bf57569aac6 100644 (file)
@@ -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; }
 
index 2a609b7947901430389727f1fb35ab536ba6c258..b5d90a94673c8346a05b0cc198f190e99b4036ab 100644 (file)
@@ -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
index 3aa12cc573a25b0c51ca451ac9a489fc105d211a..18fadfcc67d6cfb7acffb46aebf7fd93eef91b3a 100644 (file)
        <widget class="QLabel" name="turretInfoLabel">
         <property name="geometry">
          <rect>
-          <x>0</x>
+          <x>10</x>
           <y>20</y>
-          <width>261</width>
-          <height>71</height>
+          <width>241</width>
+          <height>81</height>
          </rect>
         </property>
         <property name="text">
-         <string>TextLabel</string>
+         <string>Cliquez sur une tourelle pour avoir des informations, l'améliorer ou la revendre.</string>
+        </property>
+        <property name="textFormat">
+         <enum>Qt::AutoText</enum>
+        </property>
+        <property name="scaledContents">
+         <bool>false</bool>
         </property>
         <property name="alignment">
-         <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
+         <set>Qt::AlignJustify|Qt::AlignTop</set>
+        </property>
+        <property name="wordWrap">
+         <bool>true</bool>
         </property>
        </widget>
        <widget class="QPushButton" name="upgradeTurretButton">
         <property name="geometry">
          <rect>
           <x>20</x>
-          <y>90</y>
+          <y>100</y>
           <width>98</width>
           <height>27</height>
          </rect>
         <property name="geometry">
          <rect>
           <x>140</x>
-          <y>90</y>
+          <y>100</y>
           <width>98</width>
           <height>27</height>
          </rect>
diff --git a/map.cpp b/map.cpp
index fc58e4d3828a2c0a2dcb483c03aa2a0669e7117a..b2ee92179c6adab1f99fc7a0ff2ec536b0964a66 100644 (file)
--- 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 6738ec7b74c37be119983b9cc86a868ec6b5fd1b..908504a504e0897ab7f8c2fd465256d0f4b90467 100644 (file)
--- 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<Enemy*> _enemies; // OPTIMISATION DES RECHERCHES D'ENNEMI
+    QList<Defenser*> _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<Enemy*> 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
index e3a1873be284fcf0d4f39c8ed41ea4d99c9ef20f..ad36ec6439f8dc9dd5ca2d32002f2d75cf9144dd 100644 (file)
@@ -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);}
 
index 209b33ef3a4417f855fa9d601496af6fc8e47afd..4aeb0a7fddbe86334904f1fdefb4ab1c6c880b52 100644 (file)
--- 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
index f3a265800a19d7c9fe54c6b8df46a7331f7a0f83..7d336e25847f1abc17493fdffa469df21c8b0eca 100644 (file)
--- 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 42d209b3b23bd81d36ac05cc8d928f5f51b8fc60..2ed3d2d9e49401c8a8dad2d1b62ebdc282e44a99 100644 (file)
--- 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 de8b39db0fb87a8270bd5cea0d89e62f4cb33c88..8b1142f7664f666ba3cb6a15981773257ae7f105 100644 (file)
--- 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