r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
weapon.cpp
Go to the documentation of this file.
1///
2/// @file WeaponSystem.cpp
3/// @brief Implementation of WeaponSystem
4/// @namespace cli
5///
6
8#include "Client/Common.hpp"
10
11namespace cli
12{
13 void WeaponSystem::update(ecs::Registry &registry, float dt, bool spacePressed)
14 {
15 using namespace GameConfig::Projectile;
16 using namespace GameConfig::Beam;
17
18 // Update cooldowns
19 if (m_fireCooldown > 0.0f)
20 m_fireCooldown -= dt;
21
22 // Get player entity and position
23 auto playerEntities = registry.getAll<ecs::Player>();
24 if (playerEntities.empty())
25 return;
26
27 auto &[playerEntity, player] = *playerEntities.begin();
28 auto *transform = registry.getComponent<ecs::Transform>(playerEntity);
29 auto *beamCharge = registry.getComponent<ecs::BeamCharge>(playerEntity);
30 if (!transform || !beamCharge)
31 return;
32
33 float projectileX = transform->x + GameConfig::Player::SPRITE_WIDTH;
34 float projectileY = transform->y + GameConfig::Player::SPRITE_HEIGHT / 2.0f;
35
36 if (spacePressed)
37 {
38 // Commencer le chargement si ce n'était pas déjà le cas
39 if (!m_isCharging)
40 {
41 m_isCharging = true;
42 }
43
44 // Charger la barre de Beam
45 beamCharge->current_charge += CHARGE_RATE * dt;
46 if (beamCharge->current_charge > beamCharge->max_charge)
47 beamCharge->current_charge = beamCharge->max_charge;
48
49 // Afficher l'animation de chargement si on charge
50 if (beamCharge->current_charge < beamCharge->max_charge)
51 {
52 showLoadingAnimation(registry, playerEntity, transform);
53 }
54 else
55 {
56 hideLoadingAnimation(registry, playerEntity);
57 }
58
59 // PENDANT LE CHARGEMENT : NE RIEN TIRER DU TOUT
60 return; // Sortir de la fonction sans rien tirer
61 }
62 else
63 {
64 // Si on était en train de charger et qu'on relâche espace
65 if (m_isCharging)
66 {
67 m_isCharging = false;
68
69 // Cacher l'animation de chargement
70 hideLoadingAnimation(registry, playerEntity);
71
72 // Quand on relâche espace, vérifier si on peut tirer
73 if (m_fireCooldown <= 0.0f)
74 {
75 // Si on a au moins 50% de charge, tirer un supercharged
76 float chargeThreshold = beamCharge->max_charge * 0.5f; // 50% minimum
77 if (beamCharge->current_charge >= chargeThreshold)
78 {
79 if (tryFireSupercharged(registry, projectileX, projectileY))
80 {
81 beamCharge->current_charge = 0.0f; // Consommer toute la charge
82 m_fireCooldown = Supercharged::FIRE_COOLDOWN;
83 }
84 }
85 else
86 {
87 // Si pas assez de charge, tirer un basic
88 tryFireBasic(registry, projectileX, projectileY);
89 }
90 }
91 }
92 }
93 }
94
96 {
97 m_fireCooldown = 0.0f;
98 m_isCharging = false;
99 }
100
101 bool WeaponSystem::tryFireBasic(ecs::Registry &registry, float x, float y)
102 {
103 using namespace GameConfig::Projectile;
104
105 if (m_fireCooldown > 0.0f)
106 return false;
107
108 ProjectileManager::createBasicProjectile(registry, x, y, Basic::SPEED, 0.0f);
109 m_fireCooldown = Basic::FIRE_COOLDOWN;
110 return true;
111 }
112
113 bool WeaponSystem::tryFireSupercharged(ecs::Registry &registry, float x, float y)
114 {
115 using namespace GameConfig::Projectile;
116
117 ProjectileManager::createSuperchargedProjectile(registry, x, y, Supercharged::SPEED, 0.0f);
118 return true;
119 }
120
122 const ecs::Transform *playerTransform)
123 {
124 using namespace GameConfig::LoadingAnimation;
125
126 // Chercher s'il y a déjà une animation de chargement
127 auto loadingEntities = registry.getAll<ecs::LoadingAnimation>();
128 for (auto &[entity, animation] : loadingEntities)
129 {
130 auto *loadingTransform = registry.getComponent<ecs::Transform>(entity);
131 if (loadingTransform)
132 {
133 // Mettre à jour la position
134 loadingTransform->x = playerTransform->x + OFFSET_X;
135 loadingTransform->y = playerTransform->y + OFFSET_Y;
136 return; // Animation déjà présente
137 }
138 }
139
140 // Créer une nouvelle animation de chargement
141 auto loadingEntity =
142 registry.createEntity()
143 .with<ecs::Transform>("loading_transform", playerTransform->x + OFFSET_X, playerTransform->y + OFFSET_Y,
144 0.0f)
145 .with<ecs::Rect>("loading_rect", 0.0f, 0.0f, static_cast<int>(SPRITE_WIDTH),
146 static_cast<int>(SPRITE_HEIGHT))
147 .with<ecs::Scale>("loading_scale", 1.0f, 1.0f)
148 .with<ecs::Texture>("loading_texture", Path::Texture::TEXTURE_SHOOT_LOADING)
149 .with<ecs::LoadingAnimation>("loading_animation", 0, ANIMATION_FRAMES, ANIMATION_DURATION, 0.0f,
150 SPRITE_WIDTH, SPRITE_HEIGHT, ANIMATION_FRAMES)
151 .build();
152 }
153
155 {
156 // Supprimer toutes les animations de chargement
157 auto loadingEntities = registry.getAll<ecs::LoadingAnimation>();
158 std::vector<ecs::Entity> toRemove;
159
160 for (auto &[entity, animation] : loadingEntities)
161 {
162 toRemove.push_back(entity);
163 }
164
165 for (auto entity : toRemove)
166 {
167 if (registry.hasComponent<ecs::Transform>(entity))
168 registry.removeComponent<ecs::Transform>(entity);
169 if (registry.hasComponent<ecs::Rect>(entity))
170 registry.removeComponent<ecs::Rect>(entity);
171 if (registry.hasComponent<ecs::Scale>(entity))
172 registry.removeComponent<ecs::Scale>(entity);
173 if (registry.hasComponent<ecs::Texture>(entity))
174 registry.removeComponent<ecs::Texture>(entity);
175 if (registry.hasComponent<ecs::LoadingAnimation>(entity))
176 registry.removeComponent<ecs::LoadingAnimation>(entity);
177 }
178 }
179} // namespace cli
Manages projectile creation and configuration.
static ecs::Entity createBasicProjectile(ecs::Registry &registry, float x, float y, float velocityX, float velocityY)
Create a basic projectile.
static ecs::Entity createSuperchargedProjectile(ecs::Registry &registry, float x, float y, float velocityX, float velocityY)
Create a supercharged projectile.
void reset()
Reset weapon state.
Definition weapon.cpp:95
void update(ecs::Registry &registry, float dt, bool spacePressed)
Update weapon system.
Definition weapon.cpp:13
bool tryFireSupercharged(ecs::Registry &registry, float x, float y)
Try to fire supercharged projectile.
Definition weapon.cpp:113
void hideLoadingAnimation(ecs::Registry &registry, ecs::Entity playerEntity)
Hide loading animation.
Definition weapon.cpp:154
void showLoadingAnimation(ecs::Registry &registry, ecs::Entity playerEntity, const ecs::Transform *playerTransform)
Show loading animation in front of the player.
Definition weapon.cpp:121
float m_fireCooldown
Definition Weapon.hpp:47
bool tryFireBasic(ecs::Registry &registry, float x, float y)
Try to fire basic projectile.
Definition weapon.cpp:101
EntityBuilder & with(Args &&...args)
Definition Registry.hpp:40
Class for managing entities and their components.
Definition Registry.hpp:25
EntityBuilder createEntity()
Definition Registry.hpp:53
bool hasComponent(Entity e)
Definition Registry.hpp:79
std::unordered_map< Entity, T > & getAll()
Definition Registry.hpp:77
T * getComponent(Entity e)
Definition Registry.hpp:71
void removeComponent(Entity e)
Definition Registry.hpp:85
This file contains common definitions and constants.
constexpr float SPRITE_WIDTH
constexpr float SPRITE_HEIGHT
constexpr auto TEXTURE_SHOOT_LOADING
Definition Common.hpp:66
std::uint32_t Entity
Definition Entity.hpp:13