r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
gameOver.cpp
Go to the documentation of this file.
2#include "ECS/Component.hpp"
3#include "Utils/Common.hpp"
4#include <cmath>
5
6static constexpr eng::Color RED_COLOR = {.r = 255U, .g = 50U, .b = 50U, .a = 255U};
7static constexpr eng::Color WHITE_COLOR = {.r = 255U, .g = 255U, .b = 255U, .a = 255U};
8
9namespace gme
10{
11 GameOverScene::GameOverScene(const eng::id assignedId, const std::shared_ptr<eng::IRenderer> &renderer)
12 : AScene(assignedId), m_renderer(renderer)
13 {
14 auto &registry = AScene::getRegistry();
15
16 registry.onComponentAdded(
17 [&renderer, &registry](const ecs::Entity e, const std::type_info &type)
18 {
19 const auto *colorComp = registry.getComponent<ecs::Color>(e);
20 const auto *fontComp = registry.getComponent<ecs::Font>(e);
21 const auto *textComp = registry.getComponent<ecs::Text>(e);
22 const auto *transform = registry.getComponent<ecs::Transform>(e);
23
24 if (type == typeid(ecs::Text))
25 {
26 if (textComp && transform && fontComp)
27 {
28 renderer->createFont(fontComp->id, fontComp->path);
29 renderer->createText(
30 {.font_name = fontComp->id,
31 .color = {.r = colorComp->r, .g = colorComp->g, .b = colorComp->b, .a = colorComp->a},
32 .content = textComp->content,
33 .size = textComp->font_size,
34 .x = transform->x,
35 .y = transform->y,
36 .name = textComp->id});
37 }
38 }
39 });
40
41 // Game Over title
42 registry.createEntity()
43 .with<ecs::Font>("main_font", utl::Path::Font::FONTS_RTYPE)
44 .with<ecs::Transform>("transform_gameover", 150.F, 150.F, 0.F)
45 .with<ecs::Color>("color_gameover", RED_COLOR.r, RED_COLOR.g, RED_COLOR.b, RED_COLOR.a)
46 .with<ecs::Text>("gameover_text", std::string("GAME OVER"), 72U)
47 .build();
48
49 // "Bande de Nuls" text
50 registry.createEntity()
51 .with<ecs::Font>("main_font", utl::Path::Font::FONTS_RTYPE)
52 .with<ecs::Transform>("transform_message", 150.F, 260.F, 0.F)
53 .with<ecs::Color>("color_message", WHITE_COLOR.r, WHITE_COLOR.g, WHITE_COLOR.b, WHITE_COLOR.a)
54 .with<ecs::Text>("message_text", std::string("Bande de Nuls"), 48U)
55 .build();
56
57 // Instruction text
58 registry.createEntity()
59 .with<ecs::Font>("main_font", utl::Path::Font::FONTS_RTYPE)
60 .with<ecs::Transform>("transform_instruction", 150.F, 350.F, 0.F)
61 .with<ecs::Color>("color_instruction", WHITE_COLOR.r, WHITE_COLOR.g, WHITE_COLOR.b, WHITE_COLOR.a)
62 .with<ecs::Text>("instruction_text", std::string("Press ESC to return to menu"), 28U)
63 .build();
64 }
65
66 void GameOverScene::update(float dt, const eng::WindowSize & /*size*/)
67 {
68 m_animationTime += dt;
69
70 // Add pulsing animation to "GAME OVER" text
71 auto &registry = AScene::getRegistry();
72
73 // Find and animate the game over text
74 auto textEntities = registry.getAll<ecs::Text>();
75
76 for (const auto &[entity, text] : textEntities)
77 {
78 auto *color = registry.getComponent<ecs::Color>(entity);
79
80 if (color && text.content == "GAME OVER")
81 {
82 // Pulsing effect: oscillate between 150 and 255 alpha
83 float pulse = (std::sin(m_animationTime * 3.0f) + 1.0f) / 2.0f; // 0.0 to 1.0
84 color->a = static_cast<unsigned char>(150 + pulse * 105);
85 }
86 }
87 }
88
90 {
92 {
93 if (event.key == eng::Key::Escape)
94 {
95 if (onBackToMenu)
96 {
98 }
99 }
100 }
101 }
102} // namespace gme
This file contains the component definitions.
This file contains the game over scene.
void event(const eng::Event &event) override
Definition gameOver.cpp:89
GameOverScene(eng::id assignedId, const std::shared_ptr< eng::IRenderer > &renderer)
Definition gameOver.cpp:11
void update(float dt, const eng::WindowSize &size) override
Definition gameOver.cpp:66
std::function< void()> onBackToMenu
Definition GameOver.hpp:36
static constexpr eng::Color WHITE_COLOR
Definition gameOver.cpp:7
static constexpr eng::Color RED_COLOR
Definition gameOver.cpp:6
This file contains common definitions and constants.
std::uint32_t Entity
Definition Entity.hpp:13
unsigned int id
Definition IScene.hpp:20
constexpr auto FONTS_RTYPE
Definition Common.hpp:97
unsigned char a
Definition Component.hpp:29
std::string id
Definition Component.hpp:15
unsigned char r
Definition IRenderer.hpp:17
unsigned char a
Definition IRenderer.hpp:20
unsigned char g
Definition IRenderer.hpp:18
unsigned char b
Definition IRenderer.hpp:19