diff options
author | Star Rauchenberger <fefferburbia@gmail.com> | 2021-03-13 12:24:05 -0500 |
---|---|---|
committer | Star Rauchenberger <fefferburbia@gmail.com> | 2021-03-13 15:34:50 -0500 |
commit | fce37403bbc29521b2b5bd983291b3730f8ad7b4 (patch) | |
tree | c7bdf0fa62f6d67f85b97a7cda746da31470def2 /src/menu_system.cpp | |
parent | c6b5e936ff9869d8a3de9ea41db784a4cb46a818 (diff) | |
download | tanetane-fce37403bbc29521b2b5bd983291b3730f8ad7b4.tar.gz tanetane-fce37403bbc29521b2b5bd983291b3730f8ad7b4.tar.bz2 tanetane-fce37403bbc29521b2b5bd983291b3730f8ad7b4.zip |
Added submenus
#7
Diffstat (limited to 'src/menu_system.cpp')
-rw-r--r-- | src/menu_system.cpp | 95 |
1 files changed, 61 insertions, 34 deletions
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 @@ | |||
4 | 4 | ||
5 | void MenuSystem::tick(double dt) { | 5 | void MenuSystem::tick(double dt) { |
6 | pauseAnimation_.tick(dt); | 6 | pauseAnimation_.tick(dt); |
7 | menuChangeAnimation_.tick(dt); | ||
7 | 8 | ||
8 | if (openState_ == OpenState::Animating && pauseAnimation_.isComplete()) { | 9 | if (openState_ == OpenState::Animating && pauseAnimation_.isComplete()) { |
9 | if (pauseAnimation_.getProgress() == 0.0) { | 10 | if (pauseAnimation_.getProgress() == 0.0) { |
10 | openState_ = OpenState::Closed; | 11 | openState_ = OpenState::Closed; |
12 | menus_.clear(); | ||
11 | 13 | ||
12 | game_.unpauseGameplay(); | 14 | game_.unpauseGameplay(); |
13 | game_.getMixer().unpauseSounds(); | 15 | game_.getMixer().unpauseSounds(); |
@@ -15,20 +17,28 @@ void MenuSystem::tick(double dt) { | |||
15 | openState_ = OpenState::Open; | 17 | openState_ = OpenState::Open; |
16 | } | 18 | } |
17 | } else if (openState_ == OpenState::Open) { | 19 | } else if (openState_ == OpenState::Open) { |
18 | cursorBobTimer_.accumulate(dt); | 20 | if (!isMenuChanging_) { |
19 | while (cursorBobTimer_.step()) { | 21 | cursorBobTimer_.accumulate(dt); |
20 | if (cursorBobDown_) { | 22 | while (cursorBobTimer_.step()) { |
21 | cursorBob_++; | 23 | if (cursorBobDown_) { |
22 | 24 | cursorBob_++; | |
23 | if (cursorBob_ >= 4) { | 25 | |
24 | cursorBobDown_ = false; | 26 | if (cursorBob_ >= 4) { |
27 | cursorBobDown_ = false; | ||
28 | } | ||
29 | } else { | ||
30 | cursorBob_--; | ||
31 | |||
32 | if (cursorBob_ <= 0) { | ||
33 | cursorBobDown_ = true; | ||
34 | } | ||
25 | } | 35 | } |
26 | } else { | 36 | } |
27 | cursorBob_--; | 37 | } else if (menuChangeAnimation_.isComplete()) { |
38 | isMenuChanging_ = false; | ||
28 | 39 | ||
29 | if (cursorBob_ <= 0) { | 40 | if (menuChangeAnimation_.getProgress() == 0.0) { |
30 | cursorBobDown_ = true; | 41 | menus_.pop_back(); |
31 | } | ||
32 | } | 42 | } |
33 | } | 43 | } |
34 | } | 44 | } |
@@ -41,7 +51,16 @@ void MenuSystem::openPauseMenu() { | |||
41 | game_.pauseGameplay(); | 51 | game_.pauseGameplay(); |
42 | 52 | ||
43 | std::vector<MenuBuilder> builders = { | 53 | std::vector<MenuBuilder> builders = { |
44 | MenuBuilder().Command("Settings"), | 54 | MenuBuilder().Command("Settings") |
55 | .ActivationFunction([this] (Game&) { | ||
56 | openSubmenu(Menu({ | ||
57 | MenuBuilder().Command("Back") | ||
58 | .ActivationFunction([this] (Game& game) { | ||
59 | closePauseMenu(); | ||
60 | }) | ||
61 | .SkipSoundEffect() | ||
62 | })); | ||
63 | }), | ||
45 | MenuBuilder().Command("Resume Game") | 64 | MenuBuilder().Command("Resume Game") |
46 | .ActivationFunction([] (Game& game) { | 65 | .ActivationFunction([] (Game& game) { |
47 | game.getSystem<MenuSystem>().closePauseMenu(); | 66 | game.getSystem<MenuSystem>().closePauseMenu(); |
@@ -61,29 +80,32 @@ void MenuSystem::openPauseMenu() { | |||
61 | })); | 80 | })); |
62 | } | 81 | } |
63 | 82 | ||
64 | menu_ = CreateMenu(builders); | 83 | menus_.emplace_back(builders); |
65 | |||
66 | cursor_ = 0; | ||
67 | 84 | ||
68 | game_.getMixer().pauseSounds(); | 85 | game_.getMixer().pauseSounds(); |
69 | game_.getMixer().playSound("../res/sfx/menu_open.wav"); | 86 | game_.getMixer().playSound("../res/sfx/menu_open.wav"); |
70 | } | 87 | } |
71 | 88 | ||
72 | void MenuSystem::closePauseMenu(bool playSfx) { | 89 | void MenuSystem::closePauseMenu(bool playSfx) { |
73 | pauseAnimation_.start(125, 0.0); | 90 | if (menus_.size() > 1) { |
74 | openState_ = OpenState::Animating; | 91 | isMenuChanging_ = true; |
92 | menuChangeAnimation_.start(250, 0.0); | ||
93 | |||
94 | if (playSfx) { | ||
95 | game_.getMixer().playSound("../res/sfx/menu_deselect.wav"); | ||
96 | } | ||
97 | } else { | ||
98 | pauseAnimation_.start(125, 0.0); | ||
99 | openState_ = OpenState::Animating; | ||
75 | 100 | ||
76 | if (playSfx) { | 101 | if (playSfx) { |
77 | game_.getMixer().playSound("../res/sfx/menu_close.wav"); | 102 | game_.getMixer().playSound("../res/sfx/menu_close.wav"); |
103 | } | ||
78 | } | 104 | } |
79 | } | 105 | } |
80 | 106 | ||
81 | void MenuSystem::pressedUp() { | 107 | void MenuSystem::pressedUp() { |
82 | cursor_--; | 108 | menus_.back().moveCursorUp(); |
83 | |||
84 | if (cursor_ < 0) { | ||
85 | cursor_ = menu_.size() - 1; | ||
86 | } | ||
87 | 109 | ||
88 | cursorBob_ = 0; | 110 | cursorBob_ = 0; |
89 | cursorBobDown_ = true; | 111 | cursorBobDown_ = true; |
@@ -92,11 +114,7 @@ void MenuSystem::pressedUp() { | |||
92 | } | 114 | } |
93 | 115 | ||
94 | void MenuSystem::pressedDown() { | 116 | void MenuSystem::pressedDown() { |
95 | cursor_++; | 117 | menus_.back().moveCursorDown(); |
96 | |||
97 | if (cursor_ >= menu_.size()) { | ||
98 | cursor_ = 0; | ||
99 | } | ||
100 | 118 | ||
101 | cursorBob_ = 0; | 119 | cursorBob_ = 0; |
102 | cursorBobDown_ = true; | 120 | cursorBobDown_ = true; |
@@ -105,13 +123,22 @@ void MenuSystem::pressedDown() { | |||
105 | } | 123 | } |
106 | 124 | ||
107 | void MenuSystem::activateOption() { | 125 | void MenuSystem::activateOption() { |
108 | if (menu_[cursor_].type == MenuType::Command) { | 126 | Menu& curMenu = menus_.back(); |
109 | if (menu_[cursor_].playSfx) { | 127 | const MenuItem& menuItem = curMenu.getItems()[curMenu.getCursorPosition()]; |
128 | |||
129 | if (menuItem.type == MenuType::Command) { | ||
130 | if (menuItem.playSfx) { | ||
110 | game_.getMixer().playSound("../res/sfx/menu_activate.wav"); | 131 | game_.getMixer().playSound("../res/sfx/menu_activate.wav"); |
111 | } | 132 | } |
112 | 133 | ||
113 | if (menu_[cursor_].activationFunction) { | 134 | if (menuItem.activationFunction) { |
114 | menu_[cursor_].activationFunction(game_); | 135 | menuItem.activationFunction(game_); |
115 | } | 136 | } |
116 | } | 137 | } |
117 | } | 138 | } |
139 | |||
140 | void MenuSystem::openSubmenu(Menu submenu) { | ||
141 | menus_.push_back(std::move(submenu)); | ||
142 | isMenuChanging_ = true; | ||
143 | menuChangeAnimation_.start(250, 1.0); | ||
144 | } | ||