From cddeeaf3f9031219e205efb4ed6dc8d34293d248 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sun, 20 Jun 2021 12:32:42 +0200 Subject: [PATCH] Fixing #255 --- sources/Runner/LobbyScene.cpp | 1 - sources/System/Lobby/LobbySystem.cpp | 40 +++++++++++-------- sources/System/Lobby/LobbySystem.hpp | 4 +- .../MenuControllableSystem.cpp | 37 ++++++++--------- .../MenuControllableSystem.hpp | 6 +-- 5 files changed, 49 insertions(+), 39 deletions(-) diff --git a/sources/Runner/LobbyScene.cpp b/sources/Runner/LobbyScene.cpp index 92f54565..6eb8d88b 100644 --- a/sources/Runner/LobbyScene.cpp +++ b/sources/Runner/LobbyScene.cpp @@ -186,7 +186,6 @@ namespace BBM .addComponent("assets/player/icons/none.png"); auto &ready = scene->addEntity("ready") .addComponent(224 * (i + 1) + 200 * i, 1080 / 3 - 50, 0) - // todo check why it does this | hacky way to fix ready texture .addComponent(); player.addComponent(i, ready, playerTile); } diff --git a/sources/System/Lobby/LobbySystem.cpp b/sources/System/Lobby/LobbySystem.cpp index 0a10d8ae..2bd34e1a 100644 --- a/sources/System/Lobby/LobbySystem.cpp +++ b/sources/System/Lobby/LobbySystem.cpp @@ -18,7 +18,6 @@ #include #include #include -#include #include #include "Component/Color/ColorComponent.hpp" #include "Component/Stat/StatComponent.hpp" @@ -66,7 +65,7 @@ namespace BBM lobby.coloredTile.getComponent().drawable->setColor(_rayColors[lobby.color]); } - void LobbySystem::onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds dtime) + void LobbySystem::onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds) { auto &lobby = entity.get(); @@ -77,7 +76,7 @@ namespace BBM if (lobby.layout == ControllableComponent::NONE) { for (auto &[_, ctrl] : this->_wal.getScene()->view()) { auto &controller = ctrl; - if (controller.bomb) { + if (controller.bomb && this->_canJoin()) { if (std::any_of(this->getView().begin(), this->getView().end(), [&controller](WAL::ViewEntity &view) { return view.get().layout == controller.layout; })) @@ -95,7 +94,7 @@ namespace BBM for (auto &[_, controller] : this->_wal.getScene()->view()) { if (controller.layout != lobby.layout) continue; - if (controller.bomb && !lobby.ready) { + if (controller.bomb && !lobby.ready && this->_canJoin()) { lobby.ready = true; lobby.lastInput = lastTick; controller.bomb = false; @@ -104,13 +103,22 @@ namespace BBM if (texture) texture->use("assets/player/icons/ready.png"); } - if (controller.secondary && !lobby.ready) { + if (controller.secondary) { lobby.lastInput = lastTick; this->_nextColor(entity); } } } + bool LobbySystem::_canJoin() + { + auto *button = this->_wal.getSystem().currentButton; + + if (!button) + return true; + return button->hasComponent>(); + } + void LobbySystem::addAI() { for (auto entity : this->getView()) { @@ -152,7 +160,7 @@ namespace BBM texture->unload(); } - void LobbySystem::onSelfUpdate(std::chrono::nanoseconds dtime) + void LobbySystem::onSelfUpdate(std::chrono::nanoseconds) { auto &view = this->_wal.getScene()->view, Drawable2DComponent>(); if (view.size() == 0) @@ -209,7 +217,7 @@ namespace BBM player.getComponent().layout = layout; } - void LobbySystem::createTile(std::shared_ptr scene, WAL::Entity &player, int color, int playerCount) + void LobbySystem::createTile(const std::shared_ptr& scene, WAL::Entity &player, int color, int playerCount) { std::string texturePath = "assets/player/ui/" + colors[color] + ".png"; int x = (playerCount % 2 == 0) ? 1920 - 10 - 320 : 10; @@ -223,25 +231,25 @@ namespace BBM scene->addEntity("player hide fireup") .addComponent(x + 220, y + 35, 0) .addComponent("", 20, x, y, WHITE) - .addComponent([&player](Drawable2DComponent &drawble) { + .addComponent([&player](Drawable2DComponent &drawable) { const BombHolderComponent *bonus = player.tryGetComponent(); if (!bonus) return; - RAY2D::Text *text = dynamic_cast(drawble.drawable.get()); + auto *text = dynamic_cast(drawable.drawable.get()); if (!text) return; text->setText(std::to_string(static_cast(bonus->explosionRadius))); }); - scene->addEntity("player hide bombup") + scene->addEntity("player hide bomb-up") .addComponent(x + 220, y + 77, 0) .addComponent("", 20, x, y, WHITE) - .addComponent([&player](Drawable2DComponent &drawble) { + .addComponent([&player](Drawable2DComponent &drawable) { const BombHolderComponent *bonus = player.tryGetComponent(); if (!bonus) return; - RAY2D::Text *text = dynamic_cast(drawble.drawable.get()); + auto *text = dynamic_cast(drawable.drawable.get()); if (!text) return; text->setText(std::to_string(bonus->bombCount) + " / " + std::to_string(bonus->maxBombCount)); @@ -249,12 +257,12 @@ namespace BBM scene->addEntity("player hide speedup") .addComponent(x + 220, y + 122, 0) .addComponent("", 20, x, y, WHITE) - .addComponent([&player](Drawable2DComponent &drawble) { + .addComponent([&player](Drawable2DComponent &drawable) { auto *speed = player.tryGetComponent(); if (!speed) return; - RAY2D::Text *text = dynamic_cast(drawble.drawable.get()); + auto *text = dynamic_cast(drawable.drawable.get()); if (!text) return; text->setText(std::to_string(static_cast(speed->speed * 100))); @@ -262,12 +270,12 @@ namespace BBM scene->addEntity("player hide wall") .addComponent(x + 220, y + 161, 0) .addComponent("", 20, x, y, WHITE) - .addComponent([&player](Drawable2DComponent &drawble) { + .addComponent([&player](Drawable2DComponent &drawable) { const PlayerBonusComponent *bonus = player.tryGetComponent(); if (!bonus) return; - RAY2D::Text *text = dynamic_cast(drawble.drawable.get()); + auto *text = dynamic_cast(drawable.drawable.get()); if (!text) return; text->setText(bonus->isNoClipOn ? "YES" : "NO"); diff --git a/sources/System/Lobby/LobbySystem.hpp b/sources/System/Lobby/LobbySystem.hpp index 67878d85..4e4e7aaa 100644 --- a/sources/System/Lobby/LobbySystem.hpp +++ b/sources/System/Lobby/LobbySystem.hpp @@ -18,6 +18,8 @@ namespace BBM { private: + bool _canJoin(); + void _nextColor(WAL::ViewEntity &entity); @@ -31,7 +33,7 @@ namespace BBM static void addController(WAL::Entity &player, ControllableComponent::Layout layout); //! @brief Create ingame tile - static void createTile(std::shared_ptr scene, WAL::Entity &player, int color, int playerCount); + static void createTile(const std::shared_ptr& drawable, WAL::Entity &player, int color, int playerCount); //! @inherit void onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds dtime) override; diff --git a/sources/System/MenuControllable/MenuControllableSystem.cpp b/sources/System/MenuControllable/MenuControllableSystem.cpp index 766d575d..919786ea 100644 --- a/sources/System/MenuControllable/MenuControllableSystem.cpp +++ b/sources/System/MenuControllable/MenuControllableSystem.cpp @@ -18,13 +18,14 @@ namespace BBM { MenuControllableSystem::MenuControllableSystem(WAL::Wal &wal) : System(wal), - _currentButton(), _oldMousePosition(-1, -1) + _oldMousePosition(-1, -1), + currentButton() { } void MenuControllableSystem::_updateCurrentButton(bool selected, Vector2f move) { - auto &buttonComponent = this->_currentButton->getComponent(); + auto &buttonComponent = this->currentButton->getComponent(); WAL::Entity *newButton = nullptr; if (move.y > 0 && buttonComponent._up) @@ -44,12 +45,12 @@ namespace BBM } if (newButton) { - this->_currentButton->getComponent().onEvent(*this->_currentButton, this->_wal); - this->_currentButton = newButton; - this->_currentButton->getComponent().onEvent(*this->_currentButton, this->_wal); + this->currentButton->getComponent().onEvent(*this->currentButton, this->_wal); + this->currentButton = newButton; + this->currentButton->getComponent().onEvent(*this->currentButton, this->_wal); } if (selected) - this->_currentButton->getComponent().onEvent(*this->_currentButton, this->_wal); + this->currentButton->getComponent().onEvent(*this->currentButton, this->_wal); } bool MenuControllableSystem::_mouseOnButton(const Vector2f &mousePos, WAL::ViewEntity &entity) const @@ -83,16 +84,16 @@ namespace BBM if (this->_oldMousePosition == Vector2f(-1, -1)) this->_oldMousePosition = relativeMousePos; - if (this->_currentButton && this->_currentButton->_scene.getID() != this->_wal.getScene()->getID()) { - this->_currentButton->getComponent().onEvent(*this->_currentButton, this->_wal); - this->_currentButton = nullptr; + if (this->currentButton && this->currentButton->_scene.getID() != this->_wal.getScene()->getID()) { + this->currentButton->getComponent().onEvent(*this->currentButton, this->_wal); + this->currentButton = nullptr; return; } - if (this->_currentButton == nullptr && buttons.size()) { - this->_currentButton = &(*buttons.front()); - this->_currentButton->getComponent().onEvent(*this->_currentButton, this->_wal); + if (this->currentButton == nullptr && buttons.size()) { + this->currentButton = &(*buttons.front()); + this->currentButton->getComponent().onEvent(*this->currentButton, this->_wal); } - if (!this->_currentButton) + if (!this->currentButton) return; for (auto &[_, controllable]: controllableView) if (controllable.move.x || controllable.move.y || controllable.bomb) { @@ -104,12 +105,12 @@ namespace BBM this->_oldMousePosition = relativeMousePos; for (auto &entity: buttons) { if (_mouseOnButton(relativeMousePos, entity)) { - if (this->_currentButton) - this->_currentButton->getComponent().onEvent(*this->_currentButton, this->_wal); - this->_currentButton = &(*entity); - this->_currentButton->getComponent().onEvent(*this->_currentButton, this->_wal); + if (this->currentButton) + this->currentButton->getComponent().onEvent(*this->currentButton, this->_wal); + this->currentButton = &(*entity); + this->currentButton->getComponent().onEvent(*this->currentButton, this->_wal); if (RAYControl::Mouse::isPressed(RAYControl::Mouse::Button::MOUSE_BUTTON_LEFT)) - this->_currentButton->getComponent().onEvent(*this->_currentButton, this->_wal); + this->currentButton->getComponent().onEvent(*this->currentButton, this->_wal); } } } diff --git a/sources/System/MenuControllable/MenuControllableSystem.hpp b/sources/System/MenuControllable/MenuControllableSystem.hpp index 28fe0a60..15d71099 100644 --- a/sources/System/MenuControllable/MenuControllableSystem.hpp +++ b/sources/System/MenuControllable/MenuControllableSystem.hpp @@ -17,9 +17,6 @@ namespace BBM class MenuControllableSystem : public WAL::System<> { private: - //! @brief index of the current button selected - WAL::Entity *_currentButton; - //! @brief position of the mouse at the precedent scene (to know which controller event to watch) Vector2f _oldMousePosition; @@ -30,6 +27,9 @@ namespace BBM //! @return true if mouse on entity bool _mouseOnButton(const Vector2f &mousePos, WAL::ViewEntity &entity) const; public: + //! @brief index of the current button selected + WAL::Entity *currentButton; + //! @brief time (in millisecond) since last check std::chrono::time_point now; //! @inherit