-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bullet.cpp
executable file
·84 lines (77 loc) · 1.71 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
78
79
80
81
82
83
84
#include "Bullet.hpp"
#include <QTimer>
#include <QList>
#include "Game.hpp"
#include "Enemy.hpp"
Bullet::Bullet(char currentDirection) {
direction = currentDirection;
switch(direction) {
case 'U': {
setPixmap(QPixmap(":/resources/assets/images/hit/top.png"));
break;
}
case 'D': {
setPixmap(QPixmap(":/resources/assets/images/hit/bottom.png"));
break;
}
case 'R': {
setPixmap(QPixmap(":/resources/assets/images/hit/right.png"));
break;
}
case 'L': {
setPixmap(QPixmap(":/resources/assets/images/hit/left.png"));
break;
}
default: return;
}
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(move()));
timer->start(16);
}
void Bullet::move() {
QList<QGraphicsItem *> collizion = collidingItems();
for (int i = 0, n = collizion.size(); i < n; ++i){
Enemy * enemy = dynamic_cast<Enemy *>(collizion[i]);
if (enemy) {
enemy->getDamage(direction);
scene()->removeItem(this);
delete this;
return;
}
}
switch(direction){
case 'U': {
setPos(x(),y() - 7);
if (pos().y() < 50){
scene()->removeItem(this);
delete this;
}
break;
}
case 'D': {
setPos(x(),y() + 7);
if (pos().y() > 720 - 50 - 38){
scene()->removeItem(this);
delete this;
}
break;
}
case 'R': {
setPos(x() + 7,y());
if (pos().x() > 1280 - 50 - 38){
scene()->removeItem(this);
delete this;
}
break;
}
case 'L': {
setPos(x() - 7,y());
if (pos().x() < 50){
scene()->removeItem(this);
delete this;
}
break;
}
default: return;
}
}