summary refs log tree commit diff stats
path: root/src/message_system.h
blob: 7f9d2879a8c60649f2d6ea9574e2a3c260830007 (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
#ifndef MESSAGE_SYSTEM_H_DE10D011
#define MESSAGE_SYSTEM_H_DE10D011

#include <list>
#include <string_view>
#include <string>
#include "system.h"
#include "timer.h"

class Game;

enum class SpeakerType {
  None,
  Man,
  Woman,
  Boy,
  Girl,
  Nonhuman
};

struct MessageLine {
  std::string text;
  int charsRevealed = 0;
  bool pause = false;
  bool isChoice = false;
  int choicePos[2] = {-1, -1};
  bool bulleted = false;
};

class MessageSystem : public System {
public:

  static constexpr SystemKey Key = SystemKey::Message;

  MessageSystem(Game& game) : game_(game) {}

  void tick(double dt) override;

  // Commands

  void displayCutsceneBars();

  void hideCutsceneBars();

  // Adds text to the message queue. Separate lines with \n.
  // \f is a special character -- put it after a \n to indicate that a button
  // press is required to advance text.
  void displayMessage(
    std::string_view msg,
    std::string speakerName = "",
    SpeakerType speakerType = SpeakerType::None);

  void showChoice(std::string choice1, std::string choice2);

  void advanceText();

  void selectFirstChoice();

  void selectSecondChoice();

  // Info

  double getCutsceneBarsProgress() const;

  const std::list<MessageLine>& getLines() const { return linesToShow_; }

  bool isMessageActive() const { return !linesToShow_.empty(); }

  const std::string& getSpeaker() const { return speakerName_; }

  bool isNextArrowShowing() const { return showNextArrow_; }

  int getNextArrowBob() const { return nextArrowBobPos_; }

  bool isChoiceActive() const { return isMessageActive() && linesToShow_.back().isChoice; }

  int getChoiceSelection() const { return choiceSelection_; }

private:

  enum class BarsState {
    Closed,
    Opening,
    Open,
    Closing
  };

  Game& game_;
  BarsState barsState_ = BarsState::Closed;
  double accum_ = 0.0;
  double length_ = 1000.0/8;
  SpeakerType speaker_ = SpeakerType::None;
  std::string speakerName_;
  std::list<MessageLine> lines_;
  std::list<MessageLine> linesToShow_;
  Timer textAdvTimer_ { 15 };
  bool showNextArrow_ = false;
  int nextArrowBobPos_ = 0;
  bool nextArrowBobDown_ = true;
  Timer nextArrowBobTimer_ { 125 };
  int choiceSelection_ = 0;
};

#endif /* end of include guard: MESSAGE_SYSTEM_H_DE10D011 */