|
| 1 | +/* |
| 2 | +** EPITECH PROJECT, 2024 |
| 3 | +** r-type |
| 4 | +** File description: |
| 5 | +** create_room.cpp |
| 6 | +*/ |
| 7 | + |
| 8 | +#include "create_room.hpp" |
| 9 | + |
| 10 | +#include "client/src/systems/register/connection.hpp" |
| 11 | +#include "client/src/systems/ui/input_cursor.hpp" |
| 12 | +#include "client/src/ui/input.hpp" |
| 13 | +#include "libs/mew/src/sets/events/events.hpp" |
| 14 | +#include "scenes/lobby.hpp" |
| 15 | +#include "start.hpp" |
| 16 | +#include "systems/animations/blink.hpp" |
| 17 | + |
| 18 | +using namespace rtype::client::scenes; |
| 19 | +using namespace rtype::client::services; |
| 20 | +using namespace rtype::client::systems; |
| 21 | +using namespace mew::sets::drawable; |
| 22 | +using namespace mew::sets::events; |
| 23 | +using namespace mew::managers; |
| 24 | +using namespace zygarde::core::components; |
| 25 | +using namespace zygarde::core::types; |
| 26 | + |
| 27 | +SceneCreateRoom::SceneCreateRoom(DependenciesHandler::Ptr services) |
| 28 | + : SceneBase(std::move(services)) { |
| 29 | + registry_->RegisterComponent<Tags>(); |
| 30 | + registry_->AddSystem<BlinkSystem>(); |
| 31 | + constexpr Alignment alignment{HorizontalAlign::kLeft, VerticalAlign::kCenter}; |
| 32 | + registry_->AddSystem<systems::ui::CursorSystem>(alignment); |
| 33 | + const Vector3f pos{CREATE_ROOM_PIXEL_LEFT + 40, managers_.window->GetHeight() / 2 - 50, 0}; |
| 34 | + |
| 35 | + ui::Input::Create(registry_, "create_room", pos, alignment, 20); |
| 36 | + serverConnectionService_ = services_->GetOrThrow<ServerConnectionService>(); |
| 37 | + settingsManager_ = services_->GetOrThrow<SettingsManager>(); |
| 38 | + scenesManager_ = services_->GetOrThrow<ScenesManager>(); |
| 39 | +} |
| 40 | + |
| 41 | +void SceneCreateRoom::OnCreate() { |
| 42 | + CreateTitle(); |
| 43 | + CreateInputLabel(); |
| 44 | + CreateDifficulty(); |
| 45 | + CreateNbPlayers(); |
| 46 | + CreateCreateButton(); |
| 47 | + CreateBackButton(); |
| 48 | +} |
| 49 | + |
| 50 | +void SceneCreateRoom::OnActivate() { |
| 51 | + difficulty_ = 0; |
| 52 | + nbPlayers_ = 1; |
| 53 | + const auto all_tags = registry_->GetComponents<Tags>(); |
| 54 | + const auto all_drawables = registry_->GetComponents<Drawable>(); |
| 55 | + if (!all_tags || !all_drawables) |
| 56 | + return; |
| 57 | + for (std::size_t i = 0; i < all_tags->size(); i++) { |
| 58 | + if (!(*all_tags)[i]) |
| 59 | + continue; |
| 60 | + if ((*all_tags)[i].value() & "create_room_input") { |
| 61 | + (*all_tags)[i]->RemoveTag("disabled"); |
| 62 | + if (i < all_drawables->size() && (*all_drawables)[i]) { |
| 63 | + (*all_drawables)[i]->drawable = Text{"", "main", 20}; |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +void SceneCreateRoom::CreateTitle() const { |
| 70 | + const auto title = registry_->SpawnEntity(); |
| 71 | + const auto aligns = Alignment{HorizontalAlign::kCenter, VerticalAlign::kCenter}; |
| 72 | + const auto point = |
| 73 | + Vector3f(managers_.window->GetWidth() / 2, managers_.window->GetHeight() / 2 - 250); |
| 74 | + |
| 75 | + registry_->AddComponent<Position>(title, {point, aligns}); |
| 76 | + registry_->AddComponent<Drawable>(title, |
| 77 | + {Text{"Create a Room", "main", 60}, WindowManager::View::HUD}); |
| 78 | +} |
| 79 | + |
| 80 | +void SceneCreateRoom::CreateCreateButton() const { |
| 81 | + const auto connect_button = registry_->SpawnEntity(); |
| 82 | + const auto point = |
| 83 | + Vector3f(managers_.window->GetWidth() / 2, managers_.window->GetHeight() / 2 + 150); |
| 84 | + const auto aligns = Alignment{HorizontalAlign::kCenter, VerticalAlign::kCenter}; |
| 85 | + |
| 86 | + registry_->AddComponent<Position>(connect_button, {point, aligns}); |
| 87 | + registry_->AddComponent<Drawable>(connect_button, |
| 88 | + {Text{"Create", "main", 20}, WindowManager::View::HUD}); |
| 89 | + registry_->AddComponent<OnMousePressed>( |
| 90 | + connect_button, |
| 91 | + OnMousePressed{.strategy = MouseEventTarget::kLocalTarget, |
| 92 | + .handler = [this](const sf::Mouse::Button& button, const sf::Vector2f& pos, |
| 93 | + const MouseEventTarget& target) { |
| 94 | + if (button == sf::Mouse::Button::Left) { |
| 95 | + const auto all_tags = registry_->GetComponents<Tags>(); |
| 96 | + const auto all_drawables = registry_->GetComponents<Drawable>(); |
| 97 | + if (!all_tags || !all_drawables) |
| 98 | + return; |
| 99 | + for (std::size_t i = 0; i < all_tags->size(); i++) { |
| 100 | + if (!(*all_tags)[i]) |
| 101 | + continue; |
| 102 | + if ((*all_tags)[i].value() & "create_room_input") { |
| 103 | + auto& drawable = (*all_drawables)[i]; |
| 104 | + auto& text = std::get<Text>(drawable->drawable); |
| 105 | + if (text.text.empty() || difficulty_ == 0 || nbPlayers_ == 0) { |
| 106 | + return; |
| 107 | + } |
| 108 | + api::payload::CreateRoom payload{}; |
| 109 | + payload.difficulty = difficulty_ - 1; |
| 110 | + payload.nbPlayers = nbPlayers_ - 1; |
| 111 | + char usernameChar[20] = {0}; |
| 112 | + strncpy(usernameChar, text.text.c_str(), sizeof(usernameChar) - 1); |
| 113 | + std::memcpy(payload.name, usernameChar, sizeof(payload.name)); |
| 114 | + auto res = serverConnectionService_->client()->CreateRoom(payload); |
| 115 | + if (res) { |
| 116 | + scenesManager_->GoToScene<SceneStart>(); |
| 117 | + (*all_tags)[i]->AddTag("disabled"); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + }}); |
| 123 | + registry_->AddComponent<OnMouseMoved>( |
| 124 | + connect_button, |
| 125 | + OnMouseMoved{.strategy = MouseEventTarget::kAnyTarget, |
| 126 | + .handler = [this, connect_button](const sf::Vector2f& pos, |
| 127 | + const MouseEventTarget& target) { |
| 128 | + auto drawables = registry_->GetComponents<Drawable>(); |
| 129 | + |
| 130 | + auto& dr = (*drawables)[static_cast<std::size_t>(connect_button)]; |
| 131 | + |
| 132 | + if (dr) { |
| 133 | + auto& drawable = dr.value(); |
| 134 | + auto& variant = drawable.drawable; |
| 135 | + |
| 136 | + if (std::holds_alternative<Text>(variant)) { |
| 137 | + auto& text = std::get<Text>(variant); |
| 138 | + if (difficulty_ == 0 || nbPlayers_ == 0) { |
| 139 | + text.style = sf::Text::Style::Regular; |
| 140 | + text.color = sf::Color::White; |
| 141 | + return; |
| 142 | + } |
| 143 | + if (target == MouseEventTarget::kLocalTarget) { |
| 144 | + text.style = sf::Text::Style::Underlined; |
| 145 | + text.color = sf::Color::Green; |
| 146 | + } else { |
| 147 | + text.style = sf::Text::Style::Regular; |
| 148 | + text.color = sf::Color::White; |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + }}); |
| 153 | +} |
| 154 | + |
| 155 | +void SceneCreateRoom::CreateBackButton() const { |
| 156 | + const auto exit_button = registry_->SpawnEntity(); |
| 157 | + const auto point = Vector3f(managers_.window->GetWidth() / 2, managers_.window->GetHeight() - 50); |
| 158 | + const auto aligns = Alignment{HorizontalAlign::kCenter, VerticalAlign::kCenter}; |
| 159 | + registry_->AddComponent<Position>(exit_button, {point, aligns}); |
| 160 | + registry_->AddComponent<Drawable>(exit_button, |
| 161 | + {Text{"Back", "main", 20}, WindowManager::View::HUD}); |
| 162 | + registry_->AddComponent<OnMousePressed>( |
| 163 | + exit_button, |
| 164 | + OnMousePressed{.strategy = MouseEventTarget::kLocalTarget, |
| 165 | + .handler = [this](const sf::Mouse::Button& button, const sf::Vector2f& pos, |
| 166 | + MouseEventTarget target) { |
| 167 | + if (button == sf::Mouse::Button::Left) { |
| 168 | + managers_.scenes->GoToScene<SceneStart>(); |
| 169 | + } |
| 170 | + }}); |
| 171 | + registry_->AddComponent<OnMouseMoved>( |
| 172 | + exit_button, OnMouseMoved{.strategy = MouseEventTarget::kAnyTarget, |
| 173 | + .handler = [this, exit_button](const sf::Vector2f& pos, |
| 174 | + const MouseEventTarget& target) { |
| 175 | + auto drawables = registry_->GetComponents<Drawable>(); |
| 176 | + |
| 177 | + auto& dr = (*drawables)[static_cast<std::size_t>(exit_button)]; |
| 178 | + |
| 179 | + if (dr) { |
| 180 | + auto& drawable = dr.value(); |
| 181 | + auto& variant = drawable.drawable; |
| 182 | + |
| 183 | + if (std::holds_alternative<Text>(variant)) { |
| 184 | + auto& text = std::get<Text>(variant); |
| 185 | + if (target == MouseEventTarget::kLocalTarget) { |
| 186 | + text.style = sf::Text::Style::Underlined; |
| 187 | + } else { |
| 188 | + text.style = sf::Text::Style::Regular; |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + }}); |
| 193 | +} |
| 194 | + |
| 195 | +void SceneCreateRoom::CreateInputLabel() { |
| 196 | + const auto input_label = registry_->SpawnEntity(); |
| 197 | + const auto point = Vector3f(CREATE_ROOM_PIXEL_LEFT, managers_.window->GetHeight() / 2 - 50); |
| 198 | + constexpr auto aligns = Alignment{HorizontalAlign::kRight, VerticalAlign::kCenter}; |
| 199 | + |
| 200 | + registry_->AddComponent<Position>(input_label, {point, aligns}); |
| 201 | + registry_->AddComponent<Drawable>(input_label, |
| 202 | + {Text{"Name :", "main", 20}, WindowManager::View::HUD}); |
| 203 | +} |
| 204 | + |
| 205 | +void SceneCreateRoom::CreateDifficulty() { |
| 206 | + CreateDifficultyLabel(); |
| 207 | + CreateDifficultyNumbers(); |
| 208 | +} |
| 209 | + |
| 210 | +void SceneCreateRoom::CreateDifficultyLabel() { |
| 211 | + const auto difficulty_text = registry_->SpawnEntity(); |
| 212 | + const auto point = Vector3f(CREATE_ROOM_PIXEL_LEFT, managers_.window->GetHeight() / 2); |
| 213 | + const auto aligns = Alignment{HorizontalAlign::kRight, VerticalAlign::kCenter}; |
| 214 | + |
| 215 | + registry_->AddComponent<Position>(difficulty_text, {point, aligns}); |
| 216 | + registry_->AddComponent<Drawable>(difficulty_text, |
| 217 | + {Text{"Difficulty :", "main", 20}, WindowManager::View::HUD}); |
| 218 | +} |
| 219 | + |
| 220 | +void SceneCreateRoom::CreateDifficultyNumbers() { |
| 221 | + for (unsigned int i = 1; i <= 5; i++) { |
| 222 | + CreateDifficultyNumber(i, {CREATE_ROOM_PIXEL_LEFT + 40 + static_cast<float>(i - 1) * 40, |
| 223 | + managers_.window->GetHeight() / 2}); |
| 224 | + } |
| 225 | +} |
| 226 | + |
| 227 | +void SceneCreateRoom::CreateDifficultyNumber(unsigned int number, const sf::Vector2f& pos) { |
| 228 | + const auto difficulty_number = registry_->SpawnEntity(); |
| 229 | + constexpr auto aligns = Alignment{HorizontalAlign::kCenter, VerticalAlign::kCenter}; |
| 230 | + |
| 231 | + registry_->AddComponent<Position>(difficulty_number, {Vector3f{pos.x, pos.y, 0}, aligns}); |
| 232 | + registry_->AddComponent<Drawable>( |
| 233 | + difficulty_number, {Text{std::to_string(number), "main", 20}, WindowManager::View::HUD}); |
| 234 | + registry_->AddComponent<OnMousePressed>( |
| 235 | + difficulty_number, |
| 236 | + OnMousePressed{.strategy = MouseEventTarget::kLocalTarget, |
| 237 | + .handler = [this, difficulty_number, number](const sf::Mouse::Button& button, |
| 238 | + const sf::Vector2f& pos, |
| 239 | + const MouseEventTarget& target) { |
| 240 | + if (button == sf::Mouse::Button::Left) { |
| 241 | + difficulty_ = number + 1; |
| 242 | + auto drawables = registry_->GetComponents<Drawable>(); |
| 243 | + |
| 244 | + auto& dr = (*drawables)[static_cast<std::size_t>(difficulty_number)]; |
| 245 | + |
| 246 | + if (dr) { |
| 247 | + auto& drawable = dr.value(); |
| 248 | + auto& variant = drawable.drawable; |
| 249 | + |
| 250 | + if (std::holds_alternative<Text>(variant)) { |
| 251 | + auto& text = std::get<Text>(variant); |
| 252 | + if (target == MouseEventTarget::kLocalTarget) { |
| 253 | + text.style = sf::Text::Style::Underlined; |
| 254 | + } else { |
| 255 | + text.style = sf::Text::Style::Regular; |
| 256 | + } |
| 257 | + } |
| 258 | + } |
| 259 | + } |
| 260 | + }}); |
| 261 | + |
| 262 | + registry_->AddComponent<OnMouseMoved>( |
| 263 | + difficulty_number, |
| 264 | + OnMouseMoved{.strategy = MouseEventTarget::kAnyTarget, |
| 265 | + .handler = [this, difficulty_number, number](const sf::Vector2f& pos, |
| 266 | + const MouseEventTarget& target) { |
| 267 | + auto drawables = registry_->GetComponents<Drawable>(); |
| 268 | + |
| 269 | + auto& dr = (*drawables)[static_cast<std::size_t>(difficulty_number)]; |
| 270 | + |
| 271 | + if (dr) { |
| 272 | + auto& drawable = dr.value(); |
| 273 | + auto& variant = drawable.drawable; |
| 274 | + |
| 275 | + if (std::holds_alternative<Text>(variant)) { |
| 276 | + auto& text = std::get<Text>(variant); |
| 277 | + if (target == MouseEventTarget::kLocalTarget) { |
| 278 | + text.style = sf::Text::Style::Underlined; |
| 279 | + } else { |
| 280 | + if (difficulty_ != number + 1) { |
| 281 | + text.style = sf::Text::Style::Regular; |
| 282 | + } |
| 283 | + } |
| 284 | + } |
| 285 | + } |
| 286 | + }}); |
| 287 | +} |
| 288 | + |
| 289 | +void SceneCreateRoom::CreateNbPlayers() { |
| 290 | + CreateNbPlayersLabel(); |
| 291 | + CreateNbPlayersNumbers(); |
| 292 | +} |
| 293 | + |
| 294 | +void SceneCreateRoom::CreateNbPlayersLabel() { |
| 295 | + const auto nb_players_text = registry_->SpawnEntity(); |
| 296 | + const auto point = Vector3f(CREATE_ROOM_PIXEL_LEFT, managers_.window->GetHeight() / 2 + 50); |
| 297 | + const auto aligns = Alignment{HorizontalAlign::kRight, VerticalAlign::kCenter}; |
| 298 | + |
| 299 | + registry_->AddComponent<Position>(nb_players_text, {point, aligns}); |
| 300 | + registry_->AddComponent<Drawable>(nb_players_text, |
| 301 | + {Text{"Nb Players :", "main", 20}, WindowManager::View::HUD}); |
| 302 | +} |
| 303 | + |
| 304 | +void SceneCreateRoom::CreateNbPlayersNumbers() { |
| 305 | + for (unsigned int i = 1; i <= 4; i++) { |
| 306 | + CreateNbPlayersNumber(i, {CREATE_ROOM_PIXEL_LEFT + 40 + static_cast<float>(i - 1) * 40, |
| 307 | + managers_.window->GetHeight() / 2 + 50}); |
| 308 | + } |
| 309 | +} |
| 310 | + |
| 311 | +void SceneCreateRoom::CreateNbPlayersNumber(unsigned int number, const sf::Vector2f& pos) { |
| 312 | + const auto nb_players_number = registry_->SpawnEntity(); |
| 313 | + constexpr auto aligns = Alignment{HorizontalAlign::kCenter, VerticalAlign::kCenter}; |
| 314 | + |
| 315 | + registry_->AddComponent<Position>(nb_players_number, {Vector3f{pos.x, pos.y, 0}, aligns}); |
| 316 | + registry_->AddComponent<Drawable>( |
| 317 | + nb_players_number, {Text{std::to_string(number), "main", 20}, WindowManager::View::HUD}); |
| 318 | + registry_->AddComponent<OnMousePressed>( |
| 319 | + nb_players_number, |
| 320 | + OnMousePressed{.strategy = MouseEventTarget::kLocalTarget, |
| 321 | + .handler = [this, nb_players_number, number](const sf::Mouse::Button& button, |
| 322 | + const sf::Vector2f&, |
| 323 | + const MouseEventTarget& target) { |
| 324 | + if (button == sf::Mouse::Button::Left) { |
| 325 | + nbPlayers_ = number + 1; |
| 326 | + auto drawables = registry_->GetComponents<Drawable>(); |
| 327 | + |
| 328 | + auto& dr = (*drawables)[static_cast<std::size_t>(nb_players_number)]; |
| 329 | + |
| 330 | + if (dr) { |
| 331 | + auto& drawable = dr.value(); |
| 332 | + auto& variant = drawable.drawable; |
| 333 | + |
| 334 | + if (std::holds_alternative<Text>(variant)) { |
| 335 | + auto& text = std::get<Text>(variant); |
| 336 | + if (target == MouseEventTarget::kLocalTarget) { |
| 337 | + text.style = sf::Text::Style::Underlined; |
| 338 | + } else { |
| 339 | + text.style = sf::Text::Style::Regular; |
| 340 | + } |
| 341 | + } |
| 342 | + } |
| 343 | + } |
| 344 | + }}); |
| 345 | + |
| 346 | + registry_->AddComponent<OnMouseMoved>( |
| 347 | + nb_players_number, |
| 348 | + OnMouseMoved{.strategy = MouseEventTarget::kAnyTarget, |
| 349 | + .handler = [this, nb_players_number, number](const sf::Vector2f& pos, |
| 350 | + const MouseEventTarget& target) { |
| 351 | + auto drawables = registry_->GetComponents<Drawable>(); |
| 352 | + |
| 353 | + auto& dr = (*drawables)[static_cast<std::size_t>(nb_players_number)]; |
| 354 | + |
| 355 | + if (dr) { |
| 356 | + auto& drawable = dr.value(); |
| 357 | + auto& variant = drawable.drawable; |
| 358 | + |
| 359 | + if (std::holds_alternative<Text>(variant)) { |
| 360 | + auto& text = std::get<Text>(variant); |
| 361 | + if (target == MouseEventTarget::kLocalTarget) { |
| 362 | + text.style = sf::Text::Style::Underlined; |
| 363 | + } else { |
| 364 | + if (nbPlayers_ != number + 1) { |
| 365 | + text.style = sf::Text::Style::Regular; |
| 366 | + } |
| 367 | + } |
| 368 | + } |
| 369 | + } |
| 370 | + }}); |
| 371 | +} |
0 commit comments