-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathufo.cpp
83 lines (72 loc) · 1.74 KB
/
ufo.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "ufo.h"
#include "game.h"
#include "shard.h"
#include "spark.h"
#include "ufobullet.h"
extern Game* game;
ufo::ufo()
{
setPixmap(QPixmap(":/images/ufo.png"));
setPos(game->gameWidth - 200, rand() % game->gameHeight);
setZValue(39);
behavior = (rand() % 2) + 1;
}
void ufo::destroy()
{
if(game->enableParticles){
//Burst of ufo shards
for(int i = 0; i < 30; i ++){
Shard * u = new Shard(x(), y(), false);
scene()->addItem(u);
connect(game, SIGNAL(frame()), u, SLOT(fall()));
}
//Burst of sparks
for(int j = 0; j < 40; j ++){
Spark * s = new Spark(x(), y(), 50);
scene()->addItem(s);
connect(game, SIGNAL(frame()), s, SLOT(fall()));
}
}
//Get rid of the ufo
scene()->removeItem(this);
delete this;
}
void ufo::decreaseHealth()
{
health -= 1;
if(health == 0){
destroy();
}
}
void ufo::shoot()
{
timeSinceShot ++;
if(timeSinceShot > reloadTime){
ufoBullet* uu = new ufoBullet(x(), y());
scene()->addItem(uu);
connect(game, SIGNAL(frame()), uu, SLOT(move()));
timeSinceShot = 0;
}
}
void ufo::move()
{
if(behavior == 1){ //Move up and down
if(movingUp){
setPos(x(), y() - speed);
}else{
setPos(x(), y() + speed);
}
if(y() < 0){
movingUp = false;
}else if(y() > game->gameHeight - 50){
movingUp = true;
}
}else if(behavior == 2){ //follow player
if(y() > game->player->y() + 26){
setPos(x(), y() - speed);
}else if(y() < game->player->y() + 24){
setPos(x(), y() + speed);
}
}
shoot();
}