-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemy.cpp
More file actions
65 lines (59 loc) · 1.9 KB
/
Copy pathenemy.cpp
File metadata and controls
65 lines (59 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "enemy.h"
#include <QTimer>
#include <QGraphicsScene>
#include <iostream>
#include "turret.h"
#include <iostream>
Enemy::Enemy(QString sprite)
{
setPixmap(QPixmap(sprite));
directionMove = 10; //De base, l'enemy se déplace de 10 px vers la droite
QTimer *timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(move()));
timer->start(50);
frameNumber = 0;
QTimer *otherTimer = new QTimer();
connect(otherTimer, SIGNAL(timeout()), this, SLOT(displayNextFrame()));
otherTimer->start(150);
}
void Enemy::move(){
QList<QGraphicsItem*> collidingItemsList = this->collidingItems();
foreach(QGraphicsItem *item, collidingItemsList){
if(typeid(*item) == typeid (Turret) ){
this->scene()->removeItem(item);
delete item;
//this->scene()->removeItem(this);
//delete this
emit touchTurret();
}
}
bool isRemove = false;
if(y() >= this->scene()->height()){ //Si il dépasse le bas de l'écran
this->scene()->removeItem(this);
isRemove = true;
delete this;
}
if(!isRemove){
if(x() <= 0){ //Si touche le bord de gauche
directionMove = 10;
this->setPos(x(), y()+30);
}
else if(x() >= this->scene()->width() - this->boundingRect().width()){
directionMove = -10;
this->setPos(x(), y()+30);
}
setPos(x()+directionMove, y());
}
}
void Enemy::displayNextFrame(){
QPixmap *spriteSheet = new QPixmap(":/spriteSheetInvaders.png");
int largeurFrame = spriteSheet->width()/2;
int hauteurFrame = spriteSheet->height()/3;
QRect *myRect = new QRect(frameNumber, 0, largeurFrame, hauteurFrame);
setPixmap(spriteSheet->copy(*myRect));
frameNumber += largeurFrame;
if(frameNumber >= spriteSheet->width()) frameNumber = 0;
}
Enemy::~Enemy(){
emit killed(10);
}