summary refs log tree commit diff stats
path: root/src/menu_system.cpp
blob: 7ac8af51ef85daf6043f2565121dfaac626fb0a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "menu_system.h"
#include "game.h"
#include "script_system.h"

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();
    } else if (pauseAnimation_.getProgress() == 1.0) {
      openState_ = OpenState::Open;
    }
  } else if (openState_ == OpenState::Open) {
    if (!isMenuChanging_) {
      cursorBobTimer_.accumulate(dt);
      while (cursorBobTimer_.step()) {
        if (cursorBobDown_) {
          cursorBob_++;

          if (cursorBob_ >= 4) {
            cursorBobDown_ = false;
          }
        } else {
          cursorBob_--;

          if (cursorBob_ <= 0) {
            cursorBobDown_ = true;
          }
        }
      }
    } else if (menuChangeAnimation_.isComplete()) {
      isMenuChanging_ = false;

      if (menuChangeAnimation_.getProgress() == 0.0) {
        menus_.pop_back();
      }
    }
  }
}

void MenuSystem::openPauseMenu() {
  pauseAnimation_.start(125, 1.0);
  openState_ = OpenState::Animating;

  game_.pauseGameplay();

  std::vector<MenuBuilder> builders = {
    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<MenuSystem>().closePauseMenu();
                 })
                 .SkipSoundEffect(),
    MenuBuilder().Command("Quit")
                 .ActivationFunction([] (Game& game) {
                   game.quit();
                 })
  };

  if (game_.getMap().hasExitArea()) {
    builders.push_back(MenuBuilder().Command("Exit Area")
                                    .ActivationFunction([] (Game& game) {
                                      game.getSystem<MenuSystem>().closePauseMenu(false);
                                      game.getSystem<ScriptSystem>().runScript(game.getMap().getName(), "exit_area");
                                    }));
  }

  menus_.emplace_back(builders);

  game_.getMixer().pauseSounds();
  game_.getMixer().playSound("../res/sfx/menu_open.wav");
}

void MenuSystem::closePauseMenu(bool playSfx) {
  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");
    }
  }
}

void MenuSystem::pressedUp() {
  menus_.back().moveCursorUp();

  cursorBob_ = 0;
  cursorBobDown_ = true;

  game_.getMixer().playSound("../res/sfx/vertical_menu.wav");
}

void MenuSystem::pressedDown() {
  menus_.back().moveCursorDown();

  cursorBob_ = 0;
  cursorBobDown_ = true;

  game_.getMixer().playSound("../res/sfx/vertical_menu.wav");
}

void MenuSystem::activateOption() {
  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 (menuItem.activationFunction) {
      menuItem.activationFunction(game_);
    }
  }
}

void MenuSystem::openSubmenu(Menu submenu) {
  menus_.push_back(std::move(submenu));
  isMenuChanging_ = true;
  menuChangeAnimation_.start(250, 1.0);
}