From f9dd0fbabe5348fcd9841978cdd9d3a5824a5dec Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sat, 13 Mar 2021 09:51:24 -0500 Subject: Pause menu commands can do something #7 --- res/sfx/menu_activate.wav | Bin 0 -> 62048 bytes src/input_system.cpp | 4 ++++ src/menu.h | 9 +++++++++ src/menu_system.cpp | 16 +++++++++++++++- src/menu_system.h | 2 ++ 5 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 res/sfx/menu_activate.wav diff --git a/res/sfx/menu_activate.wav b/res/sfx/menu_activate.wav new file mode 100644 index 0000000..22da52b Binary files /dev/null and b/res/sfx/menu_activate.wav differ diff --git a/src/input_system.cpp b/src/input_system.cpp index 0c68c49..25af7ed 100644 --- a/src/input_system.cpp +++ b/src/input_system.cpp @@ -75,6 +75,10 @@ void InputSystem::tick(double dt) { } } } else if (e.key.keysym.sym == SDLK_SPACE) { + if (game_.getSystem().isMenuOpen()) { + game_.getSystem().activateOption(); + } + if (game_.isGameplayPaused()) continue; // If there is text on screen, try to advance it. diff --git a/src/menu.h b/src/menu.h index 1b91b88..967b384 100644 --- a/src/menu.h +++ b/src/menu.h @@ -1,10 +1,13 @@ #ifndef MENU_H_3F6E62B3 #define MENU_H_3F6E62B3 +#include #include #include #include +class Game; + enum class MenuType { Command }; @@ -12,6 +15,7 @@ enum class MenuType { struct MenuItem { MenuType type = MenuType::Command; std::string text; + std::function activationFunction; }; class MenuBuilder { @@ -23,6 +27,11 @@ public: return *this; } + MenuBuilder& ActivationFunction(std::function val) { + result_.activationFunction = std::move(val); + return *this; + } + MenuItem Build() const { return result_; } diff --git a/src/menu_system.cpp b/src/menu_system.cpp index a6fd566..34616a6 100644 --- a/src/menu_system.cpp +++ b/src/menu_system.cpp @@ -40,8 +40,14 @@ void MenuSystem::openPauseMenu() { menu_ = CreateMenu({ MenuBuilder().Command("Settings"), - MenuBuilder().Command("Return to Main Menu"), MenuBuilder().Command("Resume Game") + .ActivationFunction([] (Game& game) { + game.getSystem().closePauseMenu(); + }), + MenuBuilder().Command("Quit") + .ActivationFunction([] (Game& game) { + game.quit(); + }) }); cursor_ = 0; @@ -77,3 +83,11 @@ void MenuSystem::pressedDown() { game_.getMixer().playSound("../res/sfx/vertical_menu.wav"); } + +void MenuSystem::activateOption() { + game_.getMixer().playSound("../res/sfx/menu_activate.wav"); + + if (menu_[cursor_].type == MenuType::Command && menu_[cursor_].activationFunction) { + menu_[cursor_].activationFunction(game_); + } +} diff --git a/src/menu_system.h b/src/menu_system.h index fa373cb..58a574f 100644 --- a/src/menu_system.h +++ b/src/menu_system.h @@ -28,6 +28,8 @@ public: void pressedDown(); + void activateOption(); + // Info double getPauseAnimationProgress() const { return pauseAnimation_.getProgress(); } -- cgit 1.4.1