summary refs log tree commit diff stats
path: root/src/menu_system.cpp
blob: 6b03fca549e0c461b830f0df10f35575106a1ccd (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
#include "menu_system.h"
#include "game.h"
#include "script_system.h"

void MenuSystem::tick(double dt) {
  pauseAnimation_.tick(dt);

  if (openState_ == OpenState::Animating && pauseAnimation_.isComplete()) {
    if (pauseAnimation_.getProgress() == 0.0) {
      openState_ = OpenState::Closed;

      game_.unpauseGameplay();
    } else if (pauseAnimation_.getProgress() == 1.0) {
      openState_ = OpenState::Open;
    }
  } else if (openState_ == OpenState::Open) {
    cursorBobTimer_.accumulate(dt);
    while (cursorBobTimer_.step()) {
      if (cursorBobDown_) {
        cursorBob_++;

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

        if (cursorBob_ <= 0) {
          cursorBobDown_ = true;
        }
      }
    }
  }
}

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

  game_.pauseGameplay();

  std::vector<MenuBuilder> builders = {
    MenuBuilder().Command("Settings"),
    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");
                                    }));
  }

  menu_ = CreateMenu(builders);

  cursor_ = 0;

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

void MenuSystem::closePauseMenu(bool playSfx) {
  pauseAnimation_.start(125, 0.0);
  openState_ = OpenState::Animating;

  if (playSfx) {
    game_.getMixer().playSound("../res/sfx/menu_close.wav");
  }
}

void MenuSystem::pressedUp() {
  cursor_--;

  if (cursor_ < 0) {
    cursor_ = menu_.size() - 1;
  }

  cursorBob_ = 0;
  cursorBobDown_ = true;

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

void MenuSystem::pressedDown() {
  cursor_++;

  if (cursor_ >= menu_.size()) {
    cursor_ = 0;
  }

  cursorBob_ = 0;
  cursorBobDown_ = true;

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

void MenuSystem::activateOption() {
  if (menu_[cursor_].type == MenuType::Command) {
    if (menu_[cursor_].playSfx) {
      game_.getMixer().playSound("../res/sfx/menu_activate.wav");
    }

    if (menu_[cursor_].activationFunction) {
      menu_[cursor_].activationFunction(game_);
    }
  }
}