r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
configMulti.cpp
Go to the documentation of this file.
1#include <cmath>
2
3#include "ECS/Component.hpp"
5#include "Utils/Common.hpp"
6
7gme::ConfigMulti::ConfigMulti(const eng::id assignedId, const std::shared_ptr<eng::IRenderer> &renderer)
8 : AScene(assignedId)
9{
10 auto &registry = AScene::getRegistry();
11
12 registry.onComponentAdded(
13 [&renderer, &registry](const ecs::Entity e, const std::type_info &type)
14 {
15 const auto *colorComp = registry.getComponent<ecs::Color>(e);
16 const auto *fontComp = registry.getComponent<ecs::Font>(e);
17 const auto *rectComp = registry.getComponent<ecs::Rect>(e);
18 const auto *scaleComp = registry.getComponent<ecs::Scale>(e);
19 const auto *textComp = registry.getComponent<ecs::Text>(e);
20 const auto *textureComp = registry.getComponent<ecs::Texture>(e);
21 const auto *transform = registry.getComponent<ecs::Transform>(e);
22
23 if (type == typeid(ecs::Text))
24 {
25 if (textComp && transform && fontComp)
26 {
27 renderer->createFont(fontComp->id, fontComp->path);
28 renderer->createText(
29 {.font_name = fontComp->id,
30 .color = {.r = colorComp->r, .g = colorComp->g, .b = colorComp->b, .a = colorComp->a},
31 .content = textComp->content,
32 .size = textComp->font_size,
33 .x = transform->x,
34 .y = transform->y,
35 .name = textComp->id});
36 }
37 }
38 else if (type == typeid(ecs::Texture))
39 {
40 const float scale_x = scaleComp ? scaleComp->x : 1.F;
41 const float scale_y = scaleComp ? scaleComp->y : 1.F;
42
43 renderer->createTexture(textureComp->id, textureComp->path);
44
45 if (transform && textureComp)
46 {
47 if (rectComp)
48 {
49 renderer->createSprite(textureComp->id + std::to_string(e), textureComp->id, transform->x,
50 transform->y, scale_x, scale_y, static_cast<int>(rectComp->pos_x),
51 static_cast<int>(rectComp->pos_y), rectComp->size_x, rectComp->size_y);
52 }
53 else
54 {
55 renderer->createSprite(textureComp->id + std::to_string(e), textureComp->id, transform->x,
56 transform->y);
57 }
58 }
59 }
60 });
61
63 registry.createEntity()
64 .with<ecs::Font>("main_font", utl::Path::Font::FONTS_RTYPE)
65 .with<ecs::Transform>("transform_title", 100.F, 60.F, 0.F)
68 .with<ecs::Text>("title", std::string("MULTIPLAYER"), 72U)
69 .build();
70
71 for (size_t i = 0; i < m_menuOptions.size(); ++i)
72 {
73 registry.createEntity()
74 .with<ecs::Font>("main_font", utl::Path::Font::FONTS_RTYPE)
75 .with<ecs::Transform>("transform_menu_" + std::to_string(i), 100.F, 200.F + i * 60.F, 0.F)
76 .with<ecs::Color>("color_menu_" + std::to_string(i), utl::Config::Color::GRAY_BLUE_SUBTLE.r,
79 .with<ecs::Text>("menu_" + m_menuOptions[i], m_menuOptions[i], 40U)
80 .build();
81 }
82
84}
85
86void gme::ConfigMulti::update(const float dt, const eng::WindowSize & /*size*/)
87{
88 auto &reg = getRegistry();
89
90 auto &colors = reg.getAll<ecs::Color>();
91 auto &texts = reg.getAll<ecs::Text>();
92
93 m_animationTime += dt;
94 m_titlePulseTime += dt;
95
96 int i = 0;
97 for (auto &[entity, text] : texts)
98 {
99 if (text.content == "Create room" || text.content == "Join room" || text.content == "Go back to menu")
100 {
101 auto &color = colors.at(entity);
102
103 if (i == m_selectedIndex)
104 {
105 const float glowIntensity = std::sin(m_animationTime * 2.5f);
106 color.r = 0U;
107 color.g = static_cast<unsigned char>(191U + glowIntensity * 50);
108 color.b = 255U;
109 }
110 else
111 {
115 }
116 i++;
117 }
118 }
119 if (auto *titleColor = reg.getComponent<ecs::Color>(m_titleEntity))
120 {
121 const float pulsation = (std::sin(m_titlePulseTime * 2.0f) * 0.4f) + 0.6f;
122 titleColor->r = static_cast<unsigned char>(utl::Config::Color::CYAN_ELECTRIC.r * pulsation);
123 titleColor->g = static_cast<unsigned char>(utl::Config::Color::CYAN_ELECTRIC.g * pulsation);
124 titleColor->b = static_cast<unsigned char>(utl::Config::Color::CYAN_ELECTRIC.b * pulsation);
125 }
126}
127
129{
130 switch (event.type)
131 {
133 if (event.key == eng::Key::Up)
134 {
135 m_selectedIndex = (m_selectedIndex == 2) ? 0 : m_selectedIndex + 1;
136 m_playMusic = true;
137 }
138 else if (event.key == eng::Key::Down)
139 {
140 m_selectedIndex = (m_selectedIndex == 0) ? 2 : m_selectedIndex - 1;
141 m_playMusic = true;
142 }
143 else if (event.key == eng::Key::Enter)
144 {
145 const std::string &selectedOption =
146 m_menuOptions[static_cast<int>(m_menuOptions.size()) - 1 - m_selectedIndex];
147 if (onOptionSelected)
148 onOptionSelected(selectedOption);
149 }
150 break;
151
153 m_keysPressed[event.key] = false;
154 break;
155
156 default:
157 break;
158 }
159}
This file contains the component definitions.
This file contains the multiplayer configuration scene.
void event(const eng::Event &event) override
const std::vector< std::string > m_menuOptions
ecs::Entity m_titleEntity
ConfigMulti(eng::id assignedId, const std::shared_ptr< eng::IRenderer > &renderer)
void update(float dt, const eng::WindowSize &size) override
This file contains common definitions and constants.
std::uint32_t Entity
Definition Entity.hpp:13
unsigned int id
Definition IScene.hpp:20
static constexpr eng::Color GRAY_BLUE_SUBTLE
Definition Common.hpp:57
static constexpr eng::Color CYAN_ELECTRIC
Definition Common.hpp:56
constexpr auto FONTS_RTYPE
Definition Common.hpp:97
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
EventType type