]> khamidou — GitHub mirrors - Bug_Attack.git/commitdiff
-Turrets shoot with a specific rate
authorcboissie <clemzbox@gmail.com>
Sat, 21 May 2011 22:00:19 +0000 (00:00 +0200)
committercboissie <clemzbox@gmail.com>
Sat, 21 May 2011 22:00:19 +0000 (00:00 +0200)
-Bullets can collide with enemies
-Enemies can die (because of a bullet shot)
-Enemies position fixed
-Turret target position fixed

19 files changed:
Bug_Attack.pro
cafard.h [deleted file]
defenser.cpp
defenser.h
enemy.cpp [moved from cafard.cpp with 67% similarity]
enemy.h [new file with mode: 0644]
enemyfactory.cpp
enemyfactory.h
entity.cpp
map.h
media/maps/waves.txt
monster.cpp [deleted file]
monster.h [deleted file]
projectile.cpp
projectile.h
tile.cpp
tile.h
watergun.cpp [deleted file]
watergun.h [deleted file]

index 3c128a8c7871ce864d1d7c79fe4962fe78cdc6f9..896c03bcb240ab1588b16b1374651c9650df970c 100644 (file)
@@ -15,10 +15,8 @@ SOURCES += main.cpp\
     tile.cpp \
     map.cpp \
     entity.cpp \
-    monster.cpp \
-    cafard.cpp \
+    enemy.cpp \
     defenser.cpp \
-    watergun.cpp \
     enemyfactory.cpp \
     projectile.cpp
 
@@ -26,11 +24,9 @@ HEADERS  += mainwindow.h \
     tile.h \
     map.h \
     entity.h \
-    monster.h \
-    cafard.h \
+    enemy.h \
     types.h \
     defenser.h \
-    watergun.h \
     enemyfactory.h \
     projectile.h
 
diff --git a/cafard.h b/cafard.h
deleted file mode 100644 (file)
index f95743c..0000000
--- a/cafard.h
+++ /dev/null
@@ -1,22 +0,0 @@
-#ifndef CAFARD_H
-#define CAFARD_H
-
-#include "entity.h"
-#include "monster.h"
-#include "map.h"
-
-class Map;
-
-class Cafard : public Monster
-{
-public:
-    Cafard(Map* map,int posx,int posy, float size);
-    ~Cafard(void);
-
-protected:
-    void advance(int phase);
-
-    Map* _map;
-};
-
-#endif // CAFARD_H
index 6b98369c2d9c2fe0129168571604980b21d02c52..6db87b4ec6262e6cfc47753941599fc2fed08e3c 100644 (file)
@@ -1,4 +1,139 @@
+#include <iostream>
+#include <typeinfo>
+#include <math.h>
 #include "defenser.h"
+#include "projectile.h"
+
+/**
+* DEFENSER
+**/
 
 Defenser::Defenser(int posx,int posy,int level,TYPE::ENTITY target)
     :Entity(posx,posy),_level(level),_target(target) {}
