tile.cpp \
map.cpp \
entity.cpp \
- monster.cpp \
- cafard.cpp \
+ enemy.cpp \
defenser.cpp \
- watergun.cpp \
enemyfactory.cpp \
projectile.cpp
tile.h \
map.h \
entity.h \
- monster.h \
- cafard.h \
+ enemy.h \
types.h \
defenser.h \
- watergun.h \
enemyfactory.h \
projectile.h
+++ /dev/null
-#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
+#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);
+
+}
+
#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
-#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;
this->setPixmap(*_animPixmap.first());
}
+
void Cafard::advance(int phase) {
// Si 'phase' vaut 0, rien ne se passe
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();
--- /dev/null
+#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
+
void EnemyFactory::productEnemy(WaveElement* enemy){
- Monster* newEnemy;
+ Enemy* newEnemy;
QPoint start = _currentMap->getStart();
// Crée l'ennemi correspondant à la description du WaveElement
// 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);
}
}
#include "map.h"
#include "types.h"
-#include "cafard.h"
+#include "enemy.h"
class Map;
#include "entity.h"
Entity::Entity(float posX, float posY) {
- this->setPos(posX,posY);
+ this->setPos(posX,posY);
}
#include "enemyfactory.h"
#include "tile.h"
#include "entity.h"
-#include "watergun.h"
+#include "defenser.h"
class EnemyFactory;
-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
+++ /dev/null
-#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; }
-
+++ /dev/null
-#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
-
}
QRectF Projectile::boundingRect() const {
- return QRectF(-5,-5,5,5);
+ return QRectF(0,0,5,5);
}
void Projectile::paint(QPainter *painter, const QStyleOptionGraphicsItem *,QWidget*) {
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();
#ifndef PROJECTILE_H
#define PROJECTILE_H
-#include <QVector2D>
#include <QPainter>
#include "entity.h"
int _damages;
float _targetX;
float _targetY;
- QVector2D _movementVect;
+ QPointF _movementVect;
virtual void advance(int phase);
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 {
#define TILE_H
#include <QGraphicsPixmapItem>
-#include <QVector2D>
#include "types.h"
// Masque binaire pour déterminer l'orientation d'un tile
int getTileNumber(void) const;
bool turretAllowed(void) const;
void setHasTurret(bool);
- QVector2D getDirection(void) const;
+ QPointF getDirection(void) const;
QString TexName(int n);
int _x, _y;
int _tileNumber;
bool _hasTurret;
- QVector2D _dir;
+ QPointF _dir;
};
#endif // TILE_H
+++ /dev/null
-#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);
-
-}
+++ /dev/null
-#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