]> khamidou — GitHub mirrors - Bug_Attack.git/commitdiff
Ennemy factory is working, you can now launch a wave from the right gui. (a short...
authorcboissie <clemzbox@gmail.com>
Fri, 20 May 2011 15:44:50 +0000 (17:44 +0200)
committercboissie <clemzbox@gmail.com>
Fri, 20 May 2011 15:44:50 +0000 (17:44 +0200)
16 files changed:
Bug_Attack.pro
cafard.cpp
cafard.h
enemyfactory.cpp [new file with mode: 0644]
enemyfactory.h [new file with mode: 0644]
mainwindow.cpp
mainwindow.h
mainwindow.ui
map.cpp
map.h
media/maps/waves.txt
monster.cpp
monster.h
tile.cpp
tile.h
types.h

index afff092b8b219c727109462b91ba4cb8702f1ef8..9bd01b7b9ba890de0a4d3795af3baf6030b1cc5b 100644 (file)
@@ -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
index 2d35fc7c6b876fc3984830e7aa767249412b8b94..04dd7a176c7072ecdd8e92513aad4efb04ca3bd7 100644 (file)
@@ -2,7 +2,7 @@
 #include <iostream>
 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);
+
+
 
 }
index cddfa186f352e425a1361660dec60c1e73259505..3e9892284a967b674ee20b20a70efa34a8abf074 100644 (file)
--- 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 (file)
index 0000000..d887b7a
--- /dev/null
@@ -0,0 +1,160 @@
+#include "enemyfactory.h"
+#include <iostream>
+
+WaveElement::WaveElement(QString data) {
+
+    // Extraction des données
+    QList<QString> 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<QString> waveDesc = data.split(";");
+
+    // Titre de la vague courante
+    _desc = waveDesc.first();
+    waveDesc.pop_front();
+
+    // Création des wave elements
+    QList<QString>::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 (file)
index 0000000..dd63f5c
--- /dev/null
@@ -0,0 +1,72 @@
+#ifndef ENEMYFACTORY_H
+#define ENEMYFACTORY_H
+
+#include <QList>
+#include <QTimer>
+#include <QString>
+
+#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<Wave*> _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
index 1ee5d9f9200ce8a6c02cde37cbcadeb9675604fd..3180b23f7b39f4e39414a4a3ab7bb05ddcdd0780 100644 (file)
@@ -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)));
 
 }
 
index 5da48441e8a4f2d186e9b4afff295f25f9ed9f66..9eca2720f27b8c47eb65a2064f1f110e66cb5e84 100644 (file)
@@ -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;
index 1c681f6a856951da3b3940026671c3a51ff6260c..5ed5526188dbaf927c6e3ea56ba606bf2feaa416 100644 (file)
@@ -52,7 +52,7 @@
     </property>
     <layout class="QVBoxLayout" name="verticalLayout">
      <item>
-      <widget class="QLabel" name="label">
+      <widget class="QLabel" name="waveDescLabel">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
          <horstretch>0</horstretch>
         </size>
        </property>
        <property name="text">
-        <string>TextLabel</string>
+        <string>Clique sur le bouton pour commencer.</string>
+       </property>
+       <property name="alignment">
+        <set>Qt::AlignCenter</set>
        </property>
       </widget>
      </item>
      <item>
-      <widget class="QPushButton" name="pushButton">
+      <widget class="QPushButton" name="launchWaveButton">
        <property name="text">
-        <string>PushButton</string>
+        <string>Lancer la vague !</string>
        </property>
       </widget>
      </item>
     <property name="horizontalScrollBarPolicy">
      <enum>Qt::ScrollBarAlwaysOff</enum>
     </property>
+    <property name="sceneRect">
+     <rectf>
+      <x>0.000000000000000</x>
+      <y>0.000000000000000</y>
+      <width>510.000000000000000</width>
+      <height>510.000000000000000</height>
+     </rectf>
+    </property>
     <property name="alignment">
      <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
     </property>
diff --git a/map.cpp b/map.cpp
index 8a5a5da746690c0bd3736dc544a0dd1d4e614368..f46de87aac2bd42d985e31487a18210ca91b2f12 100644 (file)
--- 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<QByteArray> 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 314fa49d74bab82ff0a817d913bdf6d95e6aafb5..b65345754c9fa8a5637d5984d041a16cb46551de 100644 (file)
--- a/map.h
+++ b/map.h
@@ -7,11 +7,12 @@
 #include <QGraphicsSceneMouseEvent>
 #include <QMouseEvent>
 
+#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<int>* _ennemyList;
+    EnemyFactory* _waveGenerator;
     Tile* _mapTiles[16][16];
     QList<Entity*> _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
index 579c8f4de996d176fd94db17ae40beec02656092..05a105e5218a6621edfcee4f6d6f1dafc4054d86 100644 (file)
@@ -1,7 +1,7 @@
-Quinze petites fourmis;fourmi:1:15:20\r
-Des cafards...;cafard:1:15:20\r
-Invasion de guêpes;guepe:1:15:20\r
-Ca gratte...;moustique:1:15:20\r
-Les fourmis et une grande guêpe;fourmi:1:15:25;guepe:1.7:1:25\r
-Multiplication;cafard:2:7:30\r
-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\r
+15x cafards;cafard:1:15:20\r
+15x guepes;guepe:1:15:20\r
+15x moustiques;moustique:1:15:20\r
+15x fourmis, 1x GIANT guepes;fourmi:1:15:25;guepe:1.7:1:25\r
+7x GIANT cafards;cafard:2:7:30\r
+7x cafards, 3x moustiques, 10x GIANT fourmis;cafard:1:7:25;moustique:1.1:3:30;fourmi:3:10:40\r
index 9315f3303cf2496ee8ed6a243bb7b4efc754532b..20bd4d71313d133f400a8bf78dfa3faf4ebf55ef 100644 (file)
@@ -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; }
index e81f6e583bffd0a7bfbd8b1cb6bc9aaaff2db50e..ba5145e593f63e134d18cdc525e3ce3f2ea81bd3 100644 (file)
--- 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;
index 3f05a5d8d9abd4e2d83139cf06c9b04dc993288d..d495ead42e786cccb3922c6c11f3f3b06056acac 100644 (file)
--- 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 ad62b617546ba8f2d8b0b6da456a52b5cd3ec206..aa321296c2fc06aeb27edd8d7a1c045f3bd1fefc 100644 (file)
--- 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 3a0a232bd53998b9d2861fd51c5514c1ab05e4c8..938a254007ce971e1ead12ef4e98143e1ee43f5a 100644 (file)
--- 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;
 
 }