From fce37403bbc29521b2b5bd983291b3730f8ad7b4 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sat, 13 Mar 2021 12:24:05 -0500 Subject: Added submenus #7 --- src/menu_system.cpp | 95 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 61 insertions(+), 34 deletions(-) (limited to 'src/menu_system.cpp') diff --git a/src/menu_system.cpp b/src/menu_system.cpp index 7631e4d..7ac8af5 100644 --- a/src/menu_system.cpp +++ b/src/menu_system.cpp @@ -4,10 +4,12 @@ void MenuSystem::tick(double dt) { pauseAnimation_.tick(dt); + menuChangeAnimation_.tick(dt); if (openState_ == OpenState::Animating && pauseAnimation_.isComplete()) { if (pauseAnimation_.getProgress() == 0.0) { openState_ = OpenState::Closed; + menus_.clear(); game_.unpauseGameplay(); game_.getMixer().unpauseSounds(); @@ -15,20 +17,28 @@ void MenuSystem::tick(double dt) { openState_ = OpenState::Open; } } else if (openState_ == OpenState::Open) { - cursorBobTimer_.accumulate(dt); - while (cursorBobTimer_.step()) { - if (cursorBobDown_) { - cursorBob_++; - - if (cursorBob_ >= 4) { - cursorBobDown_ = false; + if (!isMenuChanging_) { + cursorBobTimer_.accumulate(dt); + while (cursorBobTimer_.step()) { + if (cursorBobDown_) { + cursorBob_++; + + if (cursorBob_ >= 4) { + cursorBobDown_ = false; + } + } else { + cursorBob_--; + + if (cursorBob_ <= 0) { + cursorBobDown_ = true; + } } - } else { - cursorBob_--; + } + } else if (menuChangeAnimation_.isComplete()) { + isMenuChanging_ = false; - if (cursorBob_ <= 0) { - cursorBobDown_ = true; - } + if (menuChangeAnimation_.getProgress() == 0.0) { + menus_.pop_back(); } } } @@ -41,7 +51,16 @@ void MenuSystem::openPauseMenu() { game_.pauseGameplay(); std::vector builders = { - MenuBuilder().Command("Settings"), + MenuBuilder().Command("Settings") + .ActivationFunction([this] (Game&) { + openSubmenu(Menu({ + MenuBuilder().Command("Back") + .ActivationFunction([this] (Game& game) { + closePauseMenu(); + }) + .SkipSoundEffect() + })); + }), MenuBuilder().Command("Resume Game") .ActivationFunction([] (Game& game) { game.getSystem().closePauseMenu(); @@ -61,29 +80,32 @@ void MenuSystem::openPauseMenu() { })); } - menu_ = CreateMenu(builders); - - cursor_ = 0; + menus_.emplace_back(builders); game_.getMixer().pauseSounds(); game_.getMixer().playSound("../res/sfx/menu_open.wav"); } void MenuSystem::closePauseMenu(bool playSfx) { - pauseAnimation_.start(125, 0.0); - openState_ = OpenState::Animating; + if (menus_.size() > 1) { + isMenuChanging_ = true; + menuChangeAnimation_.start(250, 0.0); + + if (playSfx) { + game_.getMixer().playSound("../res/sfx/menu_deselect.wav"); + } + } else { + pauseAnimation_.start(125, 0.0); + openState_ = OpenState::Animating; - if (playSfx) { - game_.getMixer().playSound("../res/sfx/menu_close.wav"); + if (playSfx) { + game_.getMixer().playSound("../res/sfx/menu_close.wav"); + } } } void MenuSystem::pressedUp() { - cursor_--; - - if (cursor_ < 0) { - cursor_ = menu_.size() - 1; - } + menus_.back().moveCursorUp(); cursorBob_ = 0; cursorBobDown_ = true; @@ -92,11 +114,7 @@ void MenuSystem::pressedUp() { } void MenuSystem::pressedDown() { - cursor_++; - - if (cursor_ >= menu_.size()) { - cursor_ = 0; - } + menus_.back().moveCursorDown(); cursorBob_ = 0; cursorBobDown_ = true; @@ -105,13 +123,22 @@ void MenuSystem::pressedDown() { } void MenuSystem::activateOption() { - if (menu_[cursor_].type == MenuType::Command) { - if (menu_[cursor_].playSfx) { + Menu& curMenu = menus_.back(); + const MenuItem& menuItem = curMenu.getItems()[curMenu.getCursorPosition()]; + + if (menuItem.type == MenuType::Command) { + if (menuItem.playSfx) { game_.getMixer().playSound("../res/sfx/menu_activate.wav"); } - if (menu_[cursor_].activationFunction) { - menu_[cursor_].activationFunction(game_); + if (menuItem.activationFunction) { + menuItem.activationFunction(game_); } } } + +void MenuSystem::openSubmenu(Menu submenu) { + menus_.push_back(std::move(submenu)); + isMenuChanging_ = true; + menuChangeAnimation_.start(250, 1.0); +} -- cgit 1.4.1