r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
weapon.cpp
Go to the documentation of this file.
1#include <ranges>
2
5#include "Utils/Common.hpp"
7
8void gme::WeaponSystem::update(ecs::Registry &registry, const float dt)
9{
10 if (m_fireCooldown > 0.0f)
11 {
12 m_fireCooldown -= dt;
13 }
14 auto playerEntities = registry.getAll<ecs::Player>();
15 if (playerEntities.empty())
16 return;
17 auto &[playerEntity, player] = *playerEntities.begin();
18 const auto *transform = registry.getComponent<ecs::Transform>(playerEntity);
19 auto *beamCharge = registry.getComponent<ecs::BeamCharge>(playerEntity);
20 if (!transform || !beamCharge)
21 return;
22 auto keyboardEntities = registry.getAll<ecs::KeyboardInput>();
23 bool spacePressed = false;
24 if (!keyboardEntities.empty())
25 {
26 auto &[keyboardEntity, keyboardInput] = *keyboardEntities.begin();
27 spacePressed = keyboardInput.space_pressed;
28 }
29 float projectileX = transform->x + utl::GameConfig::Player::SPRITE_WIDTH;
30 float projectileY = transform->y + utl::GameConfig::Player::SPRITE_HEIGHT / 2.0f;
31 if (spacePressed)
32 {
33 if (!m_isCharging)
34 {
35 m_isCharging = true;
36 }
37 beamCharge->current_charge += utl::GameConfig::Beam::CHARGE_RATE * dt;
38 if (beamCharge->current_charge > beamCharge->max_charge)
39 beamCharge->current_charge = beamCharge->max_charge;
40 if (beamCharge->current_charge < beamCharge->max_charge)
41 {
42 showLoadingAnimation(registry, playerEntity, transform);
43 }
44 else
45 {
46 hideLoadingAnimation(registry, playerEntity);
47 }
48 return;
49 }
50 else
51 {
52 if (m_isCharging)
53 {
54 m_isCharging = false;
55 hideLoadingAnimation(registry, playerEntity);
56 if (m_fireCooldown <= 0.0f)
57 {
58 if (const float chargeThreshold = beamCharge->max_charge * 0.5F;
59 beamCharge->current_charge >= chargeThreshold)
60 {
61 if (tryFireSupercharged(registry, projectileX, projectileY))
62 {
63 beamCharge->current_charge = 0.0f;
65 }
66 }
67 else
68 {
69 tryFireBasic(registry, projectileX, projectileY);
70 }
71 }
72 }
73 }
74}
75
76bool gme::WeaponSystem::tryFireBasic(ecs::Registry &registry, float x, float y)
77{
78 if (m_fireCooldown > 0.0f)
79 return false;
80
83 return true;
84}
85
87{
89 0.0f);
90 ensureSuperShotAudio(registry);
91 if (m_superShotAudioEntity != ecs::INVALID_ENTITY)
92 {
93 if (auto *audio = registry.getComponent<ecs::Audio>(m_superShotAudioEntity))
94 {
95 audio->play = true;
96 }
97 }
98 return true;
99}
100
102 const ecs::Transform *playerTransform)
103{
104 for (auto loadingEntities = registry.getAll<ecs::LoadingAnimation>();
105 const auto &entity : loadingEntities | std::views::keys)
106 {
107 if (auto *loadingTransform = registry.getComponent<ecs::Transform>(entity))
108 {
109 loadingTransform->x = playerTransform->x + utl::GameConfig::LoadingAnimation::OFFSET_X;
110 loadingTransform->y = playerTransform->y + utl::GameConfig::LoadingAnimation::OFFSET_Y;
111 return;
112 }
113 }
114
115 auto loadingEntity =
116 registry.createEntity()
117 .with<ecs::Transform>("loading_transform", playerTransform->x + utl::GameConfig::LoadingAnimation::OFFSET_X,
118 playerTransform->y + utl::GameConfig::LoadingAnimation::OFFSET_Y, 0.0f)
119 .with<ecs::Rect>("loading_rect", 0.0f, 0.0f,
122 .with<ecs::Scale>("loading_scale", 1.0f, 1.0f)
123 .with<ecs::Texture>("loading_texture", utl::Path::Texture::TEXTURE_SHOOT_LOADING)
129 .build();
130}
131
133{
134 auto loadingEntities = registry.getAll<ecs::LoadingAnimation>();
135 std::vector<ecs::Entity> toRemove;
136
137 for (const auto &entity : loadingEntities | std::views::keys)
138 {
139 toRemove.push_back(entity);
140 }
141
142 for (const auto entity : toRemove)
143 {
144 if (registry.hasComponent<ecs::Transform>(entity))
145 {
146 registry.removeComponent<ecs::Transform>(entity);
147 }
148 if (registry.hasComponent<ecs::Rect>(entity))
149 {
150 registry.removeComponent<ecs::Rect>(entity);
151 }
152 if (registry.hasComponent<ecs::Scale>(entity))
153 {
154 registry.removeComponent<ecs::Scale>(entity);
155 }
156 if (registry.hasComponent<ecs::Texture>(entity))
157 {
158 registry.removeComponent<ecs::Texture>(entity);
159 }
160 if (registry.hasComponent<ecs::LoadingAnimation>(entity))
161 {
162 registry.removeComponent<ecs::LoadingAnimation>(entity);
163 }
164 }
165}
166
168{
169 if (m_superShotAudioEntity != ecs::INVALID_ENTITY && registry.hasComponent<ecs::Audio>(m_superShotAudioEntity))
170 {
171 return;
172 }
173
174 m_superShotAudioEntity =
175 registry.createEntity()
176 .with<ecs::Audio>("player_super_shot", utl::Path::Audio::AUDIO_SUPERCHARGED_SHOT, 2.0F, false, false)
177 .build();
178}
Configuration constants for the multiplayer game.
Manages projectile creation and configuration.
Handles weapon firing logic.
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
static ecs::Entity createSuperchargedProjectile(ecs::Registry &registry, float x, float y, float velocityX, float velocityY)
Create a supercharged projectile.
static ecs::Entity createBasicProjectile(ecs::Registry &registry, float x, float y, float velocityX, float velocityY)
Create a basic projectile.
static void hideLoadingAnimation(ecs::Registry &registry, ecs::Entity playerEntity)
Hide/remove the charging animation.
Definition weapon.cpp:91
static void showLoadingAnimation(ecs::Registry &registry, ecs::Entity playerEntity, const ecs::Transform *playerTransform)
Display charging/loading animation in front of player.
Definition weapon.cpp:62
bool m_isCharging
Whether weapon is currently charging.
Definition Weapon.hpp:90
void ensureSuperShotAudio(ecs::Registry &registry)
Ensure super shot audio entity exists.
Definition weapon.cpp:126
void update(ecs::Registry &registry, float dt) override
Update weapon system (called each frame)
Definition weapon.cpp:8
float m_fireCooldown
Remaining cooldown time before next shot (seconds)
Definition Weapon.hpp:89
bool tryFireBasic(ecs::Registry &registry, float x, float y)
Try to fire basic projectile.
Definition weapon.cpp:76
bool tryFireSupercharged(ecs::Registry &registry, float x, float y)
Try to fire supercharged projectile.
Definition weapon.cpp:86
This file contains common definitions and constants.
std::uint32_t Entity
Definition Entity.hpp:13
constexpr Entity INVALID_ENTITY
Definition Entity.hpp:14
constexpr float CHARGE_RATE
constexpr float SPRITE_HEIGHT
constexpr float SPRITE_WIDTH
constexpr auto AUDIO_SUPERCHARGED_SHOT
Definition Common.hpp:90
constexpr auto TEXTURE_SHOOT_LOADING
Definition Common.hpp:117