summary refs log tree commit diff stats
path: root/src/message_system.cpp
blob: 5abb4b3c1901f41a489ccc9d1e81e15163d0b93e (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "message_system.h"
#include "game.h"
#include "util.h"

const int CHARS_TO_REVEAL = 1;
const int CHARS_PER_BEEP = 10;

void MessageSystem::tick(double dt) {
  if (barsState_ == BarsState::Opening || barsState_ == BarsState::Closing) {
    accum_ += dt;

    if (accum_ >= length_) {
      if (barsState_ == BarsState::Opening) {
        barsState_ = BarsState::Open;
      } else {
        barsState_ = BarsState::Closed;
      }
    }
  } else if (barsState_ == BarsState::Open) {
    if (!linesToShow_.empty()) {
      textAdvTimer_.accumulate(dt);
      while (textAdvTimer_.step()) {
        // Try to advance text on the first line that isn't totally revealed yet.
        bool advancedChars = false;
        for (MessageLine& line : linesToShow_) {
          if (line.charsRevealed < line.text.size()) {
            // Every so often play a beep.
            if (line.charsRevealed % CHARS_PER_BEEP == 0) {
              if (speaker_ == SpeakerType::Man) {
                game_.getMixer().playSound("../res/speaking_man.wav");
              } else if (speaker_ == SpeakerType::Woman) {
                game_.getMixer().playSound("../res/speaking_woman.wav");
              } else if (speaker_ == SpeakerType::Boy) {
                game_.getMixer().playSound("../res/speaking_boy.wav");
              } else if (speaker_ == SpeakerType::Girl) {
                game_.getMixer().playSound("../res/speaking_girl.wav");
              } else if (speaker_ == SpeakerType::Nonhuman) {
                game_.getMixer().playSound("../res/speaking_nonhuman.wav");
              }
            }

            line.charsRevealed += CHARS_TO_REVEAL;
            if (line.charsRevealed > line.text.size()) {
              line.charsRevealed = line.text.size();
            }
            advancedChars = true;
            break;
          }
        }

        if (!advancedChars) {
          // If both lines are totally revealed, see if we can scroll up a line.
          // This is doable as long as the next line isn't the sentinel value that
          // means an A press is required.
          if (!lines_.empty() && lines_.front() != "\n") {
            if (linesToShow_.size() == 2) {
              linesToShow_.pop_front();
            }

            linesToShow_.push_back(MessageLine { .text = lines_.front() });
            lines_.pop_front();
          }
        }
      }
    }
  }
}

void MessageSystem::displayCutsceneBars() {
  accum_ = 0.0;
  barsState_ = BarsState::Opening;
}

void MessageSystem::hideCutsceneBars() {
  accum_ = 0.0;
  barsState_ = BarsState::Closing;
}

void MessageSystem::displayMessage(std::string_view msg, SpeakerType speaker) {
  if (!(barsState_ == BarsState::Opening || barsState_ == BarsState::Open)) {
    displayCutsceneBars();
  }

  speaker_ = speaker;

  auto lineChunks = splitStr<std::list<std::string>>(std::string(msg), "\n");
  for (const std::string& text : lineChunks) {
    auto words = splitStr<std::list<std::string>>(text, " ");

    std::string curLine;
    int curWidth = 0;
    bool firstWord = true;
    bool shouldAddBlank = false;

    // I'm gonna be frank and admit it: I'm not gonna take hyphenation into
    // consideration. Please don't write any words that are wider than the
    // textbox.
    for (const std::string& word : words) {
      int wordWidth = 0;
      bool firstChar = true;
      for (int i=0; i<word.size(); i++) {
        if (firstChar) {
          firstChar = false;
        } else {
          wordWidth++;
        }

        wordWidth += game_.getFont().getCharacterWidth(word[i]);
      }

      int nextWidth = curWidth + wordWidth;
      if (!firstWord) {
        nextWidth += game_.getFont().getCharacterWidth(' ');
      }

      if (nextWidth > MESSAGE_TEXT_WIDTH) {
        lines_.push_back(curLine);
        curLine = word;
        curWidth = wordWidth + game_.getFont().getCharacterWidth(' ');

        if (shouldAddBlank) {
          shouldAddBlank = false;
          lines_.push_back("\n");
        } else {
          shouldAddBlank = true;
        }
      } else {
        curWidth = nextWidth;
        if (!firstWord) {
          curLine.append(" ");
        }
        curLine.append(word);
      }

      firstWord = false;
    }

    lines_.push_back(curLine);
    lines_.push_back("\n");
  }

  if (linesToShow_.empty()) {
    linesToShow_.push_back(MessageLine { .text = lines_.front() });
    lines_.pop_front();

    if (lines_.front() != "\n") {
      linesToShow_.push_back(MessageLine { .text = lines_.front() });
      lines_.pop_front();
    }
  }
}

void MessageSystem::advanceText() {
  if (barsState_ != BarsState::Open) {
    return;
  }

  for (const MessageLine& line : linesToShow_) {
    if (line.charsRevealed != line.text.size()) {
      return;
    }
  }

  if (lines_.empty()) {
    linesToShow_.clear();
    return;
  }

  if (lines_.front() != "\n") {
    return;
  }

  lines_.pop_front();

  if (lines_.empty()) {
    linesToShow_.clear();
  }
}

double MessageSystem::getCutsceneBarsProgress() const {
  switch (barsState_) {
    case BarsState::Closed: return 0.0;
    case BarsState::Opening: return accum_ / length_;
    case BarsState::Open: return 1.0;
    case BarsState::Closing: return 1.0 - (accum_ / length_);
  }
}