From: cboissie Date: Fri, 20 May 2011 23:20:00 +0000 (+0200) Subject: "Turret can open fire (no time control, so it's like machine guns)" X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=aa71c8031c5c322ae70de3b2ab40ee3a7a863f39;p=Bug_Attack.git "Turret can open fire (no time control, so it's like machine guns)" --- diff --git a/Bug_Attack.pro b/Bug_Attack.pro index 9bd01b7..3c128a8 100644 --- a/Bug_Attack.pro +++ b/Bug_Attack.pro @@ -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 diff --git a/cafard.cpp b/cafard.cpp index 04dd7a1..9b49f1a 100644 --- a/cafard.cpp +++ b/cafard.cpp @@ -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; } diff --git a/cafard.h b/cafard.h index 3e98922..f95743c 100644 --- 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); diff --git a/defenser.h b/defenser.h index e40434b..1ec96c3 100644 --- a/defenser.h +++ b/defenser.h @@ -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: diff --git a/enemyfactory.cpp b/enemyfactory.cpp index d887b7a..6498f28 100644 --- a/enemyfactory.cpp +++ b/enemyfactory.cpp @@ -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); } } diff --git a/enemyfactory.h b/enemyfactory.h index dd63f5c..da9a73c 100644 --- a/enemyfactory.h +++ b/enemyfactory.h @@ -50,7 +50,7 @@ class EnemyFactory : public QObject Map* _currentMap; QTimer _popTimer; - void produceEnemy(WaveElement* enemy); + void productEnemy(WaveElement* enemy); public: diff --git a/entity.cpp b/entity.cpp index abdbfc5..c367df2 100644 --- a/entity.cpp +++ b/entity.cpp @@ -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) { diff --git a/entity.h b/entity.h index 4485e60..b4ab584 100644 --- 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 _animPixmap; - int _x; - int _y; int _animState; // Frame courante de l'animation TYPE::ENTITY _type; diff --git a/mainwindow.cpp b/mainwindow.cpp index 3180b23..aa54f09 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -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); diff --git a/mainwindow.h b/mainwindow.h index 9eca272..78e86e9 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -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 f46de87..32ae3b8 100644 --- 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()); diff --git a/monster.h b/monster.h index ba5145e..00474e5 100644 --- 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 index 0000000..e1fab55 --- /dev/null +++ b/projectile.cpp @@ -0,0 +1,47 @@ +#include +#include +#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 index 0000000..70156a6 --- /dev/null +++ b/projectile.h @@ -0,0 +1,29 @@ +#ifndef PROJECTILE_H +#define PROJECTILE_H + +#include +#include +#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 diff --git a/tile.cpp b/tile.cpp index d495ead..fee0940 100644 --- 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 aa32129..f4f555f 100644 --- 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; }; diff --git a/watergun.cpp b/watergun.cpp index c3145cf..4ea4c57 100644 --- a/watergun.cpp +++ b/watergun.cpp @@ -1,12 +1,13 @@ #include +#include #include +#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 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 index 7abf855..6e20b34 100644 --- a/watergun.h +++ b/watergun.h @@ -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);