r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
menu.cpp
Go to the documentation of this file.
2#include "Client/Common.hpp"
3#include "ECS/Component.hpp"
5
6static constexpr eng::Color WHITE = {.r = 255U, .g = 255U, .b = 255U, .a = 255U};
7
8cli::Menu::Menu(const std::shared_ptr<eng::IRenderer> &renderer, const std::shared_ptr<eng::IAudio> &audio)
9 : m_audio(audio)
10{
11 auto &registry = AScene::getRegistry();
12
13 registry.onComponentAdded(
14 [&renderer, &audio, &registry](const ecs::Entity e, const std::type_info &type)
15 {
16 const auto *audioComp = registry.getComponent<ecs::Audio>(e);
17 const auto *colorComp = registry.getComponent<ecs::Color>(e);
18 const auto *fontComp = registry.getComponent<ecs::Font>(e);
19 const auto *rectComp = registry.getComponent<ecs::Rect>(e);
20 const auto *scaleComp = registry.getComponent<ecs::Scale>(e);
21 const auto *textComp = registry.getComponent<ecs::Text>(e);
22 const auto *textureComp = registry.getComponent<ecs::Texture>(e);
23 const auto *transform = registry.getComponent<ecs::Transform>(e);
24
25 if (type == typeid(ecs::Text))
26 {
27 if (textComp && transform && fontComp)
28 {
29 renderer->createFont(fontComp->id, fontComp->path);
30 renderer->createText(
31 {.font_name = fontComp->id,
32 .color = {.r = colorComp->r, .g = colorComp->g, .b = colorComp->b, .a = colorComp->a},
33 .content = textComp->content,
34 .size = textComp->font_size,
35 .x = transform->x,
36 .y = transform->y,
37 .name = textComp->id});
38 }
39 }
40 else if (type == typeid(ecs::Texture))
41 {
42 const float scale_x = scaleComp ? scaleComp->x : 1.F;
43 const float scale_y = scaleComp ? scaleComp->y : 1.F;
44
45 renderer->createTexture(textureComp->id, textureComp->path);
46
47 if (transform && textureComp)
48 {
49 if (rectComp)
50 {
51 renderer->createSprite(textureComp->id + std::to_string(e), textureComp->id, transform->x,
52 transform->y, scale_x, scale_y, static_cast<int>(rectComp->pos_x),
53 static_cast<int>(rectComp->pos_y), rectComp->size_x, rectComp->size_y);
54 }
55 else
56 {
57 renderer->createSprite(textureComp->id + std::to_string(e), textureComp->id, transform->x,
58 transform->y);
59 }
60 }
61 }
62 else if (type == typeid(ecs::Audio))
63 {
64 if (audioComp)
65 {
66 audio->createAudio(audioComp->path, audioComp->volume, audioComp->loop,
67 audioComp->id + std::to_string(e));
68 }
69 }
70 });
71
72 registry.createEntity().with<ecs::Audio>("id_audio", Path::Audio::AUDIO_TITLE, 5.F, true, true).build();
73 registry.createEntity()
74 .with<ecs::Font>("main_font", Path::Font::FONTS_RTYPE)
75 .with<ecs::Transform>("transform_title", 10.F, 10.F, 0.F)
76 .with<ecs::Color>("color_title", WHITE.r, WHITE.g, WHITE.b, WHITE.a)
77 .with<ecs::Text>("id", std::string("RType Client"), 50U)
78 .build();
79 m_fpsEntity = registry.createEntity()
80 .with<ecs::Font>("main_font", Path::Font::FONTS_RTYPE)
81 .with<ecs::Transform>("transform_fps", 10.F, 70.F, 0.F)
82 .with<ecs::Color>("color_fps", WHITE.r, WHITE.g, WHITE.b, WHITE.a)
83 .with<ecs::Text>("id_text", std::string("FPS: 0"), 20U)
84 .build();
85
86 for (size_t i = 0; i < m_menuOptions.size(); ++i)
87 {
88 registry.createEntity()
89 .with<ecs::Font>("main_font", Path::Font::FONTS_RTYPE)
90 .with<ecs::Transform>("transform_menu", 100.F, 200.F + i * 60.F, 0.F)
91 .with<ecs::Color>("color_menu", WHITE.r, WHITE.g, WHITE.b, WHITE.a)
92 .with<ecs::Text>("menu_" + m_menuOptions[i], m_menuOptions[i], 40U)
93 .build();
94 }
96}
97
98void cli::Menu::update(const float dt, const eng::WindowSize &size)
99{
100 auto &reg = getRegistry();
101
102 auto &transforms = reg.getAll<ecs::Transform>();
103 auto &colors = reg.getAll<ecs::Color>();
104 auto &texts = reg.getAll<ecs::Text>();
105 auto &audios = reg.getAll<ecs::Audio>();
106
107 for (auto &audio : audios)
108 {
109 if (!audio.second.play && (m_audio->isPlaying(audio.second.id) == eng::Status::Playing))
110 {
111 m_audio->stopAudio(audio.second.id);
112 }
113 }
114 size_t i = 0;
115 for (auto &[entity, text] : texts)
116 {
117 if (text.content == "Solo" || text.content == "Multi" || text.content == "Settings")
118 {
119 auto &color = colors.at(entity);
120
121 if (i == m_selectedIndex)
122 {
123 color.r = 255;
124 color.g = 200;
125 color.b = 0;
126 }
127 else
128 {
129 color.r = 255;
130 color.g = 255;
131 color.b = 255;
132 }
133
134 i++;
135 }
136 }
137
138 if (auto *fpsText = reg.getComponent<ecs::Text>(m_fpsEntity))
139 {
140 fpsText->content = "FPS: " + std::to_string(static_cast<int>(1 / dt));
141 }
142}
143
144void cli::Menu::event(const eng::Event &event)
145{
146 switch (event.type)
147 {
149 if (event.key == eng::Key::Up)
150 {
151 if (m_selectedIndex == 2)
152 {
153 m_selectedIndex = 0;
154 }
155 else
156 {
157 m_selectedIndex++;
158 }
159 }
160 else if (event.key == eng::Key::Down)
161 {
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 Audio interface.
This file contains the menu scene.
void event(const eng::Event &event) override
Definition menu.cpp:144
const std::vector< std::string > m_menuOptions
Definition Menu.hpp:41
Menu(const std::shared_ptr< eng::IRenderer > &renderer, const std::shared_ptr< eng::IAudio > &audio)
Definition menu.cpp:8
ecs::Entity m_fpsEntity
Definition Menu.hpp:40
void update(float dt, const eng::WindowSize &size) override
Definition menu.cpp:98
int m_selectedIndex
Definition Menu.hpp:44
This file contains common definitions and constants.
static constexpr eng::Color WHITE
static constexpr eng::Color WHITE
Definition menu.cpp:6
constexpr auto AUDIO_TITLE
Definition Common.hpp:44
constexpr auto FONTS_RTYPE
Definition Common.hpp:50
std::uint32_t Entity
Definition Entity.hpp:13
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
Definition IRenderer.hpp:90