r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
HealthBarSystem.hpp
Go to the documentation of this file.
1///
2/// @file HealthBarSystem.hpp
3/// @brief System for rendering health bars above entities in multiplayer mode
4/// @details This system automatically renders health bars above all entities that have
5/// a Health component. The health bars dynamically change color based on the
6/// remaining health percentage and follow the entity's position.
7/// @namespace gme
8/// @author R-Type Team
9/// @date 2025
10///
11
12#pragma once
13
15#include "ECS/Registry.hpp"
17#include <memory>
18
19namespace gme
20{
21 ///
22 /// @class HealthBarSystem
23 /// @brief ECS System that renders dynamic health bars above entities
24 /// @details This system queries all entities with a Health component and renders
25 /// a visual health bar above each entity. Features include:
26 /// - Dynamic color based on health percentage (green -> yellow -> orange -> red)
27 /// - Positioned above entities with configurable offset
28 /// - Border and background for better visibility
29 /// - Automatic health percentage calculation
30 ///
31 /// The system is designed for multiplayer where multiple players and enemies
32 /// need health indicators.
33 /// @namespace gme
34 ///
35 class HealthBarSystem final : public ecs::ASystem
36 {
37 public:
38 ///
39 /// @brief Constructor
40 /// @param renderer Shared pointer to the rendering interface
41 ///
42 explicit HealthBarSystem(const std::shared_ptr<eng::IRenderer> &renderer);
43
44 ///
45 /// @brief Destructor
46 ///
47 ~HealthBarSystem() override = default;
48
49 ///
50 /// @brief Deleted copy constructor (non-copyable)
51 ///
53
54 ///
55 /// @brief Deleted copy assignment operator (non-copyable)
56 ///
58
59 ///
60 /// @brief Deleted move constructor (non-movable)
61 ///
63
64 ///
65 /// @brief Deleted move assignment operator (non-movable)
66 ///
68
69 ///
70 /// @brief Update the health bar system (called each frame)
71 /// @param registry ECS registry containing all entities
72 /// @param dt Delta time since last frame (unused)
73 /// @details Iterates through all entities with Health and Transform components
74 /// and renders a health bar above each one
75 ///
76 void update(ecs::Registry &registry, float dt) override;
77
78 private:
79 const std::shared_ptr<eng::IRenderer> &m_renderer; ///< Reference to the renderer for drawing
80
81 // Health bar visual configuration
82 static constexpr float BAR_WIDTH = 40.0f; ///< Width of the health bar in pixels
83 static constexpr float BAR_HEIGHT = 4.0f; ///< Height of the health bar in pixels
84 static constexpr float BAR_OFFSET_Y = -15.0f; ///< Vertical offset above entity (negative = above)
85 static constexpr float BORDER_THICKNESS = 1.0f; ///< Thickness of the border around the bar
86
87 // Health bar color thresholds and values
88 static constexpr eng::Color HEALTH_HIGH = {0, 255, 0, 255}; ///< Green color for >75% health
89 static constexpr eng::Color HEALTH_MEDIUM = {255, 255, 0, 255}; ///< Yellow color for 50-75% health
90 static constexpr eng::Color HEALTH_LOW = {255, 100, 0, 255}; ///< Orange color for 25-50% health
91 static constexpr eng::Color HEALTH_CRITICAL = {255, 0, 0, 255}; ///< Red color for <25% health
92 static constexpr eng::Color BACKGROUND = {50, 50, 50, 200}; ///< Dark gray background (semi-transparent)
93 static constexpr eng::Color BORDER = {255, 255, 255, 255}; ///< White border color
94
95 ///
96 /// @brief Calculate health bar color based on health percentage
97 /// @param healthPercent Health percentage (0.0 = dead, 1.0 = full health)
98 /// @return Color for the health bar fill
99 /// @details Color transitions:
100 /// - >75%: Green (HEALTH_HIGH)
101 /// - 50-75%: Yellow (HEALTH_MEDIUM)
102 /// - 25-50%: Orange (HEALTH_LOW)
103 /// - <25%: Red (HEALTH_CRITICAL)
104 ///
105 eng::Color getHealthColor(float healthPercent) const;
106
107 ///
108 /// @brief Render a health bar at specified position
109 /// @param x X position (center of entity)
110 /// @param y Y position (center of entity)
111 /// @param currentHealth Current health value
112 /// @param maxHealth Maximum health value
113 /// @details Draws a layered health bar with:
114 /// 1. White border
115 /// 2. Dark gray background
116 /// 3. Colored health fill (proportional to current/max health)
117 ///
118 void drawHealthBar(float x, float y, float currentHealth, float maxHealth);
119 };
120
121} // namespace gme
This file contains the IRenderer class declaration.
This file contains the interface for systems.
This file contains the Registry class declaration.
Abstract class for system.
Definition ISystems.hpp:34
Class for managing entities and their components.
Definition Registry.hpp:25
ECS System that renders dynamic health bars above entities.
static constexpr float BAR_WIDTH
Width of the health bar in pixels.
~HealthBarSystem() override=default
Destructor.
static constexpr eng::Color HEALTH_HIGH
Green color for >75% health.
static constexpr eng::Color BORDER
White border color.
static constexpr float BAR_HEIGHT
Height of the health bar in pixels.
HealthBarSystem & operator=(const HealthBarSystem &)=delete
Deleted copy assignment operator (non-copyable)
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)
HealthBarSystem & operator=(HealthBarSystem &&)=delete
Deleted move assignment operator (non-movable)
HealthBarSystem(const HealthBarSystem &)=delete
Deleted copy constructor (non-copyable)
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.
HealthBarSystem(HealthBarSystem &&)=delete
Deleted move constructor (non-movable)
static constexpr eng::Color BACKGROUND
Dark gray background (semi-transparent)
const std::shared_ptr< eng::IRenderer > & m_renderer
Reference to the renderer for drawing.
static constexpr float BORDER_THICKNESS
Thickness of the border around the bar.