From bfbd30f81631d37f76411c67afb835baf2f35419 Mon Sep 17 00:00:00 2001 From: cboissie Date: Tue, 31 May 2011 02:33:46 +0200 Subject: [PATCH] Adding a main menu that allows to : - Replay / Quit - Change difficulty between easy and hard (reload the game) TODO : make hard map and wave : D --- enemyfactory.cpp | 5 +-- enemyfactory.h | 3 +- mainwindow.cpp | 32 ++++++++++++++++++- mainwindow.h | 5 ++- mainwindow.ui | 46 ++++++++++++++++++++++++++++ map.cpp | 5 +-- media/maps/{map.txt => map1.txt} | 0 media/maps/{waves.txt => waves1.txt} | 0 player.cpp | 8 +++-- player.h | 5 ++- types.h | 1 + 11 files changed, 100 insertions(+), 10 deletions(-) rename media/maps/{map.txt => map1.txt} (100%) rename media/maps/{waves.txt => waves1.txt} (100%) diff --git a/enemyfactory.cpp b/enemyfactory.cpp index bc1fc1b..67f7237 100644 --- a/enemyfactory.cpp +++ b/enemyfactory.cpp @@ -74,11 +74,12 @@ WaveElement* Wave::popNextWaveElement(void){ bool Wave::isFinished(void) const { return(_waveElements.empty()); } QString Wave::getDesc(void) const { return _desc; } -EnemyFactory::EnemyFactory(Map* map):_currentMap(map) +EnemyFactory::EnemyFactory(Map* map,Player* player):_currentMap(map),_player(player) { // Lit le fichier wave correspondant à la map courante - QFile file("./media/maps/waves.txt"); + QString filePath = QString("media/maps/waves") + QString::number((int)_player->getDifficulty())+ QString(".txt"); + QFile file(filePath); if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; // Extrait chaque vague d'ennemis diff --git a/enemyfactory.h b/enemyfactory.h index 57fcc71..b37fb38 100644 --- a/enemyfactory.h +++ b/enemyfactory.h @@ -47,13 +47,14 @@ class EnemyFactory : public QObject QList _mapWaves; Map* _currentMap; + Player* _player; float _waveTimerStep; void productEnemy(WaveElement* enemy); public: - EnemyFactory(Map* map); + EnemyFactory(Map* map,Player* player); public slots: void getNextWaveDesc(void); diff --git a/mainwindow.cpp b/mainwindow.cpp index fe80fe2..1e29b2c 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -10,15 +10,26 @@ MainWindow::MainWindow(QWidget *parent) : ui->setupUi(this); // Lance le jeu automatiquement au démarrage de l'application + _gameDifficulty = GAME::EASY; this->launchGame(parent); + + // Menu par défaut + // . Partie en cours + QObject::connect(ui->actionRecommencer,SIGNAL(triggered()),this,SLOT(reloadGame())); + QObject::connect(ui->actionQuitter,SIGNAL(triggered()),this,SLOT(close())); + // . Difficulté + QObject::connect(ui->actionFacile,SIGNAL(triggered()),this,SLOT(setGameModeEasy())); + QObject::connect(ui->actionPro_gamer,SIGNAL(triggered()),this,SLOT(setGameModeHard())); + } void MainWindow::launchGame(QWidget *parent) { + /** * Initialisation des données du joueur **/ - _player = new Player(22,10); // 22 - 10 + _player = new Player(22,10,_gameDifficulty); // 22 - 10 /** @@ -38,6 +49,17 @@ void MainWindow::launchGame(QWidget *parent) { /** * Interface graphique **/ + + /// Menu principal (difficulté actuellement selectionnée) + if(_gameDifficulty == GAME::EASY) { + ui->actionFacile->setDisabled(true); + ui->actionPro_gamer->setDisabled(false); + } + else { + ui->actionFacile->setDisabled(false); + ui->actionPro_gamer->setDisabled(true); + } + /// Lancement de la prochaine vague // . Lance la production d'ennemi ui->launchWaveButton->setDisabled(false); @@ -178,6 +200,14 @@ void MainWindow::reloadGame(void) { // Relance le jeu (nb: les éléments de l'ancienne partie seront automatiquement supprimés par l'appel au new) this->launchGame(this->parentWidget()); +} +void MainWindow::setGameModeEasy(void){ + _gameDifficulty=GAME::EASY; + this->reloadGame(); +} +void MainWindow::setGameModeHard(void){ + _gameDifficulty=GAME::HARD; + this->reloadGame(); } diff --git a/mainwindow.h b/mainwindow.h index cebcfc2..31cced7 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -26,16 +26,19 @@ class MainWindow : public QMainWindow QGraphicsView *_view; Map* _sceneMap; Player* _player; + GAME::DIFFICULTY _gameDifficulty; public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); void launchGame(QWidget *parent); - void reloadGame(void); public slots: void showMapContextMenu(const QPoint& pos); void endGame(QString msg); + void reloadGame(void); + void setGameModeEasy(void); + void setGameModeHard(void); }; diff --git a/mainwindow.ui b/mainwindow.ui index 14df2ac..59d5827 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -321,6 +321,52 @@ + + + + 0 + 0 + 800 + 25 + + + + + Jeu + + + + + + + Difficulté + + + + + + + + + + Recommencer + + + + + Quitter + + + + + Facile + + + + + Pro gamer + + diff --git a/map.cpp b/map.cpp index c7e74ec..643a599 100644 --- a/map.cpp +++ b/map.cpp @@ -8,7 +8,8 @@ Map::Map(QWidget *parent,Player* player):QGraphicsScene(parent),_player(player) { // Chargement du contenu de la map - std::ifstream fichier("media/maps/map.txt"); + QString filePath = QString("media/maps/map") + QString::number((int)_player->getDifficulty())+ QString(".txt"); + std::ifstream fichier(filePath.toAscii()); int i, j; int num; if(fichier) @@ -30,7 +31,7 @@ Map::Map(QWidget *parent,Player* player):QGraphicsScene(parent),_player(player) fichier.close(); // Générateur de vagues - _waveGenerator = new EnemyFactory(this); + _waveGenerator = new EnemyFactory(this,_player); // Lancement du timer (boucle principale du jeu) QObject::connect(&gameTimer, SIGNAL(timeout()), this, SLOT(advance())); gameTimer.start(1000 / GAME::FPS); diff --git a/media/maps/map.txt b/media/maps/map1.txt similarity index 100% rename from media/maps/map.txt rename to media/maps/map1.txt diff --git a/media/maps/waves.txt b/media/maps/waves1.txt similarity index 100% rename from media/maps/waves.txt rename to media/maps/waves1.txt diff --git a/player.cpp b/player.cpp index 9f6d320..b6d7558 100644 --- a/player.cpp +++ b/player.cpp @@ -2,13 +2,14 @@ #include -Player::Player(int money,int lives){ +Player::Player(int money,int lives,GAME::DIFFICULTY level){ // Initialise l'argent (money>0) ? _money = money : _money = DEFAULT_MONEY; // Intialise le nombre de vies (lives>0) ? _lives = lives : _lives = DEFAULT_LIVES; - + // Difficulté du jeu + _gameDifficulty = level; // Choix clic _turretChoice = TYPE::NONE; @@ -16,6 +17,9 @@ Player::Player(int money,int lives){ int Player::getMoney(void) const { return _money; } int Player::getLives(void) const { return _lives; } +GAME::DIFFICULTY Player::getDifficulty(void) const{ return _gameDifficulty; } +void Player::setDifficulty(GAME::DIFFICULTY level) { _gameDifficulty = level; } + TYPE::TURRET Player::getTurretChoice(void) const { return _turretChoice; } void Player::earnMoney(int money) { diff --git a/player.h b/player.h index 084de85..7982371 100644 --- a/player.h +++ b/player.h @@ -11,6 +11,7 @@ class Player : public QObject int _money; int _lives; + GAME::DIFFICULTY _gameDifficulty; TYPE::TURRET _turretChoice; public: @@ -18,12 +19,14 @@ public: static const int DEFAULT_MONEY = 100; static const int DEFAULT_LIVES = 10; - Player(int money = DEFAULT_MONEY,int lives = DEFAULT_LIVES); + Player(int money = DEFAULT_MONEY,int lives = DEFAULT_LIVES,GAME::DIFFICULTY = GAME::EASY); bool payMoney(int amount); int getMoney(void) const; int getLives(void) const; + GAME::DIFFICULTY getDifficulty(void) const; TYPE::TURRET getTurretChoice(void) const; void setTurretChoice(TYPE::TURRET choice); + void setDifficulty(GAME::DIFFICULTY level); public slots: diff --git a/types.h b/types.h index 721eeb1..93109a5 100644 --- a/types.h +++ b/types.h @@ -16,6 +16,7 @@ typedef enum { NONE, PISTOLET_A_EAU, LANCE_PIERRES, PAINTBALL, PETANQUE, MUSICIE namespace GAME { +typedef enum { EASY = 1, HARD = 2 } DIFFICULTY; static const short FPS = 60; } -- 2.43.0