-
Notifications
You must be signed in to change notification settings - Fork 0
/
healthbar.cpp
113 lines (100 loc) · 2.6 KB
/
healthbar.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
#include "healthbar.h"
#include "game.h"
#include <QBrush>
extern Game* game;
HealthBar::HealthBar(int x, int y, int width, double health, bool blue) {
// initialize data members
currHealth = maxHealth = health;
this->width = width;
// create the colors
QBrush gray(Qt::gray);
QBrush green(Qt::green);
QBrush blueColor(Qt::blue);
// create the rectangles
int offset = 8, height = 4;
int final_x = x, final_y = !blue ? y - offset : y;
setRect(final_x, final_y, width, height);
setZValue(8);
setBrush(gray);
progressBar = new QGraphicsRectItem(final_x, final_y, width, height, this);
progressBar->setZValue(9);
if(!blue)
progressBar->setBrush(green);
else
progressBar->setBrush(blueColor);
}
QGraphicsRectItem *HealthBar::getProgressBar()
{
return progressBar;
}
int HealthBar::getCurrHealth() {
return currHealth;
}
void HealthBar::decrementCurrHealth(int damage) {
currHealth -= damage;
if(currHealth < 0) currHealth = 0;
// calculate the new width
QBrush red(Qt::red);
double per = currHealth/maxHealth;
int newWidth = per * width;
// set the new width
QRectF rect = progressBar->rect();
rect.setWidth(newWidth);
progressBar->setRect(rect);
if(per < 0.5)
progressBar->setBrush(red);
}
void HealthBar::incrementCurrHealth(int x)
{
currHealth += x;
if(currHealth > maxHealth) currHealth = maxHealth;
// calculate the new width
QBrush green(Qt::green);
double per = currHealth/maxHealth;
int newWidth = per * width;
// set the new width
QRectF rect = progressBar->rect();
rect.setWidth(newWidth);
progressBar->setRect(rect);
if(per >= 0.5)
progressBar->setBrush(green);
}
void HealthBar::provideShield()
{
QBrush goldBrush(Qt::yellow);
progressBar->setBrush(goldBrush);
}
void HealthBar::removeShield()
{
QBrush green(Qt::green);
QBrush red(Qt::red);
double per = currHealth/maxHealth;
if(per >= 0.5)
progressBar->setBrush(green);
else
progressBar->setBrush(red);
}
void HealthBar::setCurrHealth(double x)
{
currHealth = x;
double per = currHealth/maxHealth;
int newWidth = per * width;
QRectF rect = progressBar->rect();
rect.setWidth(newWidth);
progressBar->setRect(rect);
if(per < 0.5)
progressBar->setBrush(Qt::red);
}
void HealthBar::show()
{
game->getScene()->addItem(this);
}
void HealthBar::hide()
{
game->getScene()->removeItem(this);
}
void HealthBar::move(int dx, int dy)
{
setPos(x() + dx, y() + dy);
// progressBar->setPos(x() + dx, y() + dy);
}