From 8d9432c1f49b6c64fefde7f2dc9dc38edb961b06 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Fri, 18 Jun 2021 12:42:51 +0200 Subject: [PATCH] Handling buttons on key up --- .gitignore | 1 + .../Controllable/ControllableComponent.hpp | 2 ++ sources/Runner/Runner.cpp | 7 ++++++- sources/System/Gamepad/GamepadSystem.cpp | 2 +- sources/System/Keyboard/KeyboardSystem.cpp | 2 +- .../MenuControllable/MenuControllableSystem.cpp | 14 +++++++------- 6 files changed, 18 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index f93eff30..4ea2a783 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .idea cmake-build-debug +cmake-build-release ./bomberman .vscode build/* diff --git a/sources/Component/Controllable/ControllableComponent.hpp b/sources/Component/Controllable/ControllableComponent.hpp index f16de468..a3b28b04 100644 --- a/sources/Component/Controllable/ControllableComponent.hpp +++ b/sources/Component/Controllable/ControllableComponent.hpp @@ -41,6 +41,8 @@ namespace BBM float speed = .15f; //! @brief The layout used for this controllable. Layout layout = NONE; + //! @brief True if buttons should be triggered every frame where the key is down, false if the button should only be triggered once the key is released. + bool fastClick = false; //! @inherit WAL::Component *clone(WAL::Entity &entity) const override; diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 2c12210c..e554e14d 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -64,6 +64,7 @@ namespace BBM engine.shouldClose = true; if (gameState.currentScene == GameState::SceneID::GameScene) { for (auto &[_, component]: engine.getScene()->view()) { + component.fastClick = true; if (component.pause && gameState.currentScene == GameState::SceneID::GameScene) { gameState.nextScene = GameState::SceneID::PauseMenuScene; break; @@ -72,8 +73,12 @@ namespace BBM } if (gameState.nextScene == gameState.currentScene) return; - if (gameState.nextScene == GameState::SceneID::ScoreScene) + if (gameState.nextScene == GameState::SceneID::ScoreScene) { gameState._loadedScenes[GameState::SceneID::ScoreScene] = Runner::loadScoreScene(*engine.getScene()); + for (auto &[_, component]: engine.getScene()->view()) { + component.fastClick = false; + } + } gameState._loadedScenes[gameState.currentScene] = engine.getScene(); engine.changeScene(gameState._loadedScenes[gameState.nextScene]); gameState.currentScene = gameState.nextScene; diff --git a/sources/System/Gamepad/GamepadSystem.cpp b/sources/System/Gamepad/GamepadSystem.cpp index 7ff78016..627b6d56 100644 --- a/sources/System/Gamepad/GamepadSystem.cpp +++ b/sources/System/Gamepad/GamepadSystem.cpp @@ -30,7 +30,7 @@ namespace BBM }; for (auto key : keyPressedMap) - key.second = gamepad.isDown(key.first); + key.second = controllable.fastClick ? gamepad.isDown(key.first) : gamepad.isPressed(key.first); controllable.move.x = gamepad.getAxisValue(gamepadComponent.LeftStickX) * -1; controllable.move.y = gamepad.getAxisValue(gamepadComponent.LeftStickY) * -1; controllable.move.x -= static_cast(gamepad.isDown(gamepadComponent.keyRight)); diff --git a/sources/System/Keyboard/KeyboardSystem.cpp b/sources/System/Keyboard/KeyboardSystem.cpp index 8dbda5ab..7f539f44 100644 --- a/sources/System/Keyboard/KeyboardSystem.cpp +++ b/sources/System/Keyboard/KeyboardSystem.cpp @@ -28,7 +28,7 @@ namespace BBM }; for (auto key : keyPressedMap) - key.second = Keyboard::isDown(key.first); + key.second = controllable.fastClick ? Keyboard::isDown(key.first) : Keyboard ::isPressed(key.first); controllable.move = Vector2f(); if (Keyboard::isDown(keyboard.keyRight)) controllable.move.x -= 1; diff --git a/sources/System/MenuControllable/MenuControllableSystem.cpp b/sources/System/MenuControllable/MenuControllableSystem.cpp index f0115900..0b05031b 100644 --- a/sources/System/MenuControllable/MenuControllableSystem.cpp +++ b/sources/System/MenuControllable/MenuControllableSystem.cpp @@ -55,24 +55,24 @@ namespace BBM bool MenuControllableSystem::_mouseOnButton(const Vector2f &mousePos, WAL::ViewEntity &entity) const { auto &positionComponent = entity.get(); - RAY::Texture *texture = dynamic_cast(entity.get().drawable.get()); - RAY2D::Text *text = dynamic_cast(entity.get().drawable.get()); Vector2f buttonPos(positionComponent.getX(), positionComponent.getY()); Vector2f dimensions; - if (texture) { + if (auto *texture = dynamic_cast(entity.get().drawable.get())) { dimensions.x = texture->getDimensions().x; dimensions.y = texture->getDimensions().y; - } else if (text) { + } + else if (auto *text = dynamic_cast(entity.get().drawable.get())) { dimensions.y = text->getFontSize(); dimensions.x = text->getString().size() * (text->getFontSize()); - } else + } + else return false; return ((buttonPos.x <= mousePos.x && mousePos.x <= buttonPos.x + dimensions.x) - && (buttonPos.y <= mousePos.y && mousePos.y <= buttonPos.y + dimensions.y)); + && (buttonPos.y <= mousePos.y && mousePos.y <= buttonPos.y + dimensions.y)); } - void MenuControllableSystem::onSelfUpdate(std::chrono::nanoseconds dtime) + void MenuControllableSystem::onSelfUpdate(std::chrono::nanoseconds) { RAY::Vector2 rayMousePos = RAYControl::Mouse::getCursorPosition(); RAY::Vector2 winSize = RAY::Window::getInstance().getDimensions();