summary refs log tree commit diff stats
path: root/src/sign.cpp
blob: 7c4f43f29b7470a3319e3227ada61f6108194919 (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
#include "sign.h"
#include <list>
#include "util.h"
#include "consts.h"
#include "game.h"

void Sign::displayMessage(std::string text) {
  signDisplayState = SignInstructionState::FadingIn;
  signDisplayFade.start(1000);
  lines.clear();

  auto lineChunks = splitStr<std::list<std::string>>(text, "\\n");
  for (std::string lineChunk : lineChunks) {
    auto words = splitStr<std::list<std::string>>(lineChunk, " ");
    std::string prev = words.front();
    words.pop_front();
    std::string cur = prev;
    bool pauseLine = false;

    while (!words.empty()) {
      cur = prev + " " + words.front();

      int width = 0;
      TTF_SizeText(font_, cur.c_str(), &width, nullptr);
      if (width > MESSAGE_TEXT_WIDTH) {
        lines.push_back({.text = prev, .pause = pauseLine});
        pauseLine = !pauseLine;
        cur = words.front();
      } else if (words.size() == 1) {
        lines.push_back({.text = cur, .pause = true});
      }

      prev = cur;
      words.pop_front();
    }
  }

  lines.back().pause = true;
  linesToShow.push_back(lines.front());
  lines.pop_front();

  if (!linesToShow.back().pause) {
    linesToShow.push_back(lines.front());
    lines.pop_front();
  }
}

void Sign::update(size_t dt, Game& game) {
  SDL_Event e;

  while (SDL_PollEvent(&e)) {
    if (e.type == SDL_QUIT) {
      game.quit = true;
    }
  }

  switch (signDisplayState) {
    case SignInstructionState::Hidden: {
      // Shouldn't happen.
      break;
    }
    case SignInstructionState::FadingIn: {
      signDisplayFade.tick(dt);
      if (signDisplayFade.isComplete()) {
        signDisplayState = SignInstructionState::Visible;
      }

      break;
    }
    case SignInstructionState::FadingOut: {
      signDisplayFade.tick(dt);
      if (signDisplayFade.isComplete()) {
        signDisplayState = SignInstructionState::Hidden;
      }

      break;
    }
    case SignInstructionState::Visible: {
      const Uint8* state = SDL_GetKeyboardState(NULL);
      if (state[SDL_SCANCODE_SPACE]) {
        bool fullyRevealed = true;
        for (const SignLine& line : linesToShow) {
          if (line.charsRevealed != line.text.size()) {
            fullyRevealed = false;
            break;
          }
        }

        if (fullyRevealed) {
          if (linesToShow.back().pause) {
            linesToShow.back().pause = false;
            // Play a sound
          }
          if (lines.empty()) {
            linesToShow.clear();
            signDisplayState = SignInstructionState::FadingOut;
            signDisplayFade.start(1000);
            break;
          }
        }
      }

      textAdvTimer_.accumulate(dt);
      while (textAdvTimer_.step()) {
        bool advancedChars = false;
        for (SignLine& line : linesToShow) {
          if (line.charsRevealed < line.text.size()) {
            if (line.charsRevealed % CHARS_PER_BEEP == 0) {
              // Play a sound
              game.muxer.playSound("textbeep");
            }
            line.charsRevealed++;
            advancedChars = true;
            break;
          }
        }
        if (!advancedChars) {
          if (!lines.empty() && !linesToShow.back().pause) {
            if (linesToShow.size() == 2) {
              linesToShow.pop_front();
            }
            linesToShow.push_back(lines.front());
            lines.pop_front();
          }
        }
      }

      break;
    }
  }
}