r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
settings.cpp
Go to the documentation of this file.
1#include <algorithm>
2#include <cmath>
3
4#include "Client/Client.hpp"
6#include "ECS/Component.hpp"
7#include "Utils/Common.hpp"
8
9cli::Settings::Settings(const eng::id assignedId, const std::shared_ptr<eng::IRenderer> &renderer,
10 utl::cli::AppConfig &config)
11 : AScene(assignedId), m_renderer(renderer), m_appConfig(config)
12{
13 auto &registry = AScene::getRegistry();
14
15 registry.onComponentAdded(
16 [&renderer, &registry](const ecs::Entity e, const std::type_info &type)
17 {
18 const auto *colorComp = registry.getComponent<ecs::Color>(e);
19 const auto *fontComp = registry.getComponent<ecs::Font>(e);
20 const auto *rectComp = registry.getComponent<ecs::Rect>(e);
21 const auto *scaleComp = registry.getComponent<ecs::Scale>(e);
22 const auto *textComp = registry.getComponent<ecs::Text>(e);
23 const auto *textureComp = registry.getComponent<ecs::Texture>(e);
24 const auto *transform = registry.getComponent<ecs::Transform>(e);
25
26 if (type == typeid(ecs::Text))
27 {
28 if (textComp && transform && fontComp)
29 {
30 renderer->createFont(fontComp->id, fontComp->path);
31 renderer->createText(
32 {.font_name = fontComp->id,
33 .color = {.r = colorComp->r, .g = colorComp->g, .b = colorComp->b, .a = colorComp->a},
34 .content = textComp->content,
35 .size = textComp->font_size,
36 .x = transform->x,
37 .y = transform->y,
38 .name = textComp->id});
39 }
40 }
41 else if (type == typeid(ecs::Texture))
42 {
43 const float scale_x = scaleComp ? scaleComp->x : 1.F;
44 const float scale_y = scaleComp ? scaleComp->y : 1.F;
45
46 renderer->createTexture(textureComp->id, textureComp->path);
47
48 if (transform && textureComp)
49 {
50 if (rectComp)
51 {
52 renderer->createSprite(textureComp->id + std::to_string(e), textureComp->id, transform->x,
53 transform->y, scale_x, scale_y, static_cast<int>(rectComp->pos_x),
54 static_cast<int>(rectComp->pos_y), rectComp->size_x, rectComp->size_y);
55 }
56 else
57 {
58 renderer->createSprite(textureComp->id + std::to_string(e), textureComp->id, transform->x,
59 transform->y);
60 }
61 }
62 }
63 });
64
66 registry.createEntity()
67 .with<ecs::Font>("main_font", utl::Path::Font::FONTS_RTYPE)
68 .with<ecs::Transform>("transform_title", 100.F, 60.F, 0.F)
71 .with<ecs::Text>("title", std::string("SETTINGS"), 72U)
72 .build();
73
74 for (size_t i = 0; i < m_settingsOptions.size(); ++i)
75 {
76 registry.createEntity()
77 .with<ecs::Font>("main_font", utl::Path::Font::FONTS_RTYPE)
78 .with<ecs::Transform>("transform_setting_" + std::to_string(i), 100.F, 200.F + i * 50.F, 0.F)
79 .with<ecs::Color>("color_setting_" + std::to_string(i), utl::Config::Color::GRAY_BLUE_SUBTLE.r,
82 .with<ecs::Text>("setting_" + m_settingsOptions[i], m_settingsOptions[i], 32U)
83 .build();
84 }
86 registry.createEntity()
87 .with<ecs::Font>("main_font", utl::Path::Font::FONTS_RTYPE)
88 .with<ecs::Transform>("transform_volume_value", 580.F, 200.F, 0.F)
89 .with<ecs::Color>("color_volume_value", utl::Config::Color::TEXT_VALUE_COLOR.r,
92 .with<ecs::Text>("volume_value", std::string("50"), 24U)
93 .build();
95 registry.createEntity()
96 .with<ecs::Font>("main_font", utl::Path::Font::FONTS_RTYPE)
97 .with<ecs::Transform>("transform_quality_value", 580.F, 250.F, 0.F)
98 .with<ecs::Color>("color_quality_value", utl::Config::Color::TEXT_VALUE_COLOR.r,
101 .with<ecs::Text>("quality_value", std::string("Medium"), 24U)
102 .build();
104 registry.createEntity()
105 .with<ecs::Font>("main_font", utl::Path::Font::FONTS_RTYPE)
106 .with<ecs::Transform>("transform_control_value", 580.F, 300.F, 0.F)
107 .with<ecs::Color>("color_control_value", utl::Config::Color::TEXT_VALUE_COLOR.r,
110 .with<ecs::Text>("control_value", std::string("WASD"), 24U)
111 .build();
112
114 registry.createEntity()
115 .with<ecs::Transform>("transform_skin_sprite", 580.F, 345.F, 0.F)
116 .with<ecs::Scale>("scale_skin_sprite", 2.0f, 2.0f)
119 .with<ecs::Rect>("rect_skin_sprite", 0.0f, 0.0f, 33, 17)
120 .with<ecs::Texture>("skin_sprite", "assets/sprites/r-typesheet42.gif")
121 .build();
122 registry.createEntity()
123 .with<ecs::Font>("main_font", utl::Path::Font::FONTS_RTYPE)
124 .with<ecs::Transform>("transform_instruction", 80.F, 480.F, 0.F)
125 .with<ecs::Color>("color_instruction", utl::Config::Color::INFO_TEXT_COLOR.r,
128 .with<ecs::Text>("instruction", std::string("UP/DOWN navigate, LEFT/RIGHT change, ESC back"), 16U)
129 .build();
130
131 m_selectedIndex = 0;
133}
134
135void cli::Settings::update(const float dt, const eng::WindowSize & /*size*/)
136{
137 auto &reg = getRegistry();
138 auto &colors = reg.getAll<ecs::Color>();
139 auto &texts = reg.getAll<ecs::Text>();
140
141 m_animationTime += dt;
142 m_titlePulseTime += dt;
143 for (auto &[entity, text] : texts)
144 {
145 for (size_t i = 0; i < m_settingsOptions.size(); ++i)
146 {
147 if (text.id == "setting_" + m_settingsOptions[i])
148 {
149 auto &color = colors.at(entity);
150
151 if (i == m_selectedIndex)
152 {
153 float glowIntensity = std::sin(m_animationTime * 2.5f);
154 color.r = 0U;
155 color.g = static_cast<unsigned char>(191U + glowIntensity * 50);
156 color.b = 255U;
157 }
158 else
159 {
163 }
164 break;
165 }
166 }
167 }
168 if (auto *titleColor = reg.getComponent<ecs::Color>(m_titleEntity))
169 {
170 const float pulsation = std::sin(m_titlePulseTime * 2.0f) * 0.4f + 0.6f;
171 titleColor->r = static_cast<unsigned char>(utl::Config::Color::CYAN_ELECTRIC.r * pulsation);
172 titleColor->g = static_cast<unsigned char>(utl::Config::Color::CYAN_ELECTRIC.g * pulsation);
173 titleColor->b = static_cast<unsigned char>(utl::Config::Color::CYAN_ELECTRIC.b * pulsation);
174 }
175
176 updateSettingsDisplay();
177}
178
180{
181 auto &registry = getRegistry();
182
183 if (auto *volumeValueText = registry.getComponent<ecs::Text>(m_volumeValueEntity))
184 {
185 volumeValueText->content = std::to_string(m_audioVolume);
186 }
187 if (auto *qualityValueText = registry.getComponent<ecs::Text>(m_qualityValueEntity))
188 {
189 const std::vector<std::string> fpsOptions = {"60 FPS", "144 FPS", "240 FPS"};
190 qualityValueText->content = fpsOptions[static_cast<size_t>(m_videoQuality)];
191 }
192 if (auto *controlValueText = registry.getComponent<ecs::Text>(m_controlValueEntity))
193 {
194 const std::vector<std::string> controlSchemes = {"WASD", "ZQSD", "Arrows"};
195 controlValueText->content = controlSchemes[static_cast<size_t>(m_controlScheme)];
196 }
197 if (auto *skinRect = registry.getComponent<ecs::Rect>(m_skinSpriteEntity))
198 {
199 const std::vector shipLines = {0.0f, 17.0f, 34.0f, 51.0f, 68.0f};
200
201 skinRect->pos_y = shipLines[static_cast<size_t>(m_skinIndex)];
202 skinRect->pos_x = 0.0f;
203 skinRect->size_x = 33U;
204 skinRect->size_y = 17U;
205 }
206}
207
209{
210 switch (event.type)
211 {
213 if (event.key == eng::Key::Escape)
214 {
215 onLeave();
216 }
217 else if (event.key == eng::Key::Up)
218 {
219 m_selectedIndex = (m_selectedIndex == 0) ? m_settingsOptions.size() - 1 : m_selectedIndex - 1;
220 m_playMusic = true;
221 }
222 else if (event.key == eng::Key::Down)
223 {
224 m_selectedIndex = (m_selectedIndex == m_settingsOptions.size() - 1) ? 0 : m_selectedIndex + 1;
225 m_playMusic = true;
226 }
227 else if (event.key == eng::Key::Enter)
228 {
229 if (const std::string &selectedOption = m_settingsOptions[m_selectedIndex];
230 selectedOption == "Back to Menu")
231 {
232 onLeave();
233 }
234 }
235 else if (event.key == eng::Key::Left || event.key == eng::Key::Right)
236 {
237
238 if (const std::string &selectedOption = m_settingsOptions[m_selectedIndex];
239 selectedOption == "Audio Volume")
240 {
241 const float newVolume = m_audioVolume + ((event.key == eng::Key::Right) ? 0.01F : -0.01F);
242 m_audioVolume = (std::max)(0.0F, (std::min)(10.0F, newVolume));
243 m_appConfig.audioVolume = m_audioVolume;
244 }
245 else if (selectedOption == "FPS")
246 {
247 if (event.key == eng::Key::Left)
248 {
249 m_videoQuality = (m_videoQuality == 0) ? 2 : m_videoQuality - 1;
250 }
251 else
252 {
253 m_videoQuality = (m_videoQuality == 2) ? 0 : m_videoQuality + 1;
254 }
255 m_appConfig.videoQuality = m_videoQuality;
256 applyVideoQuality();
257 }
258 else if (selectedOption == "Controls")
259 {
260 if (event.key == eng::Key::Left)
261 {
262 m_controlScheme = (m_controlScheme == 0) ? 2 : m_controlScheme - 1;
263 }
264 else
265 {
266 m_controlScheme = (m_controlScheme == 2) ? 0 : m_controlScheme + 1;
267 }
268 m_appConfig.controlScheme = m_controlScheme;
269 }
270 else if (selectedOption == "Skin")
271 {
272 if (event.key == eng::Key::Left)
273 {
274 m_skinIndex = (m_skinIndex == 0) ? 4 : m_skinIndex - 1;
275 }
276 else
277 {
278 m_skinIndex = (m_skinIndex == 4) ? 0 : m_skinIndex + 1;
279 }
280 m_appConfig.skinIndex = m_skinIndex;
281
282 applySkinChange();
283 }
284 updateSettingsDisplay();
285 }
286 break;
287 default:
288 break;
289 }
290}
291
293{
294 m_audioVolume = m_appConfig.audioVolume;
295 m_videoQuality = m_appConfig.videoQuality;
296 m_controlScheme = m_appConfig.controlScheme;
297 m_skinIndex = m_appConfig.skinIndex;
298
299 updateSettingsDisplay();
300 applyVideoQuality();
301}
302
304
This file contains the Client class declaration.
This file contains the component definitions.
This file contains the settings scene.
ecs::Entity m_skinSpriteEntity
Definition Settings.hpp:58
ecs::Entity m_volumeValueEntity
Definition Settings.hpp:55
const std::vector< std::string > m_settingsOptions
Definition Settings.hpp:49
static void applyVideoQuality()
Definition settings.cpp:303
ecs::Entity m_controlValueEntity
Definition Settings.hpp:57
void update(float dt, const eng::WindowSize &size) override
Definition settings.cpp:135
static void applySkinChange()
Definition settings.cpp:305
void updateSettingsDisplay()
Definition settings.cpp:179
size_t m_selectedIndex
Definition Settings.hpp:48
void loadFromConfig()
Definition settings.cpp:292
void event(const eng::Event &event) override
Definition settings.cpp:208
ecs::Entity m_qualityValueEntity
Definition Settings.hpp:56
ecs::Entity m_titleEntity
Definition Settings.hpp:59
Settings(eng::id assignedId, const std::shared_ptr< eng::IRenderer > &renderer, utl::cli::AppConfig &config)
Definition settings.cpp:9
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 TEXT_VALUE_COLOR
Definition Common.hpp:66
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
static constexpr eng::Color INFO_TEXT_COLOR
Definition Common.hpp:63
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