From: cboissie Date: Fri, 20 May 2011 15:44:50 +0000 (+0200) Subject: Ennemy factory is working, you can now launch a wave from the right gui. (a short... X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=be021025cffc2cb1dcc188b20052a8ffbb168691;p=Bug_Attack.git Ennemy factory is working, you can now launch a wave from the right gui. (a short desc of the next incoming wave is also displayed on the right). --- diff --git a/Bug_Attack.pro b/Bug_Attack.pro index afff092..9bd01b7 100644 --- a/Bug_Attack.pro +++ b/Bug_Attack.pro @@ -18,7 +18,8 @@ SOURCES += main.cpp\ monster.cpp \ cafard.cpp \ defenser.cpp \ - watergun.cpp + watergun.cpp \ + enemyfactory.cpp HEADERS += mainwindow.h \ tile.h \ @@ -28,6 +29,7 @@ HEADERS += mainwindow.h \ cafard.h \ types.h \ defenser.h \ - watergun.h + watergun.h \ + enemyfactory.h FORMS += mainwindow.ui diff --git a/cafard.cpp b/cafard.cpp index 2d35fc7..04dd7a1 100644 --- a/cafard.cpp +++ b/cafard.cpp @@ -2,7 +2,7 @@ #include using namespace TYPE; -Cafard::Cafard(Map* map, int posx,int posy, int size):Monster(posx,posy,size),_map(map) +Cafard::Cafard(Map* map, int posx,int posy, float size):Monster(posx,posy,size),_map(map) { // Données du cafard _type = T_RAMPANT; @@ -48,6 +48,25 @@ void Cafard::advance(int phase) { // Oriente l'image dans le bon sens // (nb : transformation par rapport au centre de l'image) this->setTransformOriginPoint(this->boundingRect().center().x(),this->boundingRect().center().y()); - this->setRotation(dirVect.y()*90); + + // Rotation + if(dirVect.x() == 1 && dirVect.y() == 1) + this->setRotation(45); + else if(dirVect.x() == 1 && dirVect.y() == -1) + this->setRotation(-45); + else if(dirVect.x() == -1 && dirVect.y() == 1) + this->setRotation(135); + else if(dirVect.x() == -1 && dirVect.y() == -1) + this->setRotation(-135); + else if(dirVect.x() == 1) + this->setRotation(0); + else if(dirVect.x() == -1) + this->setRotation(180); + else if(dirVect.y() == 1) + this->setRotation(90); + else if(dirVect.y() == -1) + this->setRotation(-90); + + } diff --git a/cafard.h b/cafard.h index cddfa18..3e98922 100644 --- a/cafard.h +++ b/cafard.h @@ -10,7 +10,7 @@ class Map; class Cafard : public Monster { public: - Cafard(Map* map,int posx,int posy, int size); + Cafard(Map* map,int posx,int posy, float size); protected: void advance(int phase); diff --git a/enemyfactory.cpp b/enemyfactory.cpp new file mode 100644 index 0000000..d887b7a --- /dev/null +++ b/enemyfactory.cpp @@ -0,0 +1,160 @@ +#include "enemyfactory.h" +#include + +WaveElement::WaveElement(QString data) { + + // Extraction des données + QList subElementDesc = data.split(':'); + + // Création des wave elements + // . TYPE + if(subElementDesc.first() == "cafard") + _type = TYPE::CAFARD; + else + _type = TYPE::CAFARD; + subElementDesc.pop_front(); + // . TAILLE + _size = subElementDesc.first().toFloat(); + subElementDesc.pop_front(); + // . NOMBRE + _number = subElementDesc.first().toInt(); + subElementDesc.pop_front(); + // . FREQUENCE + _frequence = subElementDesc.first().toInt(); +} + +TYPE::ENEMY WaveElement::getType(void) const{return _type;} +float WaveElement::getSize(void) const{return _size;} +int WaveElement::getNumber(void) const{return _number;} +int WaveElement::getFrequence(void) const{return _frequence;} +void WaveElement::decreaseNumber(void) { if(_number>0) --_number; } + +Wave::Wave(QString data) { + + // Extraction des données + QList waveDesc = data.split(";"); + + // Titre de la vague courante + _desc = waveDesc.first(); + waveDesc.pop_front(); + + // Création des wave elements + QList::iterator i; + for(i = waveDesc.begin() ; i != waveDesc.end() ; ++i) + _waveElements.push_back(new WaveElement(*i)); + + +} + +WaveElement* Wave::popNextWaveElement(void){ + + // Les éléments de la vague courantes ont été crées (erreur) + if(_waveElements.empty()) + return NULL; + + // On récupére l'élement courant + WaveElement* result = _waveElements.first(); + + // Si le nombre d'élément restant est nul, on passe au suivant + if(result->getNumber() == 1){ + _waveElements.pop_front(); + } + + // On retire l'élément courant qui va être créé + result->decreaseNumber(); + + return result; +} + +bool Wave::isFinished(void) const { return(_waveElements.empty()); } +QString Wave::getDesc(void) const { return _desc; } + +EnemyFactory::EnemyFactory(Map* map):_currentMap(map) +{ + + // Lit le fichier wave correspondant à la map courante + QFile file("./media/maps/waves.txt"); + if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) + return; + // Extrait chaque vague d'ennemis + while(!file.atEnd()){ + QString line = file.readLine(); + _mapWaves.push_back(new Wave(line)); + + } + // Ferme le fichier + file.close(); + + + connect(&_popTimer,SIGNAL(timeout()),this,SLOT(produceWave(void))); +} + +void EnemyFactory::produceEnemy(WaveElement* enemy){ + + Monster* newEnemy; + QPoint start = _currentMap->getStart(); + + // Crée l'ennemi correspondant à la description du WaveElement + switch(enemy->getType()) { + + case TYPE::CAFARD: + newEnemy = new Cafard(_currentMap,start.x(),start.y(),enemy->getSize()); + break; + + default: + newEnemy = new Cafard(_currentMap,start.x(),start.y(),enemy->getSize()); + } + + // Ajoute le nouvel ennemi à la map courante + _currentMap->addItem(newEnemy); +} + +void EnemyFactory::getNextWaveDesc(void){ + if(!_mapWaves.empty()) + emit changeWaveDescLabel(QString(_mapWaves.first()->getDesc())); +} + +void EnemyFactory::launchWaves(void){ + // Désactive le bouton de lancement de vague ennemie + emit setLaunchWaveButtonEnabled(false); + // Lance la production + _popTimer.start(1); +} + +void EnemyFactory::produceWave(void){ + + // Vérifie s'il reste des vagues à produire + if(_mapWaves.empty()){ + _popTimer.stop(); + return; + } + + // Récupère la vague courante + if(_mapWaves.first()->isFinished()){ + + // Charge la vague suivante + _mapWaves.pop_front(); + _popTimer.stop(); + + // Ré-active le bouton de lancement de vague ennemie + // ... s'il reste encore des vagues à lancer + if(!_mapWaves.empty()){ + emit setLaunchWaveButtonEnabled(true); + //.. et affiche le contenu de la prochaine vague + this->getNextWaveDesc(); + } + + return; + } + + // Produit le contenu de cette vague + WaveElement* nextEnemy = _mapWaves.first()->popNextWaveElement(); + if(nextEnemy != NULL) { // sécurité (TODO facultatif ?) + _popTimer.setInterval(50*nextEnemy->getFrequence()); + this->produceEnemy(nextEnemy); + } +} + + + + diff --git a/enemyfactory.h b/enemyfactory.h new file mode 100644 index 0000000..dd63f5c --- /dev/null +++ b/enemyfactory.h @@ -0,0 +1,72 @@ +#ifndef ENEMYFACTORY_H +#define ENEMYFACTORY_H + +#include +#include +#include + +#include "map.h" +#include "types.h" +#include "cafard.h" + +class Map; + +class WaveElement +{ + TYPE::ENEMY _type; + float _size; + int _number; + int _frequence; + +public: + + WaveElement(QString data); + TYPE::ENEMY getType(void) const; + float getSize(void) const; + int getNumber(void) const; + int getFrequence(void) const; + void decreaseNumber(void); +}; + + +class Wave +{ + QList< WaveElement* > _waveElements; + QString _desc; + +public: + + Wave(QString data); + WaveElement* popNextWaveElement(void); + bool isFinished(void) const; + QString getDesc(void) const; +}; + +class EnemyFactory : public QObject +{ + Q_OBJECT + + QList _mapWaves; + Map* _currentMap; + QTimer _popTimer; + + void produceEnemy(WaveElement* enemy); + +public: + + EnemyFactory(Map* map); + +public slots: + void getNextWaveDesc(void); + void launchWaves(void); + void produceWave(void); + +signals: + void changeWaveDescLabel(QString text); + void setLaunchWaveButtonEnabled(bool state); + +}; + + + +#endif // ENEMYFACTORY_H diff --git a/mainwindow.cpp b/mainwindow.cpp index 1ee5d9f..3180b23 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -23,8 +23,19 @@ MainWindow::MainWindow(QWidget *parent) : // Lancement du timer (boucle principale du jeu) QObject::connect(&timer, SIGNAL(timeout()), _sceneMap, SLOT(advance())); - timer.start(1000 / 20); // 10 FPS - + timer.start(1000 / FPS); + + /** + * Interface graphique + */ + // 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 + QObject::connect(_sceneMap->getWaveGenerator(),SIGNAL(changeWaveDescLabel(QString)),ui->waveDescLabel,SLOT(setText(QString))); + _sceneMap->getWaveGenerator()->getNextWaveDesc(); + // . Rend le bouton inactif le temps que la vague se produise + QObject::connect(_sceneMap->getWaveGenerator(),SIGNAL(setLaunchWaveButtonEnabled(bool)),ui->launchWaveButton,SLOT(setEnabled(bool))); } diff --git a/mainwindow.h b/mainwindow.h index 5da4844..9eca272 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -11,6 +11,7 @@ #include "map.h" + namespace Ui { class MainWindow; } @@ -26,6 +27,7 @@ public: private: Ui::MainWindow *ui; + static const int FPS = 50; QGraphicsView *_view; QTimer timer; diff --git a/mainwindow.ui b/mainwindow.ui index 1c681f6..5ed5526 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -52,7 +52,7 @@ - + 0 @@ -72,14 +72,17 @@ - TextLabel + Clique sur le bouton pour commencer. + + + Qt::AlignCenter - + - PushButton + Lancer la vague ! @@ -199,6 +202,14 @@ Qt::ScrollBarAlwaysOff + + + 0.000000000000000 + 0.000000000000000 + 510.000000000000000 + 510.000000000000000 + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop diff --git a/map.cpp b/map.cpp index 8a5a5da..f46de87 100644 --- a/map.cpp +++ b/map.cpp @@ -6,22 +6,8 @@ Map::Map(QObject *parent):QGraphicsScene(parent) { - /* // Loads map file - QFile mapFile("media/maps/map.txt"); - if(!mapFile.open(QIODevice::ReadOnly | QIODevice::Text)) - return; - // Reads map data and creates corresponding map tiles - int y = 0; - while(!mapFile.atEnd()){ - QByteArray line = mapFile.readLine(); - QList tileNumber = line.split(' '); - for(int x = 0; x < tileNumber.size(); ++x) - _mapTiles[x][y] = new Tile(tileNumber[x].toInt()); - y++; - } - // Close file - mapFile.close();*/ + // Chargement du contenu de la map std::ifstream fichier("media/maps/map.txt"); int i, j; int num; @@ -33,15 +19,23 @@ Map::Map(QObject *parent):QGraphicsScene(parent) { fichier >> num; _mapTiles[j][i] = new Tile(num, j, i); + // Position de départ ? + if(_mapTiles[j][i]->getTileNumber() == 18 ) + _startPos = QPoint(32*j,32*i); + // Ajoute le tile à la map courante this->addItem(_mapTiles[j][i]); } } } 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); } @@ -65,4 +59,6 @@ void Map::mousePressEvent(QGraphicsSceneMouseEvent *event) { } } +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 314fa49..b653457 100644 --- a/map.h +++ b/map.h @@ -7,11 +7,12 @@ #include #include +#include "enemyfactory.h" #include "tile.h" #include "entity.h" -#include "cafard.h" #include "watergun.h" +class EnemyFactory; class Map : public QGraphicsScene { @@ -19,15 +20,20 @@ class Map : public QGraphicsScene private: // QList* _ennemyList; + EnemyFactory* _waveGenerator; Tile* _mapTiles[16][16]; QList _entities; QGraphicsScene* _scene; + QPoint _startPos; + public: Map(QObject* parent); void mousePressEvent(QGraphicsSceneMouseEvent *event); + QPoint getStart(void) const; Tile& getTileAt(int x, int y) const; + EnemyFactory* getWaveGenerator(void) const; }; #endif // MAP_H diff --git a/media/maps/waves.txt b/media/maps/waves.txt index 579c8f4..05a105e 100644 --- a/media/maps/waves.txt +++ b/media/maps/waves.txt @@ -1,7 +1,7 @@ -Quinze petites fourmis;fourmi:1:15:20 -Des cafards...;cafard:1:15:20 -Invasion de guêpes;guepe:1:15:20 -Ca gratte...;moustique:1:15:20 -Les fourmis et une grande guêpe;fourmi:1:15:25;guepe:1.7:1:25 -Multiplication;cafard:2:7:30 -Sept cafards, trois moustiques et dix fourmis géantes;cafard:1:7:25;moustique:1.1:3:30;fourmi:3:10:40 \ No newline at end of file +2x fourmis;fourmi:1:2:20 +15x cafards;cafard:1:15:20 +15x guepes;guepe:1:15:20 +15x moustiques;moustique:1:15:20 +15x fourmis, 1x GIANT guepes;fourmi:1:15:25;guepe:1.7:1:25 +7x GIANT cafards;cafard:2:7:30 +7x cafards, 3x moustiques, 10x GIANT fourmis;cafard:1:7:25;moustique:1.1:3:30;fourmi:3:10:40 diff --git a/monster.cpp b/monster.cpp index 9315f33..20bd4d7 100644 --- a/monster.cpp +++ b/monster.cpp @@ -1,6 +1,6 @@ #include "monster.h" -Monster::Monster(int posx,int posy, int size):Entity(posx,posy),_size(size){ } +Monster::Monster(int posx,int posy, float size):Entity(posx,posy),_size(size){ } int Monster::getHP(void) const { return _hp; } diff --git a/monster.h b/monster.h index e81f6e5..ba5145e 100644 --- a/monster.h +++ b/monster.h @@ -7,12 +7,12 @@ class Monster : public Entity { public: - Monster(int posx, int posy, int size); + Monster(int posx, int posy, float size); int getResistance(void) const; int getHP(void) const; protected: - int _size; + float _size; int _hp; int _resistance; int _speed; diff --git a/tile.cpp b/tile.cpp index 3f05a5d..d495ead 100644 --- a/tile.cpp +++ b/tile.cpp @@ -47,6 +47,7 @@ Tile::Tile(int tileNumber, int x, int y):_tileNumber(tileNumber) int Tile::getX() const { return _x; } int Tile::getY() const { return _y; } +int Tile::getTileNumber(void) const { return _tileNumber; } QVector2D Tile::getDirection(void) const { return _dir; } bool Tile::turretAllowed(void) const { diff --git a/tile.h b/tile.h index ad62b61..aa32129 100644 --- a/tile.h +++ b/tile.h @@ -14,7 +14,8 @@ public: Tile(int tileNumber, int x, int y); ~Tile(void); int getX() const; - int getY() const; + int getY() const; + int getTileNumber(void) const; bool turretAllowed(void) const; QVector2D getDirection(void) const; diff --git a/types.h b/types.h index 3a0a232..938a254 100644 --- a/types.h +++ b/types.h @@ -6,8 +6,9 @@ namespace TYPE { // Utilisation du bit 2^7 pour coder une information supplémentaire // concernant les diagonales (cf doc) -typedef enum DIRECTION { NORTH = 1 , SOUTH = 2 , EAST = 4 , WEST = 8, UP = 128 } DIRECTION; -typedef enum ENTITY { T_RAMPANT = 1, T_VOLANT = 2 } ENTITY; +typedef enum { NORTH = 1 , SOUTH = 2 , EAST = 4 , WEST = 8, UP = 128 } DIRECTION; +typedef enum { T_RAMPANT = 1, T_VOLANT = 2 } ENTITY; +typedef enum { CAFARD } ENEMY; }