-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHealthInterface.cpp
More file actions
58 lines (49 loc) · 1.19 KB
/
HealthInterface.cpp
File metadata and controls
58 lines (49 loc) · 1.19 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
#include "HealthInterface.h"
#include "Event.h"
#include "TextureData.h"
#include <SFML/Graphics/RenderTarget.hpp>
HealthInterface::HealthInterface() :
Listener({ EventType::UpdateHealthInterface }),
sf::Drawable(),
sprites_()
{}
void HealthInterface::onEvent(const Event &ev)
{
this->update_(
static_cast<const EventUpdateHealthInterface &>(ev).getHealth());
}
void HealthInterface::draw(sf::RenderTarget &target, sf::RenderStates states) const
{
for (const sf::Sprite &sprite : sprites_)
{
target.draw(sprite, states);
}
}
void HealthInterface::update_(uint8_t health)
{
if (sprites_.size() < health)
{
const sf::Texture &heartTexture = TextureData::getInstance().getTexture(21);
while (sprites_.size() < health)
{
sf::Sprite sprite(heartTexture);
sprite.setPosition(
sf::Vector2f(
12.0f + 8.0f * sprites_.size(),
60.0f));
const sf::FloatRect spriteLocalBounds = sprite.getLocalBounds();
sprite.setOrigin(
sf::Vector2f(
spriteLocalBounds.width * 0.5f,
spriteLocalBounds.height * 0.5f));
sprites_.push_back(std::move(sprite));
}
}
else if (sprites_.size() > health)
{
while (sprites_.size() > health)
{
sprites_.pop_back();
}
}
}