r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
configSolo.cpp
Go to the documentation of this file.
1#include <cmath>
2
3#include "ECS/Component.hpp"
5#include "Utils/Common.hpp"
6
7gme::ConfigSolo::ConfigSolo(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>("id", std::string("SOLO"), 80U)
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", 100.F, 200.F + i * 60.F, 0.F)
78 .with<ecs::Text>("menu_" + m_menuOptions[i], m_menuOptions[i], 40U)
79 .build();
80 }
82}
83
84void gme::ConfigSolo::update(const float dt, const eng::WindowSize & /*size*/)
85{
86 auto &reg = getRegistry();
87
88 auto &colors = reg.getAll<ecs::Color>();
89 auto &texts = reg.getAll<ecs::Text>();
90
91 m_animationTime += dt;
92 m_titlePulseTime += dt;
93
94 if (auto *titleColor = reg.getComponent<ecs::Color>(m_titleEntity))
95 {
96 const float pulse = (std::sin(m_titlePulseTime * 1.2f) + 1.0f) * 0.5f;
97 titleColor->r = static_cast<uint8_t>(utl::Config::Color::CYAN_ELECTRIC.r * (0.8f + pulse * 0.2f));
98 titleColor->g = static_cast<uint8_t>(utl::Config::Color::CYAN_ELECTRIC.g * (0.8f + pulse * 0.2f));
99 titleColor->b = static_cast<uint8_t>(utl::Config::Color::CYAN_ELECTRIC.b * (0.9f + pulse * 0.1f));
100 }
101 if (auto *titleTransform = reg.getComponent<ecs::Transform>(m_titleEntity))
102 {
103 titleTransform->y = 60.0f + std::sin(m_titlePulseTime * 0.8f) * 2.0f;
104 }
105 int i = 0;
106 for (auto &[entity, text] : texts)
107 {
108 if (text.content == "Level easy" || text.content == "Level medium" || text.content == "Go back to menu")
109 {
110 auto &color = colors.at(entity);
111
112 if (i == m_selectedIndex)
113 {
114 const float glowIntensity = std::sin(m_animationTime * 2.5f);
115 color.r = 0U;
116 color.g = static_cast<unsigned char>(191U + (glowIntensity * 50));
117 color.b = 255U;
118 }
119 else
120 {
124 }
125
126 i++;
127 }
128 }
129 if (auto *titleColor = reg.getComponent<ecs::Color>(m_titleEntity))
130 {
131 const float pulsation = std::sin(m_titlePulseTime * 2.0f) * 0.4f + 0.6f;
132 titleColor->r = static_cast<unsigned char>(utl::Config::Color::CYAN_ELECTRIC.r * pulsation);
133 titleColor->g = static_cast<unsigned char>(utl::Config::Color::CYAN_ELECTRIC.g * pulsation);
134 titleColor->b = static_cast<unsigned char>(utl::Config::Color::CYAN_ELECTRIC.b * pulsation);
135 }
136 if (auto *fpsText = reg.getComponent<ecs::Text>(m_fpsEntity))
137 {
138 fpsText->content = "FPS: " + std::to_string(static_cast<int>(1 / dt));
139 }
140}
141
143{
144 switch (event.type)
145 {
147 if (event.key == eng::Key::Up)
148 {
149 m_playMusic = true;
150 if (m_selectedIndex == 2)
151 {
152 m_selectedIndex = 0;
153 }
154 else
155 {
156 m_selectedIndex++;
157 }
158 }
159 else if (event.key == eng::Key::Down)
160 {
161 m_playMusic = true;
162 if (m_selectedIndex == 0)
163 {
164 m_selectedIndex = 2;
165 }
166 else
167 {
168 m_selectedIndex--;
169 }
170 }
171 else if (event.key == eng::Key::Enter)
172 {
173 const std::string &selectedOption =
174 m_menuOptions[static_cast<int>(m_menuOptions.size()) - 1 - m_selectedIndex];
175 if (onOptionSelected)
176 {
177 onOptionSelected(selectedOption);
178 }
179 }
180 break;
181
183 if (event.key == eng::Key::Up)
184 {
185 m_keysPressed[eng::Key::Up] = false;
186 }
187 if (event.key == eng::Key::Down)
188 {
189 m_keysPressed[eng::Key::Down] = false;
190 }
191 if (event.key == eng::Key::Left)
192 {
193 m_keysPressed[eng::Key::Left] = false;
194 }
195 if (event.key == eng::Key::Right)
196 {
197 m_keysPressed[eng::Key::Right] = false;
198 }
199 if (event.key == eng::Key::Space)
200 {
201 m_keysPressed[eng::Key::Space] = false;
202 }
203 break;
204
205 default:
206 break;
207 }
208}
This file contains the component definitions.
This file contains the solo configuration scene.
void update(float dt, const eng::WindowSize &size) override
ConfigSolo(eng::id assignedId, const std::shared_ptr< eng::IRenderer > &renderer)
Definition configSolo.cpp:7
void event(const eng::Event &event) override
ecs::Entity m_titleEntity
const std::vector< std::string > m_menuOptions
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 WHITE
Definition Common.hpp:64
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