From 4f26fdc720268b136f8e29f898fd3e886b442517 Mon Sep 17 00:00:00 2001 From: cboissie Date: Wed, 25 May 2011 02:02:55 +0200 Subject: [PATCH] New gui element and beginning of the turret upgrade module. (also found a bug with right click : turrets placed with right click don't search any target in the current enemy list) --- defenser.cpp | 8 +++---- defenser.h | 9 ++++--- mainwindow.cpp | 17 +++++++++++--- mainwindow.h | 2 +- mainwindow.ui | 42 +++++++++++++++++++++++---------- map.cpp | 64 +++++++++++++++++++++++++++++++++++--------------- map.h | 6 ++--- player.cpp | 14 ++++++++++- player.h | 6 +++++ types.h | 1 + 10 files changed, 120 insertions(+), 49 deletions(-) diff --git a/defenser.cpp b/defenser.cpp index 4941cb5..3446703 100644 --- a/defenser.cpp +++ b/defenser.cpp @@ -8,18 +8,16 @@ * DEFENSER **/ -Defenser::Defenser(int posx,int posy,int level,TYPE::ENTITY target) - :Entity(posx,posy),_level(level),_target(target) {} - +Defenser::Defenser(int posx,int posy,int level,TYPE::ENTITY target,Map* map) + :Entity(posx,posy),_level(level),_target(target),_map(map) {} /** * PISTOLET A EAU **/ - WaterGun::WaterGun(int posx,int posy,int level,TYPE::ENTITY target,Map* map) - : Defenser(posx,posy,level,target),_map(map) + : Defenser(posx,posy,level,target,map) { this->updateStats(); diff --git a/defenser.h b/defenser.h index 323894b..2710987 100644 --- a/defenser.h +++ b/defenser.h @@ -13,8 +13,10 @@ class Map; class Defenser : public Entity { + Q_OBJECT + public: - Defenser(int posx,int posy,int level,TYPE::ENTITY target); + Defenser(int posx,int posy,int level,TYPE::ENTITY target,Map* map); virtual int getCost(void) const = 0; protected: @@ -27,6 +29,9 @@ protected: float _targetX; float _targetY; bool _isShooting; + Map* _map; + + }; /** @@ -36,7 +41,6 @@ protected: class WaterGun : public Defenser { - Q_OBJECT public: @@ -53,7 +57,6 @@ public: protected: void advance(int phase); void updateStats(void); - Map* _map; public slots: void shootTarget(void); diff --git a/mainwindow.cpp b/mainwindow.cpp index c483744..2a609b7 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -27,6 +27,7 @@ MainWindow::MainWindow(QWidget *parent) : _view->setCacheMode(QGraphicsView::CacheBackground); _view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); + _view->show(); // Lancement du timer (boucle principale du jeu) @@ -36,7 +37,7 @@ MainWindow::MainWindow(QWidget *parent) : /** * Interface graphique **/ - // Lancement de la prochaine vague + /// Lancement de la prochaine vague // . Lance la production d'ennemi QObject::connect(ui->launchWaveButton, SIGNAL(pressed()),_sceneMap->getWaveGenerator(),SLOT(launchWaves())); // . Change le texte d'ambiance associé à la vague courante @@ -45,7 +46,17 @@ MainWindow::MainWindow(QWidget *parent) : // . Rend le bouton inactif le temps que la vague se produise QObject::connect(_sceneMap->getWaveGenerator(),SIGNAL(setLaunchWaveButtonEnabled(bool)),ui->launchWaveButton,SLOT(setEnabled(bool))); - // Gestions des compteurs + + /// Boutons de choix de tourelle + // . Pistolet à eau + QObject::connect(ui->turretButton_PistoletAEau,SIGNAL(pressed()),_player,SLOT(setTurretChoice1())); + + + // Demande d'informations sur une tourelle + // QObject::connect(_sceneMap,SIGNAL(loadTurretInfos(QString)),ui->turretInfoLabel,SLOT(setText(QString))); + + + /// Gestions des compteurs // Argent ui->creditCounter->display(_player->getMoney()); // C'est du vol QObject::connect(_player,SIGNAL(moneyValueChanged(int)),ui->creditCounter,SLOT(display(int))); @@ -83,7 +94,7 @@ void MainWindow::showMapContextMenu(const QPoint& pos) { { // TODO CHANGER ADD TURRET if(selectedItem == createTurret1) - _sceneMap->addTurretAt(mousePos.x(),mousePos.y()); + _sceneMap->addTurretAt(TYPE::PISTOLET_A_EAU,mousePos.x(),mousePos.y()); } } diff --git a/mainwindow.h b/mainwindow.h index 41e7926..237bca8 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -2,13 +2,13 @@ #define MAINWINDOW_H #include -#include "map.h" #include #include #include #include +#include "types.h" #include "map.h" #include "player.h" diff --git a/mainwindow.ui b/mainwindow.ui index 2e2fb93..3aa12cc 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -117,8 +117,21 @@ - GroupBox + Défenses + + + + 20 + 30 + 98 + 27 + + + + Pistolet à eau + + @@ -127,48 +140,51 @@ true - GroupBox + Informations false - + - 90 - 40 - 67 - 17 + 0 + 20 + 261 + 71 TextLabel + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + - + 20 - 80 + 90 98 27 - PushButton + Améliorer - + 140 - 80 + 90 98 27 - PushButton + Revendre diff --git a/map.cpp b/map.cpp index c7c685e..fc58e4d 100644 --- a/map.cpp +++ b/map.cpp @@ -29,11 +29,6 @@ Map::Map(QWidget *parent,Player* player):QGraphicsScene(parent),_player(player) } fichier.close(); - /* TEST - _entities.push_front(new Cafard(this,32*2,0,1)); - this->addItem(_entities.first()); - */ - // Générateur de vagues _waveGenerator = new EnemyFactory(this); } @@ -48,7 +43,7 @@ void Map::mousePressEvent(QGraphicsSceneMouseEvent *event) { // CLIC GAUCHE : POSE DE TOURELLE if(event->button() == Qt::LeftButton) - this->addTurretAt(mx,my); + this->addTurretAt(_player->getTurretChoice(),mx,my); } @@ -91,28 +86,59 @@ void Map::removeEnemy(Enemy* ptr) { QList Map::getEnemyList(void) const { return _enemies; }; -bool Map::addTurretAt(float mx,float my) { +bool Map::addTurretAt(TYPE::TURRET turretType,float mx,float my) { // Vérifie s'il est possible de poser une tourelle à cet emplacement - // ... et si le joueur a assez d'argent (le cas échant, il est débité aussitôt - if( this->getTileAt(mx/32,my/32).turretAllowed() && - _player->payMoney(WaterGun::getBasicCost())) { + if( this->getTileAt(mx/32,my/32).turretAllowed()) { - this->getTileAt(mx/32,my/32).setHasTurret(true); - WaterGun* test = new WaterGun(((int)(mx/32))*32, - ((int)(my/32))*32, - 1, - (TYPE::ENTITY)(TYPE::T_RAMPANT | TYPE::T_VOLANT), - this); - this->addItem(test); + // On pose une nouvelle tourelle du type souhaité ... + // ... si le joueur a assez d'argent (le cas échant, il est débité aussitôt) + Defenser* newTurret; + bool enoughMoney = false; - return true; // Succès + switch(turretType) { - } + // Pistolet à eau + case TYPE::PISTOLET_A_EAU: + if(_player->payMoney(WaterGun::getBasicCost())) + { + enoughMoney = true; + newTurret = new WaterGun(((int)(mx/32))*32, + ((int)(my/32))*32, + 1, + (TYPE::ENTITY)(TYPE::T_RAMPANT | TYPE::T_VOLANT), + this); + } + break; + + // Type inconnu ~ erreur + default: + break; + + } // eos + + // 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); + + // Gestion des clics sur la tourelle + // QObject::connect(newTurret,SIGNAL(turretClicked(QString)),this,SLOT(turretInfosRequest(QString))); + + // On remet à zéro le choix du joueur + _player->setTurretChoice(TYPE::NONE); + + return true; // Succès + } + + } // End test case libre return false; // Echec } + QPoint Map::getStart(void) const { return _startPos; } Tile& Map::getTileAt(int x, int y) const { return *_mapTiles[x][y]; } EnemyFactory* Map::getWaveGenerator(void) const { return _waveGenerator; } diff --git a/map.h b/map.h index c08c255..6738ec7 100644 --- a/map.h +++ b/map.h @@ -21,7 +21,6 @@ class Map : public QGraphicsScene Q_OBJECT - // QList* _ennemyList; EnemyFactory* _waveGenerator; Tile* _mapTiles[16][16]; QList _enemies; // OPTIMISATION DES RECHERCHES D'ENNEMI @@ -29,13 +28,13 @@ class Map : public QGraphicsScene QPoint _startPos; Player* _player; + void mousePressEvent(QGraphicsSceneMouseEvent *event); public: Map(QWidget* parent,Player* player); - void mousePressEvent(QGraphicsSceneMouseEvent *event); void addEnemy(Enemy* enemy); - bool addTurretAt(float mx,float my); + bool addTurretAt(TYPE::TURRET turretType,float mx,float my); QPoint getStart(void) const; Tile& getTileAt(int x, int y) const; EnemyFactory* getWaveGenerator(void) const; @@ -43,7 +42,6 @@ public: public slots: void removeEnemy(Enemy* ptr); - }; #endif // MAP_H diff --git a/player.cpp b/player.cpp index 18df21c..e3a1873 100644 --- a/player.cpp +++ b/player.cpp @@ -1,4 +1,5 @@ #include "player.h" +#include Player::Player(int money,int lives){ @@ -8,10 +9,14 @@ Player::Player(int money,int lives){ // Intialise le nombre de vies (lives>0) ? _lives = lives : _lives = DEFAULT_LIVES; + // Choix clic + _turretChoice = TYPE::NONE; + } 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) { @@ -34,6 +39,14 @@ void Player::loseLives(int livesLost) { emit livesValueChanged(_lives); } +void Player::setTurretChoice(TYPE::TURRET choice) { + _turretChoice = choice; + std::cout << "Le joueur a choisi " << choice << std::endl; +} +void Player::setTurretChoice1(){this->setTurretChoice(TYPE::PISTOLET_A_EAU);} + + + bool Player::payMoney(int amount) { // Vérifie si le joueur peut payer @@ -45,4 +58,3 @@ bool Player::payMoney(int amount) { else return false; // Paiement impossible } - diff --git a/player.h b/player.h index ac64172..209b33e 100644 --- a/player.h +++ b/player.h @@ -2,6 +2,7 @@ #define PLAYER_H #include +#include "types.h" class Player : public QObject { @@ -9,6 +10,7 @@ class Player : public QObject int _money; int _lives; + TYPE::TURRET _turretChoice; public: @@ -19,11 +21,15 @@ public: bool payMoney(int amount); int getMoney(void) const; int getLives(void) const; + TYPE::TURRET getTurretChoice(void) const; + void setTurretChoice(TYPE::TURRET choice); + public slots: void earnMoney(int amount); void loseLives(int livesLost); + void setTurretChoice1(); signals: void moneyValueChanged(int); diff --git a/types.h b/types.h index 19bbba7..de8b39d 100644 --- a/types.h +++ b/types.h @@ -9,6 +9,7 @@ namespace TYPE { typedef enum { NORTH = 1 , SOUTH = 2 , EAST = 4 , WEST = 8, GOAL = 32, UP = 128 } DIRECTION; typedef enum { T_RAMPANT = 1, T_VOLANT = 2 } ENTITY; typedef enum { CAFARD } ENEMY; +typedef enum { NONE, PISTOLET_A_EAU } TURRET; } -- 2.43.0