-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cherry.cpp
53 lines (43 loc) · 1.37 KB
/
Cherry.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
#include "Cherry.h"
#include <QList>
#include <QTimer>
#include <QGraphicsScene>
#include "Zombie.h"
Cherry::Cherry(QTimer * cherryTimer , QGraphicsItem *parent)
: QObject() , Plant(parent)
{
// set picture
setPixmap(QPixmap(":/images/cherry.png"));
// set player
cherryPlayer = new QMediaPlayer();
cherryPlayer->setMedia(QUrl("qrc:/musics/cherrybomb.mp3"));
// connect
connect(cherryTimer , SIGNAL(timeout()) , this , SLOT(explode()));
timeIntervals = 0;
}
void Cherry::explode() {
timeIntervals += 50;
// it explodes after 2 seconds
if(timeIntervals == 2000){
// play cherryPlayer
if(cherryPlayer->state() == QMediaPlayer::PlayingState){
cherryPlayer->setPosition(0);
}
else if (cherryPlayer->state() == QMediaPlayer::StoppedState){
cherryPlayer->play();
}
// collect all colliding items in a list
QList<QGraphicsItem *> collidingList = collidingItems();
for (size_t i = 0 ; i < collidingList.size() ; ++i){
Zombie * zom = dynamic_cast<Zombie*>(collidingList[i]);
if(zom){
// delete and remove zombie
scene()->removeItem(collidingList[i]);
delete collidingList[i];
//return;
}
}
scene()->removeItem(this);
delete this;
}
}