-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbullet.cpp
78 lines (66 loc) · 2.19 KB
/
bullet.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
#include "bullet.h"
#include "enemy.h"
#include "gift.h"
#include <QPixmap>
#include <QTimer>
#include <qmath.h>
#include <QDebug>
#include <QGraphicsScene>
#include <QMediaPlayer>
#include <QAudioOutput>
#include "game.h"
extern Game* game;
Bullet::Bullet() {
// initialize the bullet picture and dimensions
int len = 30;
if(game->getMap() != 3) {
setPixmap(QPixmap(":/images/img/bullet.png").scaled(len, len));
} else {
setPixmap(QPixmap(":/images/img/bullet2.png").scaled(len, len));
}
QTimer* timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(move()));
timer->start(50);
damage = 20;
setZValue(5);
//play sound
game->playSound(QUrl("qrc:/audio/audio/bullet.wav"));
}
int Bullet::getDamage() {return damage;}
void Bullet::setDamage(int x) {damage = x;}
void Bullet::move() {
// handle if the bullet collides with an enemy
QList<QGraphicsItem *> collided_items = collidingItems();
foreach(auto& item, collided_items) {
if(item && typeid(*item) == typeid(Enemy)) {
Enemy* e = dynamic_cast<Enemy*> (item);
e->decrementHealth(damage);
// remove the bullet from the scene
scene()->removeItem(this);
// release the memory from the heap
delete this;
return;
} else if(item && typeid(*item) == typeid(Gift)) {
Gift* g = dynamic_cast<Gift*> (item);
if(g != NULL) {
g->utilize();
game->getScene()->removeItem(g);
delete g;
game->getScene()->removeItem(this);
delete this;
return;
}
}
}
// handle the bullet movement
const int STEP_SIZE = 20; // this represents the velocity of the bullet
double theta = rotation(); // degrees
double dy = STEP_SIZE * qSin(qDegreesToRadians(theta));
double dx = STEP_SIZE * qCos(qDegreesToRadians(theta));
setPos(x()+dx, y()+dy);
// handle the case when the bullet goes out of the view
if(y() < 0 || y() > scene()->height() || x() < 0 || x() > scene()->width()) {
scene()->removeItem(this);
delete this;
}
}