r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
HealthBarSystem.cpp
Go to the documentation of this file.
1///
2/// @file HealthBarSystem.cpp
3/// @brief Implementation of HealthBarSystem for rendering health bars
4/// @namespace gme
5///
6
8#include "ECS/Component.hpp"
9#include "Utils/Logger.hpp"
10#include <algorithm>
11
12namespace gme
13{
14 HealthBarSystem::HealthBarSystem(const std::shared_ptr<eng::IRenderer> &renderer) : m_renderer(renderer) {}
15
16 void HealthBarSystem::update(ecs::Registry &registry, float /*dt*/)
17 {
18 // Iterate through all entities with Player component
19 auto playerEntities = registry.getAll<ecs::Player>();
20
21 for (const auto &[entity, player] : playerEntities)
22 {
23 auto *transform = registry.getComponent<ecs::Transform>(entity);
24 auto *health = registry.getComponent<ecs::Health>(entity);
25
26 // Only render health bars for players with health
27 if (transform && health)
28 {
29 // Don't show health bar if at full health (optional: remove this to always show)
30 if (health->current < health->max)
31 {
32 drawHealthBar(transform->x, transform->y, health->current, health->max);
33 }
34 }
35 }
36 }
37
39 {
40 if (healthPercent > 0.75f)
41 {
42 return HEALTH_HIGH; // Green
43 }
44 else if (healthPercent > 0.50f)
45 {
46 return HEALTH_MEDIUM; // Yellow
47 }
48 else if (healthPercent > 0.25f)
49 {
50 return HEALTH_LOW; // Orange
51 }
52 else
53 {
54 return HEALTH_CRITICAL; // Red
55 }
56 }
57
58 void HealthBarSystem::drawHealthBar(float x, float y, float currentHealth, float maxHealth)
59 {
60 if (maxHealth <= 0.0f)
61 return;
62
63 // Calculate health percentage
64 float healthPercent = std::clamp(currentHealth / maxHealth, 0.0f, 1.0f);
65
66 // Calculate bar position (centered above entity)
67 float barX = x - (BAR_WIDTH / 2.0f);
68 float barY = y + BAR_OFFSET_Y;
69
70 // TODO: IRenderer doesn't support rectangle drawing yet
71 // For now, we use circles to represent health
72 // Draw health indicator using circle shapes
73 std::string healthCircleName =
74 "health_" + std::to_string(static_cast<int>(x)) + "_" + std::to_string(static_cast<int>(y));
75
76 eng::Color healthColor = getHealthColor(healthPercent);
77 float circleRadius = 3.0f;
78
79 m_renderer->createCircleShape({.name = healthCircleName,
80 .radius = circleRadius,
81 .color = healthColor,
82 .x = barX + (BAR_WIDTH * healthPercent),
83 .y = barY});
84 m_renderer->drawCircleShape(healthCircleName);
85
86 (void)barX; // Suppress unused variable warning
87 (void)barY;
88 (void)healthPercent;
89 }
90
91} // namespace gme
This file contains the component definitions.
System for rendering health bars above entities in multiplayer mode.
This file contains the Logger class.
Class for managing entities and their components.
Definition Registry.hpp:25
std::unordered_map< Entity, T > & getAll()
Definition Registry.hpp:77
T * getComponent(Entity e)
Definition Registry.hpp:71
virtual void createCircleShape(CircleShape circleShape)=0
virtual void drawCircleShape(const std::string &name)=0
static constexpr float BAR_WIDTH
Width of the health bar in pixels.
static constexpr eng::Color HEALTH_HIGH
Green color for >75% health.
void update(ecs::Registry &registry, float dt) override
Update the health bar system (called each frame)
void drawHealthBar(float x, float y, float currentHealth, float maxHealth)
Render a health bar at specified position.
static constexpr eng::Color HEALTH_CRITICAL
Red color for <25% health.
eng::Color getHealthColor(float healthPercent) const
Calculate health bar color based on health percentage.
static constexpr float BAR_OFFSET_Y
Vertical offset above entity (negative = above)
static constexpr eng::Color HEALTH_MEDIUM
Yellow color for 50-75% health.
static constexpr eng::Color HEALTH_LOW
Orange color for 25-50% health.
HealthBarSystem(const std::shared_ptr< eng::IRenderer > &renderer)
Constructor.
const std::shared_ptr< eng::IRenderer > & m_renderer
Reference to the renderer for drawing.