-
Notifications
You must be signed in to change notification settings - Fork 0
/
bullet.cpp
60 lines (50 loc) · 1.64 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
#include "bullet.h"
#include <QTimer>
#include <QGraphicsScene>
#include <QList>
#include "enemy.h"
#include <typeinfo>
#include "game.h"
#include "score.h"
#include <QMediaPlayer>
#include "health.h"
extern Game * game;
Bullet::Bullet(QGraphicsItem *parent): QObject(), QGraphicsPixmapItem(parent)
{
setPixmap(QPixmap(":/images/bullet.png"));
QTimer * timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(move()));
timer->start(35);
}
void Bullet::move()
{
QList<QGraphicsItem *> colliding_items = collidingItems();
for (int i = 0, n = colliding_items.size(); i < n; i++)
{
if (typeid(*(colliding_items[i])) == typeid(Enemy))
{
game->score->increase();
scene()->removeItem(colliding_items[i]);
scene()->removeItem(this);
deathsound = new QMediaPlayer();
deathsound->setMedia(QUrl("qrc:soundeffects/8bitboom1-1.wav"));
if (deathsound->state() == QMediaPlayer::StoppedState){
qDebug() << "attempted to play deathsound from stopped state" << deathsound->state() << deathsound->error() << deathsound->mediaStatus();
deathsound->play();
}
else
{
qDebug() << "attempted to play deathsound otherwise " << deathsound->state() << deathsound->error() << deathsound->mediaStatus();
deathsound->stop();
}
delete colliding_items[i];
delete this;
return;
}
}
setPos(x(), y()-18);
if (pos().y() + pixmap().height()/2 < 0) {
scene()->removeItem(this);
delete this;
}
}