]> khamidou — GitHub mirrors - Bug_Attack.git/commitdiff
"Turret can open fire (no time control, so it's like machine guns)"
authorcboissie <clemzbox@gmail.com>
Fri, 20 May 2011 23:20:00 +0000 (01:20 +0200)
committercboissie <clemzbox@gmail.com>
Fri, 20 May 2011 23:20:00 +0000 (01:20 +0200)
18 files changed:
Bug_Attack.pro
cafard.cpp
cafard.h
defenser.h
enemyfactory.cpp
enemyfactory.h
entity.cpp
entity.h
mainwindow.cpp
mainwindow.h
map.cpp
monster.h
projectile.cpp [new file with mode: 0644]
projectile.h [new file with mode: 0644]
tile.cpp
tile.h
watergun.cpp
watergun.h

index 9bd01b7b9ba890de0a4d3795af3baf6030b1cc5b..3c128a8c7871ce864d1d7c79fe4962fe78cdc6f9 100644 (file)
@@ -19,7 +19,8 @@ SOURCES += main.cpp\
     cafard.cpp \
     defenser.cpp \
     watergun.cpp \
-    enemyfactory.cpp
+    enemyfactory.cpp \
+    projectile.cpp
 
 HEADERS  += mainwindow.h \
     tile.h \
@@ -30,6 +31,7 @@ HEADERS  += mainwindow.h \
     types.h \
     defenser.h \
     watergun.h \
-    enemyfactory.h
+    enemyfactory.h \
+    projectile.h
 
 FORMS    += mainwindow.ui
index 04dd7a176c7072ecdd8e92513aad4efb04ca3bd7..9b49f1a003d56be7c5d447ffaea48e0a75c9f052 100644 (file)
@@ -20,9 +20,6 @@ Cafard::Cafard(Map* map, int posx,int posy, float size):Monster(posx,posy,size),
 
     // Image et taille
     this->setPixmap(*_animPixmap.first());
-
-    // Position
-    this->setPos(_x,_y);
 }
 
 void Cafard::advance(int phase) {
@@ -37,13 +34,13 @@ 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((_x+16)/32,(_y+16)/32);
+    Tile& currentTile = _map->getTileAt((this->x()+16)/32,(this->y()+16)/32);
     QVector2D dirVect = currentTile.getDirection();
 
-    _x += _speed*dirVect.x();
-    _y += _speed*dirVect.y();
+    float new_x = this->x() + _speed*dirVect.x();
+    float new_y = this->y() + _speed*dirVect.y();
 
-    this->setPos(_x,_y);
+    this->setPos(new_x,new_y);
 
     // Oriente l'image dans le bon sens
     // (nb : transformation par rapport au centre de l'image)
@@ -66,7 +63,13 @@ void Cafard::advance(int phase) {
         this->setRotation(90);
     else if(dirVect.y() == -1)
         this->setRotation(-90);
+}
 
+// Un cafard se sous divise en deux cafard si sa taille est
+// supérieure ou égale à deux
+Cafard::~Cafard(void) {
 
+    if(_size<2)
+        return;
 
 }
index 3e9892284a967b674ee20b20a70efa34a8abf074..f95743c25c3cd91258214a75047b26eaccc8e1a0 100644 (file)
--- a/cafard.h
+++ b/cafard.h
@@ -11,6 +11,7 @@ class Cafard : public Monster
 {
 public:
     Cafard(Map* map,int posx,int posy, float size);
+    ~Cafard(void);
 
 protected:
     void advance(int phase);
index e40434b382f615db170bf65c850d8bff6a6a5fdc..1ec96c3ac3f5b2a8f1416d15e0caddef5b70d1f0 100644 (file)
@@ -7,7 +7,6 @@ class Defenser : public Entity
 {
 public:
     Defenser(int posx,int posy,int level,TYPE::ENTITY target);
-
     virtual int getCost(void) const = 0;
 
 protected:
index d887b7a782fe1a8983db8acc349dd6b708d08212..6498f28ce6d1d3e6b5e704a484b44519efe2b58b 100644 (file)
@@ -89,7 +89,7 @@ EnemyFactory::EnemyFactory(Map* map):_currentMap(map)
     connect(&_popTimer,SIGNAL(timeout()),this,SLOT(produceWave(void)));
 }
 
-void EnemyFactory::produceEnemy(WaveElement* enemy){
+void EnemyFactory::productEnemy(WaveElement* enemy){
 
     Monster* newEnemy;
     QPoint start = _currentMap->getStart();
@@ -151,7 +151,7 @@ void EnemyFactory::produceWave(void){
     WaveElement* nextEnemy = _mapWaves.first()->popNextWaveElement();
     if(nextEnemy != NULL) { // sécurité (TODO facultatif ?)
         _popTimer.setInterval(50*nextEnemy->getFrequence());
-        this->produceEnemy(nextEnemy);
+        this->productEnemy(nextEnemy);
     }
 }
 
index dd63f5cc50ff454a8f5eda50f8aa287adff84109..da9a73c58805f63aa1fa177fe22d21e1ed2c3a50 100644 (file)
@@ -50,7 +50,7 @@ class EnemyFactory : public QObject
     Map* _currentMap;
     QTimer _popTimer;
 
-    void produceEnemy(WaveElement* enemy);
+    void productEnemy(WaveElement* enemy);
 
 public:
 
index abdbfc52e534e38284116769a884c76702cdc88f..c367df276296f33152aacabf8e95eb5d569da436 100644 (file)
@@ -1,10 +1,9 @@
 #include "entity.h"
 
-Entity::Entity(int posX, int posY): _x(posX),_y(posY) {
+Entity::Entity(float posX, float posY) {
+    this->setPos(posX,posY);
 }
 
-int Entity::getX(void) const { return _x; }
-int Entity::getY(void) const { return _y; }
 
 void Entity::increaseAnimationStep(void) {
 
index 4485e60a1a3a6b4fc8b87f514497c7b11cacdc8e..b4ab5848555847884db2e75b1469a14494cebd2a 100644 (file)
--- a/entity.h
+++ b/entity.h
@@ -14,10 +14,8 @@ class Entity : public QGraphicsPixmapItem // , public QObject
 
 public:
 
-    Entity(int posX, int posY);
+    Entity(float posX, float posY);
     void increaseAnimationStep(void);
-    int getX() const;
-    int getY() const;
 
 
 
@@ -26,8 +24,6 @@ protected:
     virtual void advance(int phase) = 0;
 
     QList<QPixmap*> _animPixmap;
-    int _x;
-    int _y;
     int _animState; // Frame courante de l'animation
     TYPE::ENTITY _type;
 
index 3180b23f7b39f4e39414a4a3ab7bb05ddcdd0780..aa54f09c9be752de1296edfbf22e36d2e06a17f1 100644 (file)
@@ -10,8 +10,7 @@ MainWindow::MainWindow(QWidget *parent) :
 
     qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
 
-    /// Test : try to draw some tiles into the QGraphicView
-
+    // Création de la scène (map)
     _sceneMap = new Map(parent);
     _view = ui->graphicsView;
     _view->setScene(_sceneMap);
index 9eca2720f27b8c47eb65a2064f1f110e66cb5e84..78e86e9daabf5de02d2f07add94f984905ee4339 100644 (file)
@@ -27,7 +27,7 @@ public:
 private:
     Ui::MainWindow *ui;
 
-    static const int FPS = 50;
+    static const int FPS = 30;
 
     QGraphicsView *_view;
     QTimer timer;
diff --git a/map.cpp b/map.cpp
index f46de87aac2bd42d985e31487a18210ca91b2f12..32ae3b8e905068bd0185c4a702eeda1c81fdaeb1 100644 (file)
--- a/map.cpp
+++ b/map.cpp
@@ -6,7 +6,6 @@
 
 Map::Map(QObject *parent):QGraphicsScene(parent)
 {
-
     // Chargement du contenu de la map
     std::ifstream fichier("media/maps/map.txt");
     int i, j;
@@ -49,10 +48,12 @@ void Map::mousePressEvent(QGraphicsSceneMouseEvent *event) {
        this->getTileAt(mx/32,my/32).turretAllowed())
     {
         std::cout << "clic at : " << mx << "," <<  my << std::endl;
+        this->getTileAt(mx/32,my/32).setHasTurret(true);
         _entities.push_front(new WaterGun(((int)(mx/32))*32,
                                           ((int)(my/32))*32,
                                           1,
-                                          (TYPE::ENTITY)(TYPE::T_RAMPANT | TYPE::T_VOLANT))
+                                          (TYPE::ENTITY)(TYPE::T_RAMPANT | TYPE::T_VOLANT),
+                                          this)
                              );
         this->addItem(_entities.first());
 
index ba5145e593f63e134d18cdc525e3ce3f2ea81bd3..00474e55b9ed30d2e32e9a9d401344bf64e24ea8 100644 (file)
--- a/monster.h
+++ b/monster.h
@@ -2,6 +2,7 @@
 #define MONSTER_H
 
 #include "entity.h"
+#include "projectile.h"
 
 
 class Monster : public Entity {
@@ -15,7 +16,7 @@ protected:
     float _size;
     int _hp;
     int _resistance;
-    int _speed;
+    float _speed;
 };
 
 
diff --git a/projectile.cpp b/projectile.cpp
new file mode 100644 (file)
index 0000000..e1fab55
--- /dev/null
@@ -0,0 +1,47 @@
+#include <iostream>
+#include <math.h>
+#include "map.h"
+#include "projectile.h"
+
+Projectile::Projectile(float posX, float posY, float targetX, float targetY, float speed, int damages)
+    : Entity(posX,posY),_speed(speed),_damages(damages),_targetX(targetX),_targetY(targetY)
+{
+    // Calcul du vecteur de déplacement du point (posX,posY) vers (targetX,targetY)
+    // .. Nb : on calcule ici le vecteur normalisé pour appliquer ensuite la vitesse propre à l'objet
+    float vx = _targetX - this->x();
+    float vy = _targetY - this->y();
+
+    _movementVect.setX(vx/sqrt(vx*vx + vy*vy));
+    _movementVect.setY(vy/sqrt(vx*vx + vy*vy));
+}
+
+QRectF Projectile::boundingRect() const {
+    return QRectF(-5,-5,5,5);
+}
+
+void Projectile::paint(QPainter *painter, const QStyleOptionGraphicsItem *,QWidget*) {
+
+    painter->setBrush(QBrush(Qt::cyan));
+    // Dessine la tourelle
+    painter->drawRect(0,0,5,5);
+
+
+}
+
+void Projectile::advance(int phase) {
+    if(!phase)
+        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();
+
+    // std::cout << "I'm still alive mothafucka" << std::endl;
+
+    // Si le projectile est en dehors de l'écran, on le supprime
+    if(newx < 0 || newx > 512 || newy < 0 || newy > 512){
+        this->scene()->removeItem(this); // TODO VERIFIER Semble faire un appel à delete() de l'objet
+        return;
+    }
+    this->setPos(newx,newy);
+
+}
diff --git a/projectile.h b/projectile.h
new file mode 100644 (file)
index 0000000..70156a6
--- /dev/null
@@ -0,0 +1,29 @@
+#ifndef PROJECTILE_H
+#define PROJECTILE_H
+
+#include <QVector2D>
+#include <QPainter>
+#include "entity.h"
+
+
+class Projectile : public Entity
+{
+
+   float _speed;
+   int _damages;
+   float _targetX;
+   float _targetY;
+   QVector2D _movementVect;
+
+   virtual void advance(int phase);
+
+public:
+   QRectF boundingRect() const;
+   virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *,QWidget*);
+
+   Projectile(float posX,float posY,float targetX,float targetY,float speed,int damages);
+
+
+};
+
+#endif // PROJECTILE_H
index d495ead42e786cccb3922c6c11f3f3b06056acac..fee0940e18f1fdeccd0a3fb08f243343df6c0bf5 100644 (file)
--- a/tile.cpp
+++ b/tile.cpp
@@ -30,16 +30,15 @@ Tile::Tile(int tileNumber, int x, int y):_tileNumber(tileNumber)
     this->setPos(x*32,y*32);
 
 
-    /* QString filePath("./media/textures/");
-    filePath+= TexName(tileNumber); // TEST
-    filePath+= QString(".jpg");
-    cout << filePath.toStdString() << endl;*/
+    // Chargement image correspondante
     _pixmap = new QPixmap("media/textures/plain/" + TexName(tileNumber) + ".png");
     if (_pixmap->isNull())
     cout << "pixmap null" << endl;
 
+    // Données du tile
     _x = x;
     _y = y;
+    _hasTurret = false;
     *_pixmap = _pixmap->scaled(32,32,Qt::IgnoreAspectRatio);
     this->setPixmap(*_pixmap);
 
@@ -52,7 +51,7 @@ QVector2D Tile::getDirection(void) const { return _dir; }
 
 bool Tile::turretAllowed(void) const {
 
-    if(_tileNumber == 0)
+    if(_tileNumber == 0 && !_hasTurret)
         return true;
     return false;
 }
@@ -96,4 +95,6 @@ QString Tile::TexName(int n){
     return "grass";
 }
 
+void Tile::setHasTurret(bool){ _hasTurret = true; }
+
 Tile::~Tile(){}
diff --git a/tile.h b/tile.h
index aa321296c2fc06aeb27edd8d7a1c045f3bd1fefc..f4f555f7c08ac34cec1f098325e8134e3b052142 100644 (file)
--- a/tile.h
+++ b/tile.h
@@ -17,6 +17,7 @@ public:
     int getY() const;    
     int getTileNumber(void) const;
     bool turretAllowed(void) const;
+    void setHasTurret(bool);
     QVector2D getDirection(void) const;
 
 
@@ -26,6 +27,7 @@ private:
     QPixmap *_pixmap;
     int _x, _y;
     int _tileNumber;
+    bool _hasTurret;
     QVector2D _dir;
 };
 
index c3145cfe3c15b6fb6d6c928d5f806c0911e59a15..4ea4c57b5fee07a9170e7060034be6421f57e0f9 100644 (file)
@@ -1,12 +1,13 @@
 #include <iostream>
+#include <typeinfo>
 #include <math.h>
+#include "projectile.h"
 #include "watergun.h"
 
-WaterGun::WaterGun(int posx,int posy,int level,TYPE::ENTITY target)
-    : Defenser(posx,posy,level,target)
+WaterGun::WaterGun(int posx,int posy,int level,TYPE::ENTITY target,Map* map)
+    : Defenser(posx,posy,level,target),_map(map)
 {
-    updateStats();
-    this->setPos(_x,_y);
+    this->updateStats();
 }
 
 int WaterGun::getCost(void) const{
@@ -27,7 +28,7 @@ void WaterGun::updateStats(void) {
 }
 
 QRectF WaterGun::boundingRect() const {
-    return QRectF(-100,-100,200,200);
+    return QRectF(-32,-32,_range,_range);
 }
 
 void WaterGun::paint(QPainter *painter, const QStyleOptionGraphicsItem *,QWidget*) {
@@ -47,6 +48,37 @@ 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);
 
 }
index 7abf855ded2a8239f406f2c185e38bd8b352c508..6e20b3408cf266b22199da275756894710a2d80a 100644 (file)
@@ -10,10 +10,11 @@ class Map;
 class WaterGun : public Defenser
 {
 public:
-    WaterGun(int posx,int posy,int level,TYPE::ENTITY target);
+    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);