Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
HealthComponent.h
Go to the documentation of this file.
1/**
2 * @file HealthComponent.h
3 * @ingroup mnd_components
4 * @brief Universal HP sink: damageable + killable game objects.
5 *
6 * Any GameObject that can take damage (player, goblins, breakables) carries
7 * a HealthComponent. The DamageSystem looks for this component on the victim
8 * GameObject when applying a DamageEvent. Death emits an OnDeath callback so
9 * gameplay code (loot drops, ragdoll, score) can react without coupling to
10 * the damage source.
11 */
12
13#pragma once
14
15#include <functional>
16
17#include "scene/Component.h"
18
19namespace mnd
20{
21
23{
24 GameObject *attacker = nullptr;
25 float amount = 0.0F;
28};
29
31{
33 public:
34 using DamageFn = std::function<void(const DamageInfo &)>;
35 using DeathFn = std::function<void(const DamageInfo &)>;
36
37 void Update(f32 deltaTime) override;
38 void LoadProperties(const nlohmann::json &json) override;
39
40 /// Apply damage. Routed through DamageSystem::Apply, do not call directly.
41 void ApplyDamage(const DamageInfo &info);
42
43 [[nodiscard]] int GetMax() const { return m_max; }
44 [[nodiscard]] int GetCurrent() const { return m_current; }
45 [[nodiscard]] bool IsDead() const { return m_current <= 0; }
46 [[nodiscard]] bool IsInvulnerable() const { return m_invulnerable; }
47
48 void SetMax(int hp);
49 void SetCurrent(int hp);
50 void SetInvulnerable(bool inv) { m_invulnerable = inv; }
51 void SetDestroyOnDeath(bool b) { m_destroyOnDeath = b; }
52
53 /// Heal up to max. No-op if dead (use Revive for that).
54 void Heal(int amount);
55
56 /// Reset to full HP and clear dead state.
57 void Revive();
58
59 /// Subscribe to damage events. Listeners fire on every non-zero damage tick.
60 void OnDamage(DamageFn fn) { m_onDamage = std::move(fn); }
61 /// Subscribe to death event. Fires once per transition from alive→dead.
62 void OnDeath(DeathFn fn) { m_onDeath = std::move(fn); }
63
64 /// Toggle on-screen 2D bar.
65 void SetShowBar(bool show) { m_showBar = show; }
66 [[nodiscard]] bool ShowsBar() const { return m_showBar; }
67
68 private:
69 void DrawHealthBar();
70
71 int m_max = 100;
72 int m_current = 100;
73 bool m_invulnerable = false;
74 bool m_destroyOnDeath = false;
75 DamageFn m_onDamage;
76 DeathFn m_onDeath;
77
78 // 2D health bar overlay.
79 bool m_showBar = true;
80 bool m_hideAtFull = false;
81 bool m_showText = true;
82 f32 m_barOffsetY = 1.1F; ///< World-space offset above owner origin.
83 f32 m_barWorldWidth = 1.6F; ///< World-space reference width — scales with camera distance.
84 f32 m_barAspect = 0.18F; ///< Height = width * aspect.
85 f32 m_minScreenWidth = 32.0F; ///< Pixel floor so distant bars stay readable.
86 f32 m_maxScreenWidth = 360.0F; ///< Pixel ceiling so close-up bars don't dominate.
87 f32 m_displayedFill = 1.0F; ///< Smoothed [0,1] fill ratio.
88 f32 m_barLerpSpeed = 8.0F; ///< Per-second lerp toward target ratio.
89 vec4 m_shadowColor = vec4(0.0F, 0.0F, 0.0F, 0.55F);
90 vec4 m_borderColor = vec4(0.04F, 0.05F, 0.07F, 0.95F);
91 vec4 m_bgColorTop = vec4(0.10F, 0.11F, 0.13F, 0.92F);
92 vec4 m_bgColorBottom = vec4(0.18F, 0.20F, 0.23F, 0.92F);
93 vec4 m_fillColorHigh = vec4(0.30F, 0.92F, 0.45F, 1.0F);
94 vec4 m_fillColorMid = vec4(0.98F, 0.82F, 0.25F, 1.0F);
95 vec4 m_fillColorLow = vec4(0.95F, 0.25F, 0.25F, 1.0F);
96 vec4 m_glossColor = vec4(1.0F, 1.0F, 1.0F, 0.22F);
97 vec4 m_textColor = vec4(1.0F, 1.0F, 1.0F, 0.95F);
98 vec4 m_textShadowColor = vec4(0.0F, 0.0F, 0.0F, 0.85F);
99};
100
101} // namespace mnd
Node in the scene graph: a named transform that owns components and children.
Definition GameObject.h:91
void Update(f32 deltaTime) override
void LoadProperties(const nlohmann::json &json) override
void ApplyDamage(const DamageInfo &info)
Apply damage. Routed through DamageSystem::Apply, do not call directly.
std::function< void(const DamageInfo &)> DeathFn
void SetShowBar(bool show)
Toggle on-screen 2D bar.
void OnDamage(DamageFn fn)
Subscribe to damage events. Listeners fire on every non-zero damage tick.
void SetInvulnerable(bool inv)
std::function< void(const DamageInfo &)> DamageFn
void OnDeath(DeathFn fn)
Subscribe to death event. Fires once per transition from alive→dead.
void SetDestroyOnDeath(bool b)
void Revive()
Reset to full HP and clear dead state.
void Heal(int amount)
Heal up to max. No-op if dead (use Revive for that).
bool IsInvulnerable() const
#define COMPONENT(ComponentClass)
Definition Component.h:105
glm::vec4 vec4
4-component float vector (e.g. RGBA colour, homogeneous coords).
Definition Types.h:52
glm::vec3 vec3
3-component float vector (e.g. world position, RGB colour, normals).
Definition Types.h:51
float f32
32-bit IEEE float — the standard GL scalar type.
Definition Types.h:40
GameObject * attacker