R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
Buff.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** RTYPE
4** File description:
5** Buff - Component for temporary and permanent buffs
6*/
7
8#pragma once
9
10#include <algorithm>
11#include <vector>
12#include "IComponent.hpp"
13
14namespace ecs {
15
20 enum class BuffType {
21 // Temporary buffs (duration-based)
25 Shield,
27
28 // Permanent buffs (modify behavior permanently)
29 MultiShot,
35 };
36
41 struct BuffInstance {
43 float duration;
44 float value;
46
47 BuffInstance(BuffType t, float dur, float val, bool perm = false)
49 };
50
58 class Buff : public IComponent {
59 public:
63 Buff() = default;
64
71 Buff(BuffType type, float duration, float value) { addBuff(type, duration, value); }
72
73 ~Buff() override = default;
74
81 void addBuff(BuffType type, float duration, float value) {
82 bool isPermanent = (duration <= 0.0f);
83
84 // Check if buff already exists
85 for (auto &buff : _buffs) {
86 if (buff.type == type) {
87 // Refresh duration and update value
88 buff.duration = duration;
89 buff.value = value;
90 buff.isPermanent = isPermanent;
91 return;
92 }
93 }
94
95 // Add new buff
96 _buffs.emplace_back(type, duration, value, isPermanent);
97 }
98
103 void removeBuff(BuffType type) {
104 _buffs.erase(std::remove_if(_buffs.begin(), _buffs.end(),
105 [type](const BuffInstance &b) { return b.type == type; }),
106 _buffs.end());
107 }
108
114 bool hasBuff(BuffType type) const {
115 for (const auto &buff : _buffs) {
116 if (buff.type == type)
117 return true;
118 }
119 return false;
120 }
121
127 float getBuffValue(BuffType type) const {
128 for (const auto &buff : _buffs) {
129 if (buff.type == type)
130 return buff.value;
131 }
132 return 1.0f;
133 }
134
139 const std::vector<BuffInstance> &getBuffs() const { return _buffs; }
140
145 std::vector<BuffInstance> &getBuffsMutable() { return _buffs; }
146
151 bool hasAnyBuffs() const { return !_buffs.empty(); }
152
156 void clearAllBuffs() { _buffs.clear(); }
157
162 ComponentType getType() const override { return getComponentType<Buff>(); }
163
164 private:
165 std::vector<BuffInstance> _buffs;
166 };
167
168} // namespace ecs
Component managing active buffs on an entity.
Definition Buff.hpp:58
bool hasBuff(BuffType type) const
Check if entity has a specific buff.
Definition Buff.hpp:114
float getBuffValue(BuffType type) const
Get buff value for a specific type.
Definition Buff.hpp:127
ComponentType getType() const override
Get the component type ID.
Definition Buff.hpp:162
void removeBuff(BuffType type)
Remove a specific buff.
Definition Buff.hpp:103
std::vector< BuffInstance > & getBuffsMutable()
Get mutable reference to buffs (for system updates)
Definition Buff.hpp:145
std::vector< BuffInstance > _buffs
Active buffs on this entity.
Definition Buff.hpp:165
~Buff() override=default
void addBuff(BuffType type, float duration, float value)
Add a new buff.
Definition Buff.hpp:81
void clearAllBuffs()
Clear all buffs.
Definition Buff.hpp:156
const std::vector< BuffInstance > & getBuffs() const
Get all active buffs.
Definition Buff.hpp:139
Buff()=default
Default constructor.
Buff(BuffType type, float duration, float value)
Constructor with initial buff.
Definition Buff.hpp:71
bool hasAnyBuffs() const
Check if any buffs are active.
Definition Buff.hpp:151
Base interface for all ECS components.
Maximum number of distinct component types supported by the Registry.
Definition GameLogic.hpp:26
BuffType
Types of buffs that can be applied to entities.
Definition Buff.hpp:20
@ PiercingShot
Projectiles pierce through enemies.
@ TripleShot
Fire three projectiles at once.
@ DamageBoost
Increases weapon damage.
@ HomingShot
Projectiles home towards enemies.
@ FireRateBoost
Increases fire rate.
@ MaxHealthIncrease
Permanently increase max health.
@ Shield
Temporary invincibility.
@ HealthRegen
Regenerates health over time.
@ SpeedBoost
Increases movement speed.
@ MultiShot
Shoot in multiple directions.
@ DoubleShot
Fire two projectiles at once.
std::size_t ComponentType
Type alias for component identification.
Individual buff with its properties.
Definition Buff.hpp:41
BuffInstance(BuffType t, float dur, float val, bool perm=false)
Definition Buff.hpp:47
bool isPermanent
True if buff never expires.
Definition Buff.hpp:45
float duration
Remaining duration (0.0f = permanent)
Definition Buff.hpp:43
BuffType type
Type of buff.
Definition Buff.hpp:42
float value
Buff value (multiplier or absolute value)
Definition Buff.hpp:44