-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbullet.cpp
46 lines (37 loc) · 923 Bytes
/
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
#include "bullet.h"
#include "hazard.h"
#include <QGraphicsScene>
#include <QDebug>
Bullet::Bullet(QTimer * t, int h, int x, int y)
{
limit = h;
vx = x;
vy = y;
timer = t;
setPixmap(QPixmap(":/bullet/images/bullet.png"));
setZValue(2);
connect(timer, SIGNAL(timeout()), this, SLOT(move()));
}
Bullet::~Bullet()
{
if(scene() != NULL){
scene()->removeItem(this);
}
}
void Bullet::move()
{
QList<QGraphicsItem*> colliding_items = collidingItems();
for(int i = 0, n = colliding_items.size();i<n; ++i){
Monster * monster = dynamic_cast<Monster *>(colliding_items[i]);
if(monster){
monster->hit();
} else {
continue;
}
}
setPos(x() + vx, y() + vy);
if(x() < 0 || x() > 640 || y() < limit){
scene()->removeItem(this);
delete this;
}
}