+
+
+
+/**
+* PISTOLET A EAU
+**/
+
+
+WaterGun::WaterGun(int posx,int posy,int level,TYPE::ENTITY target,Map* map)
+    : Defenser(posx,posy,level,target),_map(map)
+{
+    this->updateStats();
+
+    QObject::connect(&_shootTimer,SIGNAL(timeout()),this,SLOT(shootTarget()));
+}
+
+int WaterGun::getCost(void) const{
+
+    switch(_level) {
+        case 1: return 8;
+        case 2: return 20;
+        default: return 45;
+    }
+}
+
+bool WaterGun::isShooting(void) const { return _isShooting;}
+
+
+void WaterGun::updateStats(void) {
+
+    _range = (2 + _level/2)*50;
+    _rate = 4 - _level/2;
+    _power = 5 * _level * sqrt(_level); // level ^ 1.5
+
+}
+
+QRectF WaterGun::boundingRect() const {
+    return QRectF(-32,-32,_range,_range);
+}
+
+void WaterGun::setTarget(float targetX,float targetY) {
+    // Sauvegarde la position de la cible courante
+    _targetX = targetX;
+    _targetY = targetY;
+}
+
+void WaterGun::paint(QPainter *painter, const QStyleOptionGraphicsItem *,QWidget*) {
+
+    painter->setBrush(QBrush(Qt::red));
+    // Dessine la tourelle
+    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);
+
+}
+
+void WaterGun::advance(int phase) {
+
+    if(!phase)
+        return;
+
+    // Détection des ennemis aux alentours
+    QList<QGraphicsItem*> entities = _map->items();
+
+    QList<QGraphicsItem*>::iterator i;
+
+    // Recherche des ennemis de la map
+    for(i = entities.begin() ; i != entities.end() ; ++i) {
+        if(dynamic_cast<Enemy*>(*i) != 0) {
+
+            float targetX = (*i)->x();
+            float targetY = (*i)->y();
+
+            if(sqrt(pow(fabs(this->x() - targetX),2)
+                    +pow(fabs(this->y() - targetY),2)
+                )  <= _range)
+            {
+                // Si le défenseur ne tirait pas encore
+                if(!_isShooting) {
+                    this->setIsShooting(true); // Modification via la méthode pour les effets visuels
+                }
+                // Actualise la position de la cible
+                this->setTarget(targetX+dynamic_cast<Enemy*>(*i)->getSize()*16,targetY+dynamic_cast<Enemy*>(*i)->getSize()*16);
+
+                return; // On vise la première cible rencontrée
+
+            } // end distanceTest
+        } // end isMonster
+    } //eof
+
+    // Si aucun monstre n'a été rencontré
+    this->setIsShooting(false);  // Modification via la méthode pour les effets visuels
+
+} // 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
+    Projectile* shot = new Projectile(this->x()+14,this->y()+14,_targetX,_targetY,5,_power);
+    _map->addItem(shot);
+
+}
+
index 1ec96c3ac3f5b2a8f1416d15e0caddef5b70d1f0..de9513823a7c2ee5382b6fc871c75d9703d90613 100644 (file)
@@ -1,20 +1,62 @@
 #ifndef DEFENSER_H
 #define DEFENSER_H
 
+#include <QTimer>
+#include "map.h"
 #include "entity.h"
 
-class Defenser : public Entity
+class Map;
+
+/**
+* DEFENSER
+**/
+
+class Defenser : public Entity,public QObject
 {
 public:
     Defenser(int posx,int posy,int level,TYPE::ENTITY target);
     virtual int getCost(void) const = 0;
 
 protected:
+    QTimer _shootTimer;
     int _level;
     TYPE::ENTITY _target;
     int _rate;
     int _range;
     int _power;
+    float _targetX;
+    float _targetY;
+    bool _isShooting;
+};
+
+/**
+* PISTOLET A EAU
+**/
+
+
+class WaterGun : public Defenser
+{
+
+    Q_OBJECT
+
+public:
+    WaterGun(int posx,int posy,int level,TYPE::ENTITY target,Map* map);
+    int getCost(void) const;
+    bool isShooting(void) const;
+    void paint(QPainter *painter, const QStyleOptionGraphicsItem *,QWidget*);
+    void setTarget(float targetX,float targetY);
+    void setIsShooting(bool state);
+    QRectF boundingRect() const;
+
+
+protected:
+    void advance(int phase);
+    void updateStats(void);
+    Map* _map;
+
+public slots:
+    void shootTarget(void);
+
 };
 
 #endif // DEFENSER_H
similarity index 67%
rename from cafard.cpp
rename to enemy.cpp
index 9b49f1a003d56be7c5d447ffaea48e0a75c9f052..a808e8d29c8c77b80bc05f893929883dc39d6f07 100644 (file)
+++ b/enemy.cpp
@@ -1,11 +1,40 @@
-#include "cafard.h"
 #include <iostream>
-using namespace TYPE;
+#include "enemy.h"
 
-Cafard::Cafard(Map* map, int posx,int posy, float size):Monster(posx,posy,size),_map(map)
+/**
+* ENEMY
+**/
+
+Enemy::Enemy(int posx,int posy, float size):Entity(posx,posy),_size(size){
+    // Positionne l'ennemi bien au centre du chemin
+    this->setPos(this->x() - (_size-1)*16,this->y() - (_size-1)*16);
+}
+
+
+int Enemy::getHP(void) const { return _hp; }
+int Enemy::getResistance(void) const { return _resistance; }
+float Enemy::getSize(void) const { return _size; }
+void Enemy::hurt(int damages){
+    // Inflige 'damages' dégats à l'ennemi
+    _hp-=damages;
+    // Si ses HP sont tombés à zéro, on le détruit
+    if(_hp <= 0)
+        this->scene()->removeItem(this);
+
+    std::cout << "ahah il me reste : " << _hp << " HP" << std::endl;
+
+}
+
+
+/**
+* CAFARD
+**/
+
+
+Cafard::Cafard(Map* map, int posx,int posy, float size):Enemy(posx,posy,size),_map(map)
 {
     // Données du cafard
-    _type = T_RAMPANT;
+    _type = TYPE::T_RAMPANT;
     _hp = 10*size*size;
     _resistance = 5*size*size;
     _speed = 2;
@@ -22,6 +51,7 @@ Cafard::Cafard(Map* map, int posx,int posy, float size):Monster(posx,posy,size),
     this->setPixmap(*_animPixmap.first());
 }
 
+
 void Cafard::advance(int phase) {
 
     // Si 'phase' vaut 0, rien ne se passe
@@ -34,8 +64,8 @@ void Cafard::advance(int phase) {
     this->increaseAnimationStep();
 
     // Déplace l'ennemi selon sa vitesse et la direction induite par le terrain
-    Tile& currentTile = _map->getTileAt((this->x()+16)/32,(this->y()+16)/32);
-    QVector2D dirVect = currentTile.getDirection();
+    Tile& currentTile = _map->getTileAt((this->x() + _size*16)/32,(this->y() + _size*16)/32);
+    QPointF dirVect = currentTile.getDirection();
 
     float new_x = this->x() + _speed*dirVect.x();
     float new_y = this->y() + _speed*dirVect.y();
diff --git a/enemy.h b/enemy.h
new file mode 100644 (file)
index 0000000..1a00fd7
--- /dev/null
+++ b/enemy.h
@@ -0,0 +1,50 @@
+#ifndef MONSTER_H
+#define MONSTER_H
+
+#include "map.h"
+#include "entity.h"
+#include "projectile.h"
+
+class Map;
+
+
+/**
+* ENEMY
+**/
+
+class Enemy : public Entity {
+
+public:
+    Enemy(int posx, int posy, float size);
+    int getResistance(void) const;
+    int getHP(void) const;
+    float getSize(void) const;
+    void hurt(int damages);
+
+protected:
+    float _size;
+    int _hp;
+    int _resistance;
+    float _speed;
+};
+
+
+/**
+* CAFARD
+**/
+
+class Cafard : public Enemy
+{
+public:
+    Cafard(Map* map,int posx,int posy, float size);
+    ~Cafard(void);
+
+protected:
+    void advance(int phase);
+
+    Map* _map;
+};
+
+
+#endif // MONSTER_H
+
index 6498f28ce6d1d3e6b5e704a484b44519efe2b58b..923975c21b76495074e133b1ea1043c5af7e002c 100644 (file)
@@ -91,7 +91,7 @@ EnemyFactory::EnemyFactory(Map* map):_currentMap(map)
 
 void EnemyFactory::productEnemy(WaveElement* enemy){
 
-    Monster* newEnemy;
+    Enemy* newEnemy;
     QPoint start = _currentMap->getStart();
 
     // Crée l'ennemi correspondant à la description du WaveElement
@@ -150,7 +150,7 @@ void EnemyFactory::produceWave(void){
     // Produit le contenu de cette vague
     WaveElement* nextEnemy = _mapWaves.first()->popNextWaveElement();
     if(nextEnemy != NULL) { // sécurité (TODO facultatif ?)
-        _popTimer.setInterval(50*nextEnemy->getFrequence());
+        _popTimer.setInterval(100*nextEnemy->getFrequence());
         this->productEnemy(nextEnemy);
     }
 }
index da9a73c58805f63aa1fa177fe22d21e1ed2c3a50..c137a80ff44ed8a091283c2b5c7a7c163a117d7a 100644 (file)
@@ -7,7 +7,7 @@
 
 #include "map.h"
 #include "types.h"
-#include "cafard.h"
+#include "enemy.h"
 
 class Map;
 
index c367df276296f33152aacabf8e95eb5d569da436..7396713938c0b939986a7cba53b59ac671f1cb81 100644 (file)
@@ -1,7 +1,7 @@
 #include "entity.h"
 
 Entity::Entity(float posX, float posY) {
-    this->setPos(posX,posY);
+   this->setPos(posX,posY);
 }
 
 
diff --git a/map.h b/map.h
index b65345754c9fa8a5637d5984d041a16cb46551de..9900b73c89989a39bdf6ef4982f997efd8fabe07 100644 (file)
--- a/map.h
+++ b/map.h
@@ -10,7 +10,7 @@
 #include "enemyfactory.h"
 #include "tile.h"
 #include "entity.h"
-#include "watergun.h"
+#include "defenser.h"
 
 class EnemyFactory;
 
index 05a105e5218a6621edfcee4f6d6f1dafc4054d86..aec7aa305763930827a2930a121991a13f1d6901 100644 (file)
@@ -1,7 +1,7 @@
-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
+2x fourmis;fourmi:2:2:20\r
+15x cafards;cafard:1:15:5\r
+15x guepes;guepe:1:15:5\r
+15x moustiques;moustique:1:15:5\r
+15x fourmis, 1x GIANT guepes;fourmi:1:15:25;guepe:1.7:1:5\r
+7x GIANT cafards;cafard:2:7:5\r
+7x cafards, 3x moustiques, 10x GIANT fourmis;cafard:1:7:25;moustique:1.1:3:30;fourmi:3:10:5\r
diff --git a/monster.cpp b/monster.cpp
deleted file mode 100644 (file)
index 20bd4d7..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-#include "monster.h"
-
-Monster::Monster(int posx,int posy, float size):Entity(posx,posy),_size(size){ }
-
-
-int Monster::getHP(void) const { return _hp; }
-int Monster::getResistance(void) const { return _resistance; }
-
diff --git a/monster.h b/monster.h
deleted file mode 100644 (file)
index 00474e5..0000000
--- a/monster.h
+++ /dev/null
@@ -1,25 +0,0 @@
-#ifndef MONSTER_H
-#define MONSTER_H
-
-#include "entity.h"
-#include "projectile.h"
-
-
-class Monster : public Entity {
-
-public:
-    Monster(int posx, int posy, float size);
-    int getResistance(void) const;
-    int getHP(void) const;
-
-protected:
-    float _size;
-    int _hp;
-    int _resistance;
-    float _speed;
-};
-
-
-
-#endif // MONSTER_H
-
index e1fab55e386f43114a237a357fbb40f6ca77d1c5..b609e8d31ade4b29aa36922252469119be1cd064 100644 (file)
@@ -16,7 +16,7 @@ Projectile::Projectile(float posX, float posY, float targetX, float targetY, flo
 }
 
 QRectF Projectile::boundingRect() const {
-    return QRectF(-5,-5,5,5);
+    return QRectF(0,0,5,5);
 }
 
 void Projectile::paint(QPainter *painter, const QStyleOptionGraphicsItem *,QWidget*) {
@@ -31,6 +31,27 @@ void Projectile::paint(QPainter *painter, const QStyleOptionGraphicsItem *,QWidg
 void Projectile::advance(int phase) {
     if(!phase)
         return;
+
+    // Collision avec ennemi
+    // . Recherche des ennemis de la map
+    QList<QGraphicsItem*> entities = this->scene()->items();
+    QList<QGraphicsItem*>::iterator i;
+
+    for(i = entities.begin() ; i != entities.end() ; ++i) {
+        if(dynamic_cast<Enemy*>(*i) != 0) {
+
+            if(this->collidesWithItem(*i,Qt::IntersectsItemBoundingRect)){
+                // Inflige des dommages au monstre
+                dynamic_cast<Enemy*>(*i)->hurt(_damages);
+                // On retire le projectile de la scène
+                this->scene()->removeItem(this);
+                return;
+            }
+        }
+    }
+
+
+
     // Déplacement du projectile selon le vecteur de déplacement et la vitesse
     float newx = this->x() + _speed*_movementVect.x();
     float newy = this->y() + _speed*_movementVect.y();
index 70156a65c429fbcc381470398c27540fccdc7ef0..bfb160e0384b5202267a1cc9a12fea9045d34274 100644 (file)
@@ -1,7 +1,6 @@
 #ifndef PROJECTILE_H
 #define PROJECTILE_H
 
-#include <QVector2D>
 #include <QPainter>
 #include "entity.h"
 
@@ -13,7 +12,7 @@ class Projectile : public Entity
    int _damages;
    float _targetX;
    float _targetY;
-   QVector2D _movementVect;
+   QPointF _movementVect;
 
    virtual void advance(int phase);
 
index fee0940e18f1fdeccd0a3fb08f243343df6c0bf5..f3a265800a19d7c9fe54c6b8df46a7331f7a0f83 100644 (file)
--- a/tile.cpp
+++ b/tile.cpp
@@ -47,7 +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; }
+QPointF Tile::getDirection(void) const { return _dir; }
 
 bool Tile::turretAllowed(void) const {
 
diff --git a/tile.h b/tile.h
index f4f555f7c08ac34cec1f098325e8134e3b052142..42d209b3b23bd81d36ac05cc8d928f5f51b8fc60 100644 (file)
--- a/tile.h
+++ b/tile.h
@@ -2,7 +2,6 @@
 #define TILE_H
 
 #include <QGraphicsPixmapItem>
-#include <QVector2D>
 #include "types.h"
 
 // Masque binaire pour déterminer l'orientation d'un tile
@@ -18,7 +17,7 @@ public:
     int getTileNumber(void) const;
     bool turretAllowed(void) const;
     void setHasTurret(bool);
-    QVector2D getDirection(void) const;
+    QPointF getDirection(void) const;
 
 
     QString TexName(int n);
@@ -28,7 +27,7 @@ private:
     int _x, _y;
     int _tileNumber;
     bool _hasTurret;
-    QVector2D _dir;
+    QPointF _dir;
 };
 
 #endif // TILE_H
diff --git a/watergun.cpp b/watergun.cpp
deleted file mode 100644 (file)
index 4ea4c57..0000000
+++ /dev/null
@@ -1,84 +0,0 @@
-#include <iostream>
-#include <typeinfo>
-#include <math.h>
-#include "projectile.h"
-#include "watergun.h"
-
-WaterGun::WaterGun(int posx,int posy,int level,TYPE::ENTITY target,Map* map)
-    : Defenser(posx,posy,level,target),_map(map)
-{
-    this->updateStats();
-}
-
-int WaterGun::getCost(void) const{
-
-    switch(_level) {
-        case 1: return 8;
-        case 2: return 20;
-        default: return 45;
-    }
-}
-
-void WaterGun::updateStats(void) {
-
-    _range = (2 + _level/2)*50;
-    _rate = 4 - _level/2;
-    _power = 5 * _level * sqrt(_level); // level ^ 1.5
-
-}
-
-QRectF WaterGun::boundingRect() const {
-    return QRectF(-32,-32,_range,_range);
-}
-
-void WaterGun::paint(QPainter *painter, const QStyleOptionGraphicsItem *,QWidget*) {
-
-    painter->setBrush(QBrush(Qt::red));
-    // Dessine la tourelle
-    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);
-
-}
-
-void WaterGun::advance(int phase) {
-
-    if(!phase)
-        return;
-
-    this->setOpacity(1.0f);
-
-    // Détection des ennemis aux alentours
-    QList<QGraphicsItem*> entities = _map->items();
-
-    QList<QGraphicsItem*>::iterator i;
-
-    // Recherche des ennemis de la map
-    for(i = entities.begin() ; i != entities.end() ; ++i) {
-        if(dynamic_cast<Monster*>(*i) != 0) {
-
-            float targetX = (*i)->x();
-            float targetY = (*i)->y();
-
-            if(sqrt(pow(fabs(this->x() - targetX),2)
-                    +pow(fabs(this->y() - targetY),2)
-                )  <= _range)
-            {
-                this->setOpacity(0.5f); // TEST = TIR ACTIVÉ
-                this->shootAt(targetX,targetY);
-                return; // On vise la première cible rencontrée
-            } // end distanceTest
-        } // end isMonster
-    } //eof
-
-} // eom
-
-void WaterGun::shootAt(float targetX,float targetY) {
-
-    // Créé un nouveau projectile au niveau de la tourelle
-    Projectile* shot = new Projectile(this->x(),this->y(),targetX,targetY,5,_power);
-    _map->addItem(shot);
-
-}
diff --git a/watergun.h b/watergun.h
deleted file mode 100644 (file)
index 6e20b34..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-#ifndef WATERGUN_H
-#define WATERGUN_H
-
-#include <QPainter>
-#include "defenser.h"
-#include "map.h"
-
-class Map;
-
-class WaterGun : public Defenser
-{
-public:
-    WaterGun(int posx,int posy,int level,TYPE::ENTITY target,Map* map);
-    int getCost(void) const;
-    void paint(QPainter *painter, const QStyleOptionGraphicsItem *,QWidget*);
-    QRectF boundingRect() const;
-    void shootAt(float targetX,float targetY);
-protected:
-    void advance(int phase);
-    void updateStats(void);
-
-    Map* _map;
-};
-
-#endif // WATERGUN_H