-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemy.cpp
More file actions
72 lines (60 loc) · 1.55 KB
/
enemy.cpp
File metadata and controls
72 lines (60 loc) · 1.55 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
66
67
68
69
70
#include "enemy.h"
#include <QPixmap>
#include <qmath.h>
#include <QTimer>
#include <game.h>
#include <QApplication>
#include <QMessageBox>
extern Game *game;
Enemy::Enemy(QList<QPointF> pointsToFollow,QGraphicsItem *parent)
{
//set graphics
setPixmap(QPixmap(":/images/enemy.png"));
//set points
points = pointsToFollow;
point_index = 0;
dest = points[0];
rotateToPoint(dest);
//connect timer to move forward
QTimer *timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(move_forward()));
timer->start(150);
}
void Enemy::rotateToPoint(QPointF p)
{
QLineF ln(pos(),p);
setRotation(-1 * ln.angle());
}
void Enemy::move_forward()
{
//if close to dest, rotate to dest
QLineF ln(pos(),dest);
if(ln.length() < 5){
point_index++;
if(point_index>=points.size()){
return;
}
dest=points[point_index];
rotateToPoint(dest);
}
//move froward to current
int STEP_SIZE = 15;
double theta = rotation(); //degres
double dy = STEP_SIZE * qSin(qDegreesToRadians(theta));
double dx = STEP_SIZE * qCos(qDegreesToRadians(theta));
setPos(x()+dx,y()+dy);
if(pos().y()>600){
//delete and decrese
game->health->decrease();
if(game->health->getHealth() == 0)
{
game->close();
QMessageBox msgBox;
msgBox.setText("GAME OVER!!");
msgBox.exec();
msgBox.setFixedSize(500,500);
}
scene()->removeItem(this);
delete this;
}
}