-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbullet.cpp
More file actions
66 lines (57 loc) · 1.54 KB
/
bullet.cpp
File metadata and controls
66 lines (57 loc) · 1.54 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
#include "bullet.h"
#include <QGraphicsScene>
#include <QTimer>
#include "alien.h"
#include "cannon.h"
#include "settings.h"
CBullet::CBullet(int speed, QGraphicsItem *pParent) : QGraphicsPixmapItem(pParent)
{
QPixmap pixmap;
pixmap.load(":/Resources/RedBullet.png");
setPixmap(pixmap.scaled(QSize(40, 40), Qt::KeepAspectRatio));
this->speed = speed;
}
CBullet::~CBullet()
{
emit sigDeleted();
}
void CBullet::advance(int step)
{
// Check if we've hit an alien
// If so, remove this and the alien and increase score
QList<QGraphicsItem *> listCollidingItem = collidingItems();
for (auto const pItem : listCollidingItem)
{
CAlien *pAlien = dynamic_cast<CAlien *>(pItem);
if (pAlien != nullptr && speed < 0)
{
CAlien::alienCount--;
scene()->removeItem(pAlien);
scene()->removeItem(this);
emit sigIncreaseScore();
delete pAlien;
delete this;
return;
}
CCannon *pCannon = dynamic_cast<CCannon *>(pItem);
if (pCannon != nullptr && speed > 0)
{
scene()->removeItem(this);
emit sigDecreaseHealth();
delete this;
return;
}
}
// change the position of the bullet
// if (step)
if (step)
{
setPos(x(), y() + speed);
// delete it if it goes offscreen
if (pos().y() < 0 || pos().y() > scene()->height())
{
scene()->removeItem(this);
delete this;
}
}
}