-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmuk.cpp
107 lines (93 loc) · 3.03 KB
/
muk.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**@file muk.cpp
* @brief Contains the implementation for the Muk class
*/
#include "muk.h"
#include "game.h"
#include <QGraphicsScene>
#include <QList>
//external global varaible Game* game
extern Game* game;
/**This constructor sets the Pixmap for Muk
* @brief Muk::Muk sets the Pixmap for a Muk object.
* @param parent makes QGraphicsPixmapItem a parent of Muk
*/
Muk::Muk(QGraphicsItem *parent) : QObject(), QGraphicsPixmapItem (parent)
{
setPixmap(QPixmap(":/new/prefix1/Muk.png"));
setScale(.5);
// make/connect a timer to attack()
muk_timer = new QTimer(this);
connect(muk_timer, SIGNAL(timeout()), this, SLOT(attack()));
// start the timer
muk_timer->start(100);
// add attack sound
attack_sound = new QMediaPlayer();
attack_sound->setMedia(QUrl("qrc:/sounds/sound/Strong_Punch-Mike_Koenig-574430706.mp3"));
}
/**@ set Ash
*/
void Muk::setAsh(Ash* player){
ash = player;
}
/**@ implement Muk attacking Ash
* @ check if it collides with the player and react to collision
* @ decrease a health if collision
*/
void Muk::attack(){
if (!game->ash_win && !game->ash_lose){
QList<QGraphicsItem*> colliding_items = collidingItems();
// if one of the colliding items is an player, decrease a health
for (size_t i = 0, n = colliding_items.size(); i < n; ++i){
if (typeid(*(colliding_items[i])) == typeid(Ash)){
// change ash's color
ash->damaged();
// play sound
if (attack_sound->state() == QMediaPlayer::PlayingState) attack_sound->setPosition(0);
else if (attack_sound->state() == QMediaPlayer::StoppedState) attack_sound->play();
// decrease health
game->health--;
muk_timer->stop();
QTimer::singleShot(600,muk_timer,SLOT(start()));
return;
}
}
}
else {
scene()->removeItem(this);
// delete them from the heap to save memory
delete this;
return;
}
}
/**@ creates and connect QTimer based on the movement
*/
void Muk::start_movement(){
// make/connect the movement_timer
movement_timer = new QTimer(this);
move_horizontal();
connect (movement_timer, SIGNAL(timeout()), this, SLOT(move_horizontal()));
// start the movement timer
movement_timer->start(2800);
}
/**@ implement the micro movements of Beedril
*/
void Muk::left(){
setPos(x()-25,y());
}
void Muk::right(){
setPos(x()+25,y());
}
/**@ implement horizontal animation of Beedril
*/
void Muk::move_horizontal(){
if (!game->ash_win && !game->ash_lose) {
QTimer::singleShot(300,this,SLOT(left()));
QTimer::singleShot(650,this,SLOT(left()));
QTimer::singleShot(1000,this,SLOT(right()));
QTimer::singleShot(1350,this,SLOT(right()));
QTimer::singleShot(1700,this,SLOT(right()));
QTimer::singleShot(2050,this,SLOT(right()));
QTimer::singleShot(2400,this,SLOT(left()));
QTimer::singleShot(2750,this,SLOT(left()));
}
}