R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
Health.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** RTYPE
4** File description:
5** Health
6*/
7
8#pragma once
9
10#include "IComponent.hpp"
11
12namespace ecs {
20 class Health : public IComponent {
21 public:
27 explicit Health(int maxHealth)
28 : _current(maxHealth), _max(maxHealth), _invincible(false), _timer(0.0f) {}
29
35 Health(int currentHealth, int maxHealth)
36 : _current(currentHealth), _max(maxHealth), _invincible(false), _timer(0.0f) {}
37
38 ~Health() override = default;
39
44 int getCurrentHealth() const { return _current; }
45
50 int getMaxHealth() const { return _max; }
51
56 bool isInvincible() const { return _invincible; }
57
62 float getInvincibilityTimer() const { return _timer; }
63
68 void setMaxHealth(int health) { _max = health; }
69
74 void setCurrentHealth(int health) { _current = health; }
75
80 void setInvincible(bool invincible) { _invincible = invincible; }
81
86 void setInvincibilityTimer(float timer) { _timer = timer; }
87
94 bool takeDamage(int amount) {
95 if (_invincible || amount <= 0) {
96 return false;
97 }
98 _current -= amount;
99 if (_current < 0) {
100 _current = 0;
101 }
102 return true;
103 }
104
110 void heal(int amount) {
111 if (amount <= 0) {
112 return;
113 }
114 _current += amount;
115 if (_current > _max) {
116 _current = _max;
117 }
118 }
119
124 bool isDead() const { return _current <= 0; }
125
130 ComponentType getType() const override { return getComponentType<Health>(); }
131
132 private:
134 int _max;
136 float _timer;
137 };
138} // namespace ecs
Component representing entity health and invincibility.
Definition Health.hpp:20
float _timer
Invincibility duration timer in seconds.
Definition Health.hpp:136
bool takeDamage(int amount)
Apply damage to the entity. Respects invincibility frames - no damage taken if invincible.
Definition Health.hpp:94
int _max
Maximum health points.
Definition Health.hpp:134
int getCurrentHealth() const
Get current health points.
Definition Health.hpp:44
bool isInvincible() const
Check if entity is invincible.
Definition Health.hpp:56
int _current
Current health points.
Definition Health.hpp:133
void setInvincible(bool invincible)
Set invincibility state.
Definition Health.hpp:80
float getInvincibilityTimer() const
Get remaining invincibility time.
Definition Health.hpp:62
bool _invincible
Invincibility state.
Definition Health.hpp:135
ComponentType getType() const override
Get the component type ID.
Definition Health.hpp:130
void setInvincibilityTimer(float timer)
Set invincibility timer.
Definition Health.hpp:86
Health(int maxHealth)
Constructor with maximum health only. Sets current health equal to max health.
Definition Health.hpp:27
Health(int currentHealth, int maxHealth)
Constructor with current and maximum health.
Definition Health.hpp:35
~Health() override=default
void heal(int amount)
Restore health points. Cannot exceed maximum health.
Definition Health.hpp:110
void setCurrentHealth(int health)
Set current health.
Definition Health.hpp:74
int getMaxHealth() const
Get maximum health points.
Definition Health.hpp:50
void setMaxHealth(int health)
Set maximum health.
Definition Health.hpp:68
bool isDead() const
Check if entity is dead (health <= 0).
Definition Health.hpp:124
Base interface for all ECS components.
Maximum number of distinct component types supported by the Registry.
Definition GameLogic.hpp:26
std::size_t ComponentType
Type alias for component identification.