cafard.cpp \
defenser.cpp \
watergun.cpp \
- enemyfactory.cpp
+ enemyfactory.cpp \
+ projectile.cpp
HEADERS += mainwindow.h \
tile.h \
types.h \
defenser.h \
watergun.h \
- enemyfactory.h
+ enemyfactory.h \
+ projectile.h
FORMS += mainwindow.ui
// Image et taille
this->setPixmap(*_animPixmap.first());
-
- // Position
- this->setPos(_x,_y);
}
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)
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;
}
{
public:
Cafard(Map* map,int posx,int posy, float size);
+ ~Cafard(void);
protected:
void advance(int phase);
{
public:
Defenser(int posx,int posy,int level,TYPE::ENTITY target);
-
virtual int getCost(void) const = 0;
protected:
connect(&_popTimer,SIGNAL(timeout()),this,SLOT(produceWave(void)));
}
-void EnemyFactory::produceEnemy(WaveElement* enemy){
+void EnemyFactory::productEnemy(WaveElement* enemy){
Monster* newEnemy;
QPoint start = _currentMap->getStart();
WaveElement* nextEnemy = _mapWaves.first()->popNextWaveElement();
if(nextEnemy != NULL) { // sécurité (TODO facultatif ?)
_popTimer.setInterval(50*nextEnemy->getFrequence());
- this->produceEnemy(nextEnemy);
+ this->productEnemy(nextEnemy);
}
}
Map* _currentMap;
QTimer _popTimer;
- void produceEnemy(WaveElement* enemy);
+ void productEnemy(WaveElement* enemy);
public:
#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) {
public:
- Entity(int posX, int posY);
+ Entity(float posX, float posY);
void increaseAnimationStep(void);
- int getX() const;
- int getY() const;
virtual void advance(int phase) = 0;
QList<QPixmap*> _animPixmap;
- int _x;
- int _y;
int _animState; // Frame courante de l'animation
TYPE::ENTITY _type;
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);
private:
Ui::MainWindow *ui;
- static const int FPS = 50;
+ static const int FPS = 30;
QGraphicsView *_view;
QTimer timer;
Map::Map(QObject *parent):QGraphicsScene(parent)
{
-
// Chargement du contenu de la map
std::ifstream fichier("media/maps/map.txt");
int i, j;
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());
#define MONSTER_H
#include "entity.h"
+#include "projectile.h"
class Monster : public Entity {
float _size;
int _hp;
int _resistance;
- int _speed;
+ float _speed;
};
--- /dev/null
+#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);
+
+}
--- /dev/null
+#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
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);
bool Tile::turretAllowed(void) const {
- if(_tileNumber == 0)
+ if(_tileNumber == 0 && !_hasTurret)
return true;
return false;
}
return "grass";
}
+void Tile::setHasTurret(bool){ _hasTurret = true; }
+
Tile::~Tile(){}
int getY() const;
int getTileNumber(void) const;
bool turretAllowed(void) const;
+ void setHasTurret(bool);
QVector2D getDirection(void) const;
QPixmap *_pixmap;
int _x, _y;
int _tileNumber;
+ bool _hasTurret;
QVector2D _dir;
};
#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{
}
QRectF WaterGun::boundingRect() const {
- return QRectF(-100,-100,200,200);
+ return QRectF(-32,-32,_range,_range);
}
void WaterGun::paint(QPainter *painter, const QStyleOptionGraphicsItem *,QWidget*) {
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);
}
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);