forked from GianlucaBotteri/CPPVideogame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatusBar.cpp
More file actions
124 lines (98 loc) · 2.94 KB
/
StatusBar.cpp
File metadata and controls
124 lines (98 loc) · 2.94 KB
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
#include "StatusBar.h"
HealthBar::HealthBar(Hero *hero) : subject(hero) {
HealthBar::attach();
}
HealthBar::~HealthBar() {
HealthBar::detach();
}
void HealthBar::attach() {
subject->subscribe(this);
}
void HealthBar::detach() {
subject->unsubscribe(this);
}
void HealthBar::update() {
sf::Sprite tmpSprite = subject->getHeartsSprite();
if (subject->getHealth() >= 8) {
tmpSprite.setTextureRect(sf::IntRect(0, 0, 150, 36));
subject->setHeartsSprite(tmpSprite);
} else {
tmpSprite.setTextureRect(sf::IntRect(0, (8 - subject->getHealth()) * 36, 150, 36));
subject->setHeartsSprite(tmpSprite);
}
}
ExperienceBar::ExperienceBar(Hero *hero) : subject(hero) {
ExperienceBar::attach();
}
ExperienceBar::~ExperienceBar() {
ExperienceBar::detach();
}
void ExperienceBar::attach() {
subject->subscribe(this);
}
void ExperienceBar::detach() {
subject->unsubscribe(this);
}
void ExperienceBar::update() {
sf::Sprite tmpSprite = subject->getExpSprite();
tmpSprite.setTextureRect(sf::IntRect(0, subject->getExp() * 59, 150, 59));
subject->setExpSprite(tmpSprite);
}
MoneyBar::MoneyBar(Hero *hero) : subject(hero) {
MoneyBar::attach();
}
MoneyBar::~MoneyBar() {
MoneyBar::detach();
}
void MoneyBar::attach() {
subject->subscribe(this);
}
void MoneyBar::detach() {
subject->unsubscribe(this);
}
void MoneyBar::update() {
subject->text.setString(std::to_string(subject->getMoney()));
}
WeaponBar::WeaponBar(Hero *hero) : subject(hero) {
WeaponBar::attach();
}
WeaponBar::~WeaponBar() {
WeaponBar::detach();
}
void WeaponBar::attach() {
subject->subscribe(this);
}
void WeaponBar::detach() {
subject->unsubscribe(this);
}
void WeaponBar::update() {
sf::Sprite tmpSprite = subject->getWeaponSprite();
if (subject->isChangeToSword() && subject->isChangeToStick() && subject->isChangeToAxe())
tmpSprite.setTextureRect(sf::IntRect(0, 0, 150, 76));
if (subject->isChangeToSword() && subject->isChangeToStick() && !subject->isChangeToAxe())
tmpSprite.setTextureRect(sf::IntRect(0, 380, 150, 76));
if (subject->isChangeToSword() && !subject->isChangeToStick() && subject->isChangeToAxe())
tmpSprite.setTextureRect(sf::IntRect(0, 456, 150, 76));
if (!subject->isChangeToSword() && subject->isChangeToStick() && subject->isChangeToAxe())
tmpSprite.setTextureRect(sf::IntRect(0, 532, 150, 76));
subject->setWeaponSprite(tmpSprite);
}
PotionBar::PotionBar(Hero *hero) : subject(hero) {
PotionBar::attach();
}
PotionBar::~PotionBar() {
PotionBar::detach();
}
void PotionBar::attach() {
subject->subscribe(this);
}
void PotionBar::detach() {
subject->unsubscribe(this);
}
void PotionBar::update() {
sf::Sprite tmpSprite = subject->getPotionsSprite();
if (subject->getPotion()->getUseTime() <= 0) {
tmpSprite.setTextureRect(sf::IntRect(0, 490, 150, 70));
}
subject->setPotionsSprite(tmpSprite);
}