Skip to main content

HealthComponent.ixx File

Component for tracking entity health. More...

Included Headers

#include <algorithm> #include <cassert> #include <helios.engine.mechanics.health.types>

Namespaces Index

namespacehelios
namespaceengine

Main engine module aggregating core infrastructure and game systems. More...

namespacemechanics

High-level gameplay systems and components for game logic. More...

namespacehealth

Health management system for game entities. More...

namespacecomponents

Health-related ECS components. More...

Classes Index

classHealthComponent

Component for tracking entity health and damage. More...

Description

Component for tracking entity health.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file HealthComponent.ixx
3 * @brief Component for tracking entity health.
4 */
5module;
6
7#include <algorithm>
8#include <cassert>
9
10export module helios.engine.mechanics.health.components.HealthComponent;
11
12
13import helios.engine.mechanics.health.types;
14
16
18
19
20 /**
21 * @brief Component for tracking entity health and damage.
22 *
23 * Manages current and maximum health values for an entity. Provides
24 * methods for taking damage, healing, and checking alive status.
25 */
27
28 private:
29
30 /**
31 * @brief Maximum health value.
32 */
33 float maxHealth_{};
34
35
36 /**
37 * @brief Current health value.
38 */
39 float health_{};
40
41 /**
42 * @brief Whether this component is enabled.
43 */
44 bool isEnabled_ = true;
45
46
47
48 /**
49 * @brief Flags controlling the response when health reaches zero.
50 */
52
53 /**
54 * @brief True if the health value was modified since the last clear.
55 */
56 bool isDirty_ = true;
57
58 public:
59
60 /**
61 * @brief Checks whether this component is enabled.
62 *
63 * @return True if enabled, false otherwise.
64 */
65 [[nodiscard]] bool isEnabled() const noexcept {
66 return isEnabled_;
67 }
68
69 /**
70 * @brief Enables this component.
71 */
72 void enable() noexcept {
73 isEnabled_ = true;
74 }
75
76 /**
77 * @brief Disables this component.
78 */
79 void disable() noexcept {
80 isEnabled_ = false;
81 }
82
83 HealthComponent() = default;
84
85 /**
86 * @brief Constructs a HealthComponent with the given maximum health.
87 *
88 * @param maxHealth The maximum health value.
89 */
90 explicit HealthComponent(const float maxHealth) : maxHealth_(maxHealth) {};
91
92 /**
93 * @brief Copy constructor.
94 *
95 * @param other The component to copy from.
96 */
98 :
99 maxHealth_(other.maxHealth_),
100 healthDepletedBehavior_(other.healthDepletedBehavior_)
101 {
102 assert(maxHealth_ != 0.0f && "max health must not be 0");
103 isDirty_ = true;
104 }
105
107 HealthComponent(HealthComponent&&) noexcept = default;
108 HealthComponent& operator=(HealthComponent&&) noexcept = default;
109
110 /**
111 * @brief Sets the health depletion response behaviour.
112 *
113 * @param behavior Bitmask of HealthDepletedBehavior flags.
114 */
115 void setHealthDepletedBehavior(const HealthDepletedBehavior behavior) noexcept {
116 healthDepletedBehavior_ = behavior;
117 }
118
119 /**
120 * @brief Returns the health depletion response behaviour.
121 *
122 * @return Current HealthDepletedBehavior flags.
123 */
124 [[nodiscard]] HealthDepletedBehavior healthDepletedBehavior() const noexcept {
125 return healthDepletedBehavior_;
126 }
127
128 /**
129 * @brief Applies damage to this entity.
130 *
131 * Health is clamped to a minimum of 0
132 *
133 * @param damage The amount of damage to apply.
134 */
135 void takeDamage(const float damage) noexcept {
136 health_ = std::max(0.0f, health_ - damage);
137 isDirty_ = true;
138 }
139
140 /**
141 * @brief Heals this entity.
142 *
143 * Health is clamped to the maximum health value.
144 *
145 * @param amount The amount to heal.
146 */
147 void heal(const float amount) noexcept {
148 health_ = std::min(maxHealth_, health_ + amount);
149 isDirty_ = true;
150 }
151
152 /**
153 * @brief Sets the maximum health value.
154 *
155 * @param maxHealth The new maximum health.
156 */
157 void setMaxHealth(const float maxHealth) noexcept {
158 maxHealth_ = maxHealth;
159 isDirty_ = true;
160 }
161
162 /**
163 * @brief Checks if the entity is alive.
164 *
165 * @return True if health is greater than 0
166 */
167 [[nodiscard]] bool isAlive() const noexcept {
168 return health_ > 0.0f;
169 }
170
171 /**
172 * @brief Returns the current health value.
173 *
174 * @return The current health.
175 */
176 [[nodiscard]] float health() const noexcept {
177 return health_;
178 }
179
180 /**
181 * @brief Returns the maximum health value.
182 *
183 * @return The maximum health.
184 */
185 [[nodiscard]] float maxHealth() const noexcept {
186 return maxHealth_;
187 }
188
189 /**
190 * @brief Checks whether the health value has been modified.
191 *
192 * @return True if modified since the last clearDirty() call.
193 */
194 [[nodiscard]] bool isDirty() const noexcept {
195 return isDirty_;
196 }
197
198 /**
199 * @brief Resets the dirty flag.
200 */
201 void clearDirty() noexcept {
202 isDirty_ = false;
203 }
204
205
206 /**
207 * @brief Resets health to maximum.
208 */
209 void reset() {
210 health_ = maxHealth_;
211 isDirty_ = true;
212 }
213
214 /**
215 * @brief Called when this entity is acquired from a pool.
216 *
217 * @details Resets health to maximum value.
218 */
219 void onAcquire() noexcept {
220 reset();
221 }
222
223 /**
224 * @brief Called when this entity is released back to a pool.
225 *
226 * @details Resets health to maximum value.
227 */
228 void onRelease() noexcept {
229 reset();
230 }
231
232
233
234
235 };
236
237
238}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.