r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
configMulti.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::ConfigMulti::ConfigMulti(const std::shared_ptr<eng::IRenderer> &renderer,
9 const std::shared_ptr<eng::IAudio> &audio)
10 : m_audio(audio)
11{
12 auto &registry = AScene::getRegistry();
13
14 registry.onComponentAdded(
15 [&renderer, &audio, &registry](const ecs::Entity e, const std::type_info &type)
16 {
17 const auto *audioComp = registry.getComponent<ecs::Audio>(e);
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 else if (type == typeid(ecs::Audio))
64 {
65 if (audioComp)
66 {
67 audio->createAudio(audioComp->path, audioComp->volume, audioComp->loop,
68 audioComp->id + std::to_string(e));
69 }
70 }
71 });
72
73 registry.createEntity().with<ecs::Audio>("id_audio", Path::Audio::AUDIO_TITLE, 5.F, true, true).build();
74 registry.createEntity()
75 .with<ecs::Font>("main_font", Path::Font::FONTS_RTYPE)
76 .with<ecs::Transform>("transform_title", 10.F, 10.F, 0.F)
77 .with<ecs::Color>("color_title", WHITE.r, WHITE.g, WHITE.b, WHITE.a)
78 .with<ecs::Text>("id", std::string("RType Client"), 50U)
79 .build();
80 m_fpsEntity = registry.createEntity()
81 .with<ecs::Font>("main_font", Path::Font::FONTS_RTYPE)
82 .with<ecs::Transform>("transform_fps", 10.F, 70.F, 0.F)
83 .with<ecs::Color>("color_fps", WHITE.r, WHITE.g, WHITE.b, WHITE.a)
84 .with<ecs::Text>("id_text", std::string("FPS: 0"), 20U)
85 .build();
86
87 for (size_t i = 0; i < m_menuOptions.size(); ++i)
88 {
89 registry.createEntity()
90 .with<ecs::Font>("main_font", Path::Font::FONTS_RTYPE)
91 .with<ecs::Transform>("transform_menu", 100.F, 200.F + i * 60.F, 0.F)
92 .with<ecs::Color>("color_menu", WHITE.r, WHITE.g, WHITE.b, WHITE.a)
93 .with<ecs::Text>("menu_" + m_menuOptions[i], m_menuOptions[i], 40U)
94 .build();
95 }
97}
98
99void cli::ConfigMulti::update(const float dt, const eng::WindowSize &size)
100{
101 auto &reg = getRegistry();
102
103 auto &transforms = reg.getAll<ecs::Transform>();
104 auto &colors = reg.getAll<ecs::Color>();
105 auto &texts = reg.getAll<ecs::Text>();
106 auto &audios = reg.getAll<ecs::Audio>();
107
108 for (auto &audio : audios)
109 {
110 if (!audio.second.play && (m_audio->isPlaying(audio.second.id) == eng::Status::Playing))
111 {
112 m_audio->stopAudio(audio.second.id);
113 }
114 }
115 size_t i = 0;
116 for (auto &[entity, text] : texts)
117 {
118 if (text.content == "Create room" || text.content == "Join room" || text.content == "Go back to menu")
119 {
120 auto &color = colors.at(entity);
121
122 if (i == m_selectedIndex)
123 {
124 color.r = 255;
125 color.g = 200;
126 color.b = 0;
127 }
128 else
129 {
130 color.r = 255;
131 color.g = 255;
132 color.b = 255;
133 }
134
135 i++;
136 }
137 }
138
139 if (auto *fpsText = reg.getComponent<ecs::Text>(m_fpsEntity))
140 {
141 fpsText->content = "FPS: " + std::to_string(static_cast<int>(1 / dt));
142 }
143}
144
146{
147 switch (event.type)
148 {
150 if (event.key == eng::Key::Up)
151 {
152 if (m_selectedIndex == 2)
153 {
154 m_selectedIndex = 0;
155 }
156 else
157 {
158 m_selectedIndex++;
159 }
160 }
161 else if (event.key == eng::Key::Down)
162 {
163 if (m_selectedIndex == 0)
164 {
165 m_selectedIndex = 2;
166 }
167 else
168 {
169 m_selectedIndex--;
170 }
171 }
172 else if (event.key == eng::Key::Enter)
173 {
174 const std::string &selectedOption =
175 m_menuOptions[static_cast<int>(m_menuOptions.size()) - 1 - m_selectedIndex];
176 if (onOptionSelected)
177 {
178 onOptionSelected(selectedOption);
179 }
180 }
181 break;
182
184 if (event.key == eng::Key::Up)
185 {
186 m_keysPressed[eng::Key::Up] = false;
187 }
188 if (event.key == eng::Key::Down)
189 {
190 m_keysPressed[eng::Key::Down] = false;
191 }
192 if (event.key == eng::Key::Left)
193 {
194 m_keysPressed[eng::Key::Left] = false;
195 }
196 if (event.key == eng::Key::Right)
197 {
198 m_keysPressed[eng::Key::Right] = false;
199 }
200 if (event.key == eng::Key::Space)
201 {
202 m_keysPressed[eng::Key::Space] = false;
203 }
204 break;
205
206 default:
207 break;
208 }
209}
This file contains the component definitions.
This file contains the multiplayer configuration scene.
This file contains the Audio interface.
const std::vector< std::string > m_menuOptions
ecs::Entity m_fpsEntity
void update(float dt, const eng::WindowSize &size) override
ConfigMulti(const std::shared_ptr< eng::IRenderer > &renderer, const std::shared_ptr< eng::IAudio > &audio)
void event(const eng::Event &event) override
This file contains common definitions and constants.
static constexpr eng::Color WHITE
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