From: cboissie Date: Sat, 21 May 2011 22:00:19 +0000 (+0200) Subject: -Turrets shoot with a specific rate X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=0db93d143b54cb564a3fae25a72ffb39ecb5f5f5;p=Bug_Attack.git -Turrets shoot with a specific rate -Bullets can collide with enemies -Enemies can die (because of a bullet shot) -Enemies position fixed -Turret target position fixed --- diff --git a/Bug_Attack.pro b/Bug_Attack.pro index 3c128a8..896c03b 100644 --- a/Bug_Attack.pro +++ b/Bug_Attack.pro @@ -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 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 diff --git a/defenser.cpp b/defenser.cpp index 6b98369..6db87b4 100644 --- a/defenser.cpp +++ b/defenser.cpp @@ -1,4 +1,139 @@ +#include +#include +#include #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 entities = _map->items(); + + QList::iterator i; + + // Recherche des ennemis de la map + for(i = entities.begin() ; i != entities.end() ; ++i) { + if(dynamic_cast(*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(*i)->getSize()*16,targetY+dynamic_cast(*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); + +} + diff --git a/defenser.h b/defenser.h index 1ec96c3..de95138 100644 --- a/defenser.h +++ b/defenser.h @@ -1,20 +1,62 @@ #ifndef DEFENSER_H #define DEFENSER_H +#include +#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 diff --git a/cafard.cpp b/enemy.cpp similarity index 67% rename from cafard.cpp rename to enemy.cpp index 9b49f1a..a808e8d 100644 --- a/cafard.cpp +++ b/enemy.cpp @@ -1,11 +1,40 @@ -#include "cafard.h" #include -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 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 + diff --git a/enemyfactory.cpp b/enemyfactory.cpp index 6498f28..923975c 100644 --- a/enemyfactory.cpp +++ b/enemyfactory.cpp @@ -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); } } diff --git a/enemyfactory.h b/enemyfactory.h index da9a73c..c137a80 100644 --- a/enemyfactory.h +++ b/enemyfactory.h @@ -7,7 +7,7 @@ #include "map.h" #include "types.h" -#include "cafard.h" +#include "enemy.h" class Map; diff --git a/entity.cpp b/entity.cpp index c367df2..7396713 100644 --- a/entity.cpp +++ b/entity.cpp @@ -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 b653457..9900b73 100644 --- 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; diff --git a/media/maps/waves.txt b/media/maps/waves.txt index 05a105e..aec7aa3 100644 --- a/media/maps/waves.txt +++ b/media/maps/waves.txt @@ -1,7 +1,7 @@ -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 +2x fourmis;fourmi:2:2:20 +15x cafards;cafard:1:15:5 +15x guepes;guepe:1:15:5 +15x moustiques;moustique:1:15:5 +15x fourmis, 1x GIANT guepes;fourmi:1:15:25;guepe:1.7:1:5 +7x GIANT cafards;cafard:2:7:5 +7x cafards, 3x moustiques, 10x GIANT fourmis;cafard:1:7:25;moustique:1.1:3:30;fourmi:3:10:5 diff --git a/monster.cpp b/monster.cpp deleted file mode 100644 index 20bd4d7..0000000 --- a/monster.cpp +++ /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 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 - diff --git a/projectile.cpp b/projectile.cpp index e1fab55..b609e8d 100644 --- a/projectile.cpp +++ b/projectile.cpp @@ -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 entities = this->scene()->items(); + QList::iterator i; + + for(i = entities.begin() ; i != entities.end() ; ++i) { + if(dynamic_cast(*i) != 0) { + + if(this->collidesWithItem(*i,Qt::IntersectsItemBoundingRect)){ + // Inflige des dommages au monstre + dynamic_cast(*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(); diff --git a/projectile.h b/projectile.h index 70156a6..bfb160e 100644 --- a/projectile.h +++ b/projectile.h @@ -1,7 +1,6 @@ #ifndef PROJECTILE_H #define PROJECTILE_H -#include #include #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); diff --git a/tile.cpp b/tile.cpp index fee0940..f3a2658 100644 --- 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 f4f555f..42d209b 100644 --- a/tile.h +++ b/tile.h @@ -2,7 +2,6 @@ #define TILE_H #include -#include #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 index 4ea4c57..0000000 --- a/watergun.cpp +++ /dev/null @@ -1,84 +0,0 @@ -#include -#include -#include -#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 entities = _map->items(); - - QList::iterator i; - - // Recherche des ennemis de la map - for(i = entities.begin() ; i != entities.end() ; ++i) { - if(dynamic_cast(*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 index 6e20b34..0000000 --- a/watergun.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef WATERGUN_H -#define WATERGUN_H - -#include -#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