|
| 1 | +/* |
| 2 | +** EPITECH PROJECT, 2024 |
| 3 | +** rtype |
| 4 | +** File description: |
| 5 | +** leaderboard.cpp |
| 6 | +*/ |
| 7 | + |
| 8 | +#include "end.hpp" |
| 9 | + |
| 10 | +#include "client/src/systems/leaderboard/sync.hpp" |
| 11 | +#include "constants/game.hpp" |
| 12 | +#include "constants/settings.hpp" |
| 13 | +#include "libs/mew/src/sets/events/events.hpp" |
| 14 | +#include "menu.hpp" |
| 15 | + |
| 16 | +using namespace rtype::client; |
| 17 | +using namespace rtype::client::scenes; |
| 18 | +using namespace mew::sets::events; |
| 19 | +using namespace mew::sets; |
| 20 | +using namespace mew::sets::drawable; |
| 21 | +using namespace mew::managers; |
| 22 | +using namespace zygarde::core::components; |
| 23 | +using namespace zygarde::core::types; |
| 24 | +using namespace rtype::client::systems; |
| 25 | +using namespace rtype::client::services; |
| 26 | + |
| 27 | +SceneEnd::SceneEnd(DependenciesHandler::Ptr services) : SceneBase(std::move(services)) { |
| 28 | + serverConnectionService_ = services_->GetOrThrow<ServerConnectionService>(); |
| 29 | + settingsManager_ = services_->GetOrThrow<SettingsManager>(); |
| 30 | +} |
| 31 | + |
| 32 | +void SceneEnd::OnCreate() { |
| 33 | + const Vector3f origin{managers_.window->GetWidth() / 2, managers_.window->GetHeight() / 2}; |
| 34 | + constexpr Alignment textAligns{HorizontalAlign::kCenter, VerticalAlign::kCenter}; |
| 35 | + |
| 36 | + CreateWin(origin, textAligns); |
| 37 | + CreateScore(origin, textAligns); |
| 38 | + CreateTime(origin, textAligns); |
| 39 | + CreateBackButton(); |
| 40 | + CreateMainEntity(); |
| 41 | +} |
| 42 | + |
| 43 | +void SceneEnd::OnActivate() {} |
| 44 | + |
| 45 | +void SceneEnd::CreateMainEntity() const { |
| 46 | + const auto main = registry_->SpawnEntity(); |
| 47 | + |
| 48 | + registry_->AddComponent<OnMousePressed>( |
| 49 | + main, |
| 50 | + OnMousePressed{.strategy = MouseEventTarget::kAnyTarget, |
| 51 | + .handler = [this](const sf::Mouse::Button &button, const sf::Vector2f &pos, |
| 52 | + const MouseEventTarget &target) { |
| 53 | + if (button == sf::Mouse::Button::Left) { |
| 54 | + managers_.sound->PlaySound("buttons:click"); |
| 55 | + } |
| 56 | + }}); |
| 57 | +} |
| 58 | + |
| 59 | +void SceneEnd::CreateBackButton() const { |
| 60 | + const auto exit_button = registry_->SpawnEntity(); |
| 61 | + const auto aligns = Alignment{HorizontalAlign::kCenter, VerticalAlign::kCenter}; |
| 62 | + const auto point = Vector3f(managers_.window->GetWidth() / 2, managers_.window->GetHeight() - 50); |
| 63 | + |
| 64 | + registry_->AddComponent<Position>(exit_button, {point, aligns}); |
| 65 | + registry_->AddComponent<Drawable>(exit_button, |
| 66 | + {Text{"Back", "main", 20}, WindowManager::View::HUD}); |
| 67 | + registry_->AddComponent<OnMousePressed>( |
| 68 | + exit_button, |
| 69 | + OnMousePressed{.strategy = MouseEventTarget::kLocalTarget, |
| 70 | + .handler = [this](const sf::Mouse::Button &button, const sf::Vector2f &pos, |
| 71 | + const MouseEventTarget &target) { |
| 72 | + if (button == sf::Mouse::Button::Left) { |
| 73 | + managers_.scenes->GoToScene<SceneMenu>(); |
| 74 | + } |
| 75 | + }}); |
| 76 | + registry_->AddComponent<OnMouseMoved>( |
| 77 | + exit_button, OnMouseMoved{.strategy = MouseEventTarget::kAnyTarget, |
| 78 | + .handler = [this, exit_button](const sf::Vector2f &pos, |
| 79 | + const MouseEventTarget &target) { |
| 80 | + auto drawables = registry_->GetComponents<Drawable>(); |
| 81 | + |
| 82 | + auto &dr = (*drawables)[static_cast<std::size_t>(exit_button)]; |
| 83 | + |
| 84 | + if (dr) { |
| 85 | + auto &drawable = dr.value(); |
| 86 | + auto &variant = drawable.drawable; |
| 87 | + |
| 88 | + if (std::holds_alternative<Text>(variant)) { |
| 89 | + auto &text = std::get<Text>(variant); |
| 90 | + if (target == MouseEventTarget::kLocalTarget) { |
| 91 | + text.style = sf::Text::Style::Underlined; |
| 92 | + } else { |
| 93 | + text.style = sf::Text::Style::Regular; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + }}); |
| 98 | +} |
| 99 | + |
| 100 | +void SceneEnd::CreateWin(const zygarde::core::types::Vector3f &origin, |
| 101 | + const Alignment &aligns) const { |
| 102 | + const auto win_entity = registry_->SpawnEntity(); |
| 103 | + const auto point = Vector3f(origin.x, origin.y - 150); |
| 104 | + const bool win = settingsManager_->Get<bool>(GAME_END_WIN); |
| 105 | + const auto text = win ? "Win" : "Lose"; |
| 106 | + const auto color = win ? sf::Color::Green : sf::Color::Red; |
| 107 | + |
| 108 | + registry_->AddComponent<Position>(win_entity, {point, aligns}); |
| 109 | + registry_->AddComponent<Drawable>( |
| 110 | + win_entity, {Text{text, "main", 50, sf::Text::Style::Bold, color}, WindowManager::View::HUD}); |
| 111 | +} |
| 112 | + |
| 113 | +void SceneEnd::CreateScore(const zygarde::core::types::Vector3f &origin, |
| 114 | + const Alignment &aligns) const { |
| 115 | + const auto title = registry_->SpawnEntity(); |
| 116 | + const auto point = Vector3f(origin.x, origin.y); |
| 117 | + const auto score = settingsManager_->Get<std::size_t>(GAME_END_SCORE); |
| 118 | + const auto text = "Score: " + std::to_string(score); |
| 119 | + |
| 120 | + registry_->AddComponent<Position>(title, {point, aligns}); |
| 121 | + registry_->AddComponent<Drawable>(title, {Text{text, "main", 25}, WindowManager::View::HUD}); |
| 122 | +} |
| 123 | + |
| 124 | +void SceneEnd::CreateTime(const zygarde::core::types::Vector3f &origin, |
| 125 | + const Alignment &aligns) const { |
| 126 | + const auto info = registry_->SpawnEntity(); |
| 127 | + const auto point = Vector3f(origin.x, origin.y + 30); |
| 128 | + const auto time = settingsManager_->Get<time_t>(GAME_END_TIME); |
| 129 | + |
| 130 | + const auto *localTime = std::localtime(&time); |
| 131 | + |
| 132 | + char buffer[6] = {0}; |
| 133 | + std::strftime(buffer, sizeof(buffer), "%M:%S", localTime); |
| 134 | + const std::string text_time(buffer); |
| 135 | + const std::string text = "Time: " + text_time; |
| 136 | + |
| 137 | + registry_->AddComponent<Position>(info, {point, aligns}); |
| 138 | + registry_->AddComponent<Drawable>(info, {Text{text, "main", 20}, WindowManager::View::HUD}); |
| 139 | +} |
0 commit comments