-
Notifications
You must be signed in to change notification settings - Fork 0
/
castle.cpp
130 lines (104 loc) · 2.39 KB
/
castle.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "castle.h"
#include "game.h"
#include <QMediaPlayer>
#include <QAudioOutput>
extern Game* game;
// constructor
Castle::Castle(int x, int y) {
row = y/game->getBlockUnit();
col = x/game->getBlockUnit();
QPixmap castleImg;
if(game->getMap()==1) {
castleImg = QPixmap(":/images/img/castle.png");
castleImg = castleImg.scaled(100, 100);
}
else if (game->getMap()==2) {
castleImg = QPixmap(":/images/img/castle2.png");
castleImg = castleImg.scaled(100, 100);
}
else if (game->getMap()==3) {
castleImg = QPixmap(":/images/img/castle3.png");
castleImg = castleImg.scaled(100, 120);
}
setPixmap(castleImg);
// initialize data mambers
this->x = x;
this->y = y;
setPos(this->x, this->y);
maxHealth = 100;
currHealth = maxHealth;
// initialize healthBar
healthBar = new HealthBar(this->x, this->y, 100, maxHealth);
healthBar->show();
isShielded = false;
finished = false;
}
// health functions
void Castle::setCurrHealth(int x)
{
currHealth = x;
healthBar->setCurrHealth(x);
}
void Castle::decrementCurrHealth(int x)
{
if(!isShielded) {
game->playSound(QUrl("qrc:/audio/audio/enemy_hitting.wav"));
currHealth -= x;
// qDebug() << "Health = " << currHealth << '\n';
healthBar->decrementCurrHealth(x);
if(!finished && currHealth <= 0) {
finished = true;
game->mDelay(125);
game->gameOver();
}
}
}
void Castle::incrementCurrHealth(int x)
{
if(currHealth + x <= maxHealth) {
currHealth += x;
healthBar->incrementCurrHealth(x);
}
}
bool Castle::isCurrHealthMax()
{
return currHealth == maxHealth;
}
void Castle::shield()
{
isShielded = true;
healthBar->provideShield();
}
void Castle::removeShield()
{
isShielded = false;
healthBar->removeShield();
}
int Castle::getCurrHealth()
{
return currHealth;
}
// getters
int Castle::getX() {return x;}
int Castle::getY() {return y;}
bool Castle::getIsShielded() {return isShielded;}
// setters
void Castle::setX(int x) {this->x = x;}
void Castle::setY(int y) {this->y = y;}
void Castle::setIsShielded(bool b) {isShielded = b;}
int Castle::getRow() const
{
return row;
}
int Castle::getCol() const
{
return col;
}
void Castle::setRow(int r)
{
row = r;
}
void Castle::setCol(int c)
{
col = c;
}