r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
weapon.cpp
Go to the documentation of this file.
1#include <algorithm>
2#include <ranges>
3
5#include "Utils/Common.hpp"
7
8void gme::WeaponSystem::update(ecs::Registry &registry, const float dt)
9{
10
11 if (m_fireCooldown > 0.0f)
12 {
13 m_fireCooldown -= dt;
14 }
15 auto playerEntities = registry.getAll<ecs::Player>();
16 if (playerEntities.empty())
17 {
18 return;
19 }
20 auto &[playerEntity, player] = *playerEntities.begin();
21 const auto *transform = registry.getComponent<ecs::Transform>(playerEntity);
22 auto *beamCharge = registry.getComponent<ecs::BeamCharge>(playerEntity);
23 if ((transform == nullptr) || (beamCharge == nullptr))
24 {
25 return;
26 }
27 auto keyboardEntities = registry.getAll<ecs::KeyboardInput>();
28 bool spacePressed = false;
29 if (!keyboardEntities.empty())
30 {
31 auto &[keyboardEntity, keyboardInput] = *keyboardEntities.begin();
32 spacePressed = keyboardInput.space_pressed;
33 }
34
35 if (spacePressed)
36 {
37 if (!m_isCharging)
38 {
39 m_isCharging = true;
40 }
41 beamCharge->current_charge += utl::GameConfig::Beam::CHARGE_RATE * dt;
42 beamCharge->current_charge = std::min(beamCharge->current_charge, beamCharge->max_charge);
43 if (beamCharge->current_charge < beamCharge->max_charge)
44 {
45 showLoadingAnimation(registry, playerEntity, transform);
46 }
47 else
48 {
49 hideLoadingAnimation(registry, playerEntity);
50 }
51 return;
52 }
53
54 if (m_isCharging)
55 {
56 m_isCharging = false;
57 hideLoadingAnimation(registry, playerEntity);
58 beamCharge->current_charge = 0.0f;
59 }
60}
61
63 const ecs::Transform *playerTransform)
64{
65 for (auto loadingEntities = registry.getAll<ecs::LoadingAnimation>();
66 const auto &entity : loadingEntities | std::views::keys)
67 {
68 if (auto *loadingTransform = registry.getComponent<ecs::Transform>(entity))
69 {
70 loadingTransform->x = playerTransform->x + utl::GameConfig::LoadingAnimation::OFFSET_X;
71 loadingTransform->y = playerTransform->y + utl::GameConfig::LoadingAnimation::OFFSET_Y;
72 return;
73 }
74 }
75
76 (void)registry.createEntity()
77 .with<ecs::Transform>("loading_transform", playerTransform->x + utl::GameConfig::LoadingAnimation::OFFSET_X,
78 playerTransform->y + utl::GameConfig::LoadingAnimation::OFFSET_Y, 0.0f)
79 .with<ecs::Rect>("loading_rect", 0.0f, 0.0f, static_cast<int>(utl::GameConfig::LoadingAnimation::SPRITE_WIDTH),
81 .with<ecs::Scale>("loading_scale", 1.0f, 1.0f)
82 .with<ecs::Texture>("loading_texture", utl::Path::Texture::TEXTURE_SHOOT_LOADING)
88 .build();
89}
90
92{
93 auto loadingEntities = registry.getAll<ecs::LoadingAnimation>();
94 std::vector<ecs::Entity> toRemove;
95
96 for (const auto &entity : loadingEntities | std::views::keys)
97 {
98 toRemove.push_back(entity);
99 }
100
101 for (const auto entity : toRemove)
102 {
103 if (registry.hasComponent<ecs::Transform>(entity))
104 {
105 registry.removeComponent<ecs::Transform>(entity);
106 }
107 if (registry.hasComponent<ecs::Rect>(entity))
108 {
109 registry.removeComponent<ecs::Rect>(entity);
110 }
111 if (registry.hasComponent<ecs::Scale>(entity))
112 {
113 registry.removeComponent<ecs::Scale>(entity);
114 }
115 if (registry.hasComponent<ecs::Texture>(entity))
116 {
117 registry.removeComponent<ecs::Texture>(entity);
118 }
119 if (registry.hasComponent<ecs::LoadingAnimation>(entity))
120 {
121 registry.removeComponent<ecs::LoadingAnimation>(entity);
122 }
123 }
124}
125
127{
128 if (m_superShotAudioEntity != ecs::INVALID_ENTITY && registry.hasComponent<ecs::Audio>(m_superShotAudioEntity))
129 {
130 return;
131 }
132
133 m_superShotAudioEntity =
134 registry.createEntity()
135 .with<ecs::Audio>("player_super_shot", utl::Path::Audio::AUDIO_SUPERCHARGED_SHOT, 2.0F, false, false)
136 .build();
137}
Configuration constants for the multiplayer game.
Weapon system for R-Type multiplayer client.
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 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
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 auto AUDIO_SUPERCHARGED_SHOT
Definition Common.hpp:90
constexpr auto TEXTURE_SHOOT_LOADING
Definition Common.hpp:117