SOURCES += main.cpp\
mainwindow.cpp \
tile.cpp \
- map.cpp
+ map.cpp \
+ entity.cpp \
+ monster.cpp \
+ cafard.cpp \
+ defenser.cpp \
+ watergun.cpp
HEADERS += mainwindow.h \
tile.h \
- map.h
+ map.h \
+ entity.h \
+ monster.h \
+ cafard.h \
+ types.h \
+ defenser.h \
+ watergun.h
FORMS += mainwindow.ui
--- /dev/null
+#include "cafard.h"
+#include <iostream>
+using namespace TYPE;
+
+Cafard::Cafard(Map* map, int posx,int posy, int size):Monster(posx,posy,size),_map(map)
+{
+ // Données du cafard
+ _type = T_RAMPANT;
+ _hp = 10*size*size;
+ _resistance = 5*size*size;
+ _speed = 2;
+
+ // Images et animations
+ for(int i = 0; i < 3; ++i) {
+ QPixmap* animTemp = new QPixmap("./media/textures/ennemies/cafard" + QString::number(i+1) + ".png");
+ *animTemp = animTemp->scaled(size*32,size*32);
+ _animPixmap.push_back(animTemp);
+ }
+ _animState = 0; // Première frame de l'animation à 0
+
+ // Image et taille
+ this->setPixmap(*_animPixmap.first());
+
+ // Position
+ this->setPos(_x,_y);
+}
+
+void Cafard::advance(int phase) {
+
+ // Si 'phase' vaut 0, rien ne se passe
+ if(!phase)
+ return;
+ // ...sinon, on met à jour l'item
+
+ // Met à jour l'image de l'animation
+ this->setPixmap(*_animPixmap[_animState]);
+ 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);
+ QVector2D dirVect = currentTile.getDirection();
+
+ _x += _speed*dirVect.x();
+ _y += _speed*dirVect.y();
+
+ this->setPos(_x,_y);
+
+ // Oriente l'image dans le bon sens
+ // (nb : transformation par rapport au centre de l'image)
+ this->setTransformOriginPoint(this->boundingRect().center().x(),this->boundingRect().center().y());
+ this->setRotation(dirVect.y()*90);
+
+}
--- /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, int size);
+
+protected:
+ void advance(int phase);
+
+ Map* _map;
+};
+
+#endif // CAFARD_H
--- /dev/null
+#include "defenser.h"
+
+Defenser::Defenser(int posx,int posy,int level,TYPE::ENTITY target)
+ :Entity(posx,posy),_level(level),_target(target) {}
--- /dev/null
+#ifndef DEFENSER_H
+#define DEFENSER_H
+
+#include "entity.h"
+
+class Defenser : public Entity
+{
+public:
+ Defenser(int posx,int posy,int level,TYPE::ENTITY target);
+
+ virtual int getCost(void) const = 0;
+
+protected:
+ int _level;
+ TYPE::ENTITY _target;
+ int _rate;
+ int _range;
+ int _power;
+};
+
+#endif // DEFENSER_H
--- /dev/null
+#include "entity.h"
+
+Entity::Entity(int posX, int posY): _x(posX),_y(posY) {
+}
+
+int Entity::getX(void) const { return _x; }
+int Entity::getY(void) const { return _y; }
+
+void Entity::increaseAnimationStep(void) {
+
+ // Avance à la frame suivante de l'animation
+ _animState++;
+ if(_animState >= _animPixmap.size())
+ _animState = 0;
+
+}
--- /dev/null
+#ifndef ENTITY_H
+#define ENTITY_H
+
+#include <QGraphicsPixmapItem>
+#include <QPixmap>
+#include <QList>
+
+#include "types.h"
+
+class Entity : public QGraphicsPixmapItem // , public QObject
+{
+
+ // Q_OBJECT // Utilisé pour les signaux/slots
+
+public:
+
+ Entity(int posX, int posY);
+ void increaseAnimationStep(void);
+ int getX() const;
+ int getY() const;
+
+
+
+protected:
+
+ virtual void advance(int phase) = 0;
+
+ QList<QPixmap*> _animPixmap;
+ int _x;
+ int _y;
+ int _animState; // Frame courante de l'animation
+ TYPE::ENTITY _type;
+
+
+};
+
+#endif // ENTITY_H
+
ui->setupUi(this);
+ qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
+
/// Test : try to draw some tiles into the QGraphicView
- _scene = new QGraphicsScene(parent);
+ _sceneMap = new Map(parent);
_view = ui->graphicsView;
- _view->setScene(_scene);
-
- _currentMap = new Map(_scene);
- _currentMap->LoadBackground();
+ _view->setScene(_sceneMap);
+ _view->setRenderHint(QPainter::Antialiasing);
+ _view->setCacheMode(QGraphicsView::CacheBackground);
+ _view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
_view->show();
+
+ // Lancement du timer (boucle principale du jeu)
+ QObject::connect(&timer, SIGNAL(timeout()), _sceneMap, SLOT(advance()));
+ timer.start(1000 / 20); // 10 FPS
+
+
}
+
MainWindow::~MainWindow()
{
delete ui;
#define MAINWINDOW_H
#include <QMainWindow>
-#include <QGraphicsScene>
+#include "map.h"
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
+#include <QTimer>
+#include <QTime>
+
#include "map.h"
namespace Ui {
Ui::MainWindow *ui;
- QGraphicsScene *_scene;
QGraphicsView *_view;
-
- Map *_currentMap;
+ QTimer timer;
+ Map* _sceneMap;
};
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
+ <property name="alignment">
+ <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
+ </property>
</widget>
</widget>
</widget>
-Map::Map(QGraphicsScene* scene)
+Map::Map(QObject *parent):QGraphicsScene(parent)
{
-
- // Container
- _scene = scene;
-
- /* // Loads map file
+ /* // Loads map file
QFile mapFile("media/maps/map.txt");
if(!mapFile.open(QIODevice::ReadOnly | QIODevice::Text))
return;
-
-
-
// Reads map data and creates corresponding map tiles
int y = 0;
while(!mapFile.atEnd()){
_mapTiles[x][y] = new Tile(tileNumber[x].toInt());
y++;
}
-
// Close file
mapFile.close();*/
for(j = 0; j < 16; j++)
{
fichier >> num;
- std::cout << num << std::endl;
- _mapTiles[i][j] = new Tile(num);
+ _mapTiles[j][i] = new Tile(num, j, i);
+ this->addItem(_mapTiles[j][i]);
}
}
}
-}
+ fichier.close();
-void Map::LoadBackground(void) {
+ _entities.push_front(new Cafard(this,32*2,0,1));
+ this->addItem(_entities.first());
- for(int x = 0 ; x < 16 ; ++x){
- for(int y = 0 ; y < 16 ; ++y){
- _mapTiles[x][y]->setPos(y*32,x*32);
- _scene->addItem(_mapTiles[x][y]);
- }
- }
}
-Map::~Map(){
+// Gestion du clic de la souris
+void Map::mousePressEvent(QGraphicsSceneMouseEvent *event) {
+ qreal mx = event->scenePos().x();
+ qreal my = event->scenePos().y();
+
+ if(event->button() == Qt::LeftButton &&
+ this->getTileAt(mx/32,my/32).turretAllowed())
+ {
+ std::cout << "clic at : " << mx << "," << my << std::endl;
+ _entities.push_front(new WaterGun(((int)(mx/32))*32,
+ ((int)(my/32))*32,
+ 1,
+ (TYPE::ENTITY)(TYPE::T_RAMPANT | TYPE::T_VOLANT))
+ );
+ this->addItem(_entities.first());
+
+ }
}
+
+Tile& Map::getTileAt(int x, int y) const { return *_mapTiles[x][y]; }
#include <QFile>
#include <QList>
#include <QGraphicsScene>
+#include <QGraphicsSceneMouseEvent>
+#include <QMouseEvent>
+
#include "tile.h"
+#include "entity.h"
+#include "cafard.h"
+#include "watergun.h"
+
-class Map
+class Map : public QGraphicsScene
{
private:
// QList<int>* _ennemyList;
Tile* _mapTiles[16][16];
+ QList<Entity*> _entities;
QGraphicsScene* _scene;
public:
- Map(QGraphicsScene* scene);
- ~Map();
- void LoadBackground(void);
+ Map(QObject* parent);
+ void mousePressEvent(QGraphicsSceneMouseEvent *event);
+ Tile& getTileAt(int x, int y) const;
};
#endif // MAP_H
0 0 18 0 0 0 0 0 0 0 0 0 0 0 0 0 \r
0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 \r
-0 0 6 4 4 4 4 4 4 4 4 6 0 0 0 0 \r
+0 0 6 4 4 4 4 4 4 4 4 134 0 0 0 0 \r
0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 \r
-0 0 0 0 0 0 0 0 0 0 0 6 4 6 0 0 \r
-0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 \r
-0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 \r
-0 0 10 8 8 8 8 8 8 8 8 8 8 10 0 0 \r
+0 0 0 0 0 138 8 8 137 0 0 6 4 134 0 0 \r
+0 0 0 0 0 2 0 0 1 0 0 0 0 2 0 0 \r
+0 0 0 0 0 2 0 0 1 0 0 0 0 2 0 0 \r
+0 0 138 8 8 10 0 0 9 8 8 8 8 10 0 0 \r
0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 \r
0 0 2 0 0 0 64 64 64 0 0 0 0 0 0 0 \r
-0 0 6 4 6 0 64 64 64 64 0 0 0 0 0 0 \r
+0 0 6 4 134 0 64 64 64 64 0 0 0 0 0 0 \r
0 0 0 0 2 0 0 64 64 64 0 0 0 0 0 0 \r
0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 \r
-0 0 0 0 6 4 4 4 4 4 4 4 4 6 0 0 \r
+0 0 0 0 6 4 4 4 4 4 4 4 4 134 0 0 \r
0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 \r
0 0 0 0 0 0 0 0 0 0 0 0 0 32 0 0 \r
--- /dev/null
+Quinze petites fourmis;fourmi:1:15:20\r
+Des cafards...;cafard:1:15:20\r
+Invasion de guêpes;guepe:1:15:20\r
+Ca gratte...;moustique:1:15:20\r
+Les fourmis et une grande guêpe;fourmi:1:15:25;guepe:1.7:1:25\r
+Multiplication;cafard:2:7:30\r
+Sept cafards, trois moustiques et dix fourmis géantes;cafard:1:7:25;moustique:1.1:3:30;fourmi:3:10:40
\ No newline at end of file
--- /dev/null
+#include "monster.h"
+
+Monster::Monster(int posx,int posy, int 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"
+
+
+class Monster : public Entity {
+
+public:
+ Monster(int posx, int posy, int size);
+ int getResistance(void) const;
+ int getHP(void) const;
+
+protected:
+ int _size;
+ int _hp;
+ int _resistance;
+ int _speed;
+};
+
+
+
+#endif // MONSTER_H
+
#include "tile.h"
#include <iostream>
using namespace std;
+using namespace TYPE;
-Tile::Tile(int tileNumber)
+Tile::Tile(int tileNumber, int x, int y):_tileNumber(tileNumber)
{
- QString filePath("media/textures/");
+ // Direction associée au tile
+ int direction_bit_mask = tileNumber % 16; // Modulo 16 en cas de tile de départ
+
+ // Déplacement horizontal
+ // Ouest
+ if((direction_bit_mask & WEST) == WEST)
+ _dir.setX(-1);
+ // Est
+ if((direction_bit_mask & EAST) == EAST)
+ _dir.setX(1);
+ // Déplacement vertical
+ // Nord
+ if((direction_bit_mask & NORTH) == NORTH)
+ _dir.setY(-1);
+ // Sud
+ if((direction_bit_mask & SOUTH) == SOUTH)
+ _dir.setY(1);
+
+
+ // DEBUG std::cout << "at " << x << "," << y << " ,tileNumber : " << tileNumber << ", dir.setX: " << _dir.x() << ", dir.setY: " << _dir.y() << std::endl;
+
+ // Positionne relativement à la scène
+ this->setPos(x*32,y*32);
+
+
+ /* QString filePath("./media/textures/");
filePath+= TexName(tileNumber); // TEST
filePath+= QString(".jpg");
- cout << filePath.toStdString() << endl;
- QPixmap texFile(filePath);
- texFile.scaled(32,32,Qt::IgnoreAspectRatio);
- this->setPixmap(texFile);
+ cout << filePath.toStdString() << endl;*/
+ _pixmap = new QPixmap("media/textures/plain/" + TexName(tileNumber) + ".png");
+ if (_pixmap->isNull())
+ cout << "pixmap null" << endl;
+
+ _x = x;
+ _y = y;
+ *_pixmap = _pixmap->scaled(32,32,Qt::IgnoreAspectRatio);
+ this->setPixmap(*_pixmap);
}
+int Tile::getX() const { return _x; }
+int Tile::getY() const { return _y; }
+QVector2D Tile::getDirection(void) const { return _dir; }
+
+bool Tile::turretAllowed(void) const {
+
+ if(_tileNumber == 0)
+ return true;
+ return false;
+}
+
QString Tile::TexName(int n){
// Normal textures
- if(n>0 && n<32){
-
- n%=16; // To keep a value between 0 and 10
+ if((n & (TYPE::NORTH | TYPE::EAST | TYPE::UP)) == (TYPE::NORTH | TYPE::EAST | TYPE::UP))
+ return "nw";
+ if((n & (TYPE::NORTH | TYPE::EAST)) == (TYPE::NORTH | TYPE::EAST))
+ return "se";
+ if((n & (TYPE::NORTH | TYPE::WEST | TYPE::UP)) == (TYPE::NORTH | TYPE::WEST | TYPE::UP))
+ return "ne";
+ if((n & (TYPE::NORTH | TYPE::WEST)) == (TYPE::NORTH | TYPE::WEST))
+ return "sw";
+ if((n & (TYPE::SOUTH | TYPE::EAST | TYPE::UP)) == (TYPE::SOUTH | TYPE::EAST | TYPE::UP))
+ return "ne";
+ if((n & (TYPE::SOUTH | TYPE::EAST)) == (TYPE::SOUTH | TYPE::EAST))
+ return "sw";
+ if((n & (TYPE::SOUTH | TYPE::WEST | TYPE::UP)) == (TYPE::SOUTH | TYPE::WEST | TYPE::UP))
+ return "nw";
+ if((n & (TYPE::SOUTH | TYPE::WEST)) == (TYPE::SOUTH | TYPE::WEST))
+ return "se";
- if(n==0)
- return "grass";
- if(n==1 || n==2)
- return "hpath";
- if(n==4 || n==8)
- return "vpath";
- if(n==5 || n==10)
- return "nepath";
- if(n==6 || n==9)
- return "nopath";
+ if( ((n & TYPE::NORTH) == TYPE::NORTH) ||
+ ((n & TYPE::SOUTH) == TYPE::SOUTH)
+ )
+ return "vpath";
+ if( ((n & TYPE::EAST) == TYPE::EAST) ||
+ ((n & TYPE::WEST) == TYPE::WEST)
+ )
+ return "hpath";
- }
+ // Special textures
+ if(n==32)
+ return "goal";
if(n==64)
return "mud";
- // Special textures
+
return "grass";
}
#define TILE_H
#include <QGraphicsPixmapItem>
+#include <QVector2D>
+#include "types.h"
+
+// Masque binaire pour déterminer l'orientation d'un tile
class Tile : public QGraphicsPixmapItem
{
public:
- Tile(int tileNumber);
+ Tile(int tileNumber, int x, int y);
~Tile(void);
+ int getX() const;
+ int getY() const;
+ bool turretAllowed(void) const;
+ QVector2D getDirection(void) const;
- QString TexName(int n);
+ QString TexName(int n);
+private:
+ QPixmap *_pixmap;
+ int _x, _y;
+ int _tileNumber;
+ QVector2D _dir;
};
#endif // TILE_H
--- /dev/null
+#ifndef TYPES_H
+#define TYPES_H
+
+namespace TYPE {
+
+// Utilisation du bit 2^7 pour coder une information supplémentaire
+// concernant les diagonales (cf doc)
+
+typedef enum DIRECTION { NORTH = 1 , SOUTH = 2 , EAST = 4 , WEST = 8, UP = 128 } DIRECTION;
+typedef enum ENTITY { T_RAMPANT = 1, T_VOLANT = 2 } ENTITY;
+
+}
+
+#endif // TYPES_H
--- /dev/null
+#include <iostream>
+#include <math.h>
+#include "watergun.h"
+
+WaterGun::WaterGun(int posx,int posy,int level,TYPE::ENTITY target)
+ : Defenser(posx,posy,level,target)
+{
+ updateStats();
+ this->setPos(_x,_y);
+}
+
+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(-100,-100,200,200);
+}
+
+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;
+
+
+
+}
--- /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);
+ int getCost(void) const;
+ void paint(QPainter *painter, const QStyleOptionGraphicsItem *,QWidget*);
+ QRectF boundingRect() const;
+protected:
+ void advance(int phase);
+ void updateStats(void);
+
+ Map* _map;
+};
+
+#endif // WATERGUN_H