mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 07:53:43 +00:00
Merge branch 'ref/healthcomponent' into separation-refactor
This commit is contained in:
commit
e493960fe0
@ -12,7 +12,7 @@ class HealthComponent : public Component
|
||||
{
|
||||
public:
|
||||
|
||||
HealthComponent(int health, Direction side, std::string healthTexture) : health(health), side(side), healthTexture(healthTexture) {}
|
||||
HealthComponent(int health) : health(health) {}
|
||||
~HealthComponent() {}
|
||||
|
||||
void modifyHealth(int health = -1);
|
||||
@ -28,8 +28,4 @@ public:
|
||||
private:
|
||||
|
||||
int health;
|
||||
Direction side;
|
||||
std::string healthTexture;
|
||||
|
||||
std::vector<Entity*> heartElements;
|
||||
};
|
||||
@ -6,58 +6,14 @@
|
||||
#include <cstdio>
|
||||
|
||||
void HealthComponent::init()
|
||||
{
|
||||
refreshHearts();
|
||||
}
|
||||
{}
|
||||
|
||||
void HealthComponent::modifyHealth(int health)
|
||||
{
|
||||
this->health += health;
|
||||
this->refreshHearts();
|
||||
}
|
||||
|
||||
void HealthComponent::setHealth(int health)
|
||||
{
|
||||
this->health = health;
|
||||
this->refreshHearts();
|
||||
}
|
||||
|
||||
void HealthComponent::refreshHearts()
|
||||
{
|
||||
// clear hearts if exist
|
||||
for (auto& heart : this->heartElements) {
|
||||
heart->destroy();
|
||||
}
|
||||
|
||||
this->heartElements.clear();
|
||||
|
||||
int x; //starting position for first health icon
|
||||
|
||||
if(side == Direction::LEFT) {
|
||||
x = 10;
|
||||
} else {
|
||||
x = 730;
|
||||
}
|
||||
|
||||
for(int i = 0; i < health; i++) {
|
||||
|
||||
//checks for player side
|
||||
if (side == Direction::LEFT) {
|
||||
createHeartComponents(x);
|
||||
x += 50;
|
||||
continue;
|
||||
}
|
||||
|
||||
createHeartComponents(x);
|
||||
x -= 50;
|
||||
}
|
||||
}
|
||||
|
||||
void HealthComponent::createHeartComponents(int x)
|
||||
{
|
||||
auto& heart(this->entity->getManager().addEntity());
|
||||
heart.addComponent<TransformComponent>(x,5,2);
|
||||
heart.addComponent<SpriteComponent>(this->healthTexture.data());
|
||||
heart.addGroup((size_t)Entity::GroupLabel::HEARTS);
|
||||
this->heartElements.emplace_back(&heart);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user