summary refs log tree commit diff stats
path: root/src/input_system.cpp
blob: c399e916dcd8476f42e0080e69694b838f7e81cb (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
188
189
190
191
192
#include "input_system.h"
#include "game.h"
#include "character_system.h"
#include "message_system.h"
#include "script_system.h"
#include "transform_system.h"
#include "animation_system.h"

struct Input {
  bool left = false;
  bool right = false;
  bool up = false;
  bool down = false;
};

void InputSystem::tick(double dt) {
  SDL_Event e;
  while (SDL_PollEvent(&e)) {
    if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_ESCAPE) {
      if (debugConsole_) {
        debugConsole_ = false;
        game_.unpauseGameplay();
        SDL_StopTextInput();
        debugText_.clear();
      } else {
        game_.quit();
      }
    } else if (e.type == SDL_QUIT) {
      game_.quit();

      return;
    } else if (e.type == SDL_TEXTINPUT) {
      debugText_.append(e.text.text);
    } else if (e.type == SDL_KEYDOWN) {
      if (e.key.keysym.sym == SDLK_BACKQUOTE) {
#ifdef TANETANE_DEBUG
        if (!debugConsole_) {
          debugConsole_ = true;
          game_.pauseGameplay();
          SDL_StartTextInput();
          debugText_.clear();
        }
#endif
      } else if (debugConsole_ && e.key.keysym.sym == SDLK_RETURN) {
        game_.getSystem<ScriptSystem>().runDebugScript(debugText_);
        debugText_.clear();
      } else if (debugConsole_ && e.key.keysym.sym == SDLK_BACKSPACE) {
        // Make sure to keep the backtick/heart.
        if (!debugText_.empty()) {
          debugText_.pop_back();
        }
      } else if (debugConsole_ && e.key.keysym.sym == SDLK_v && SDL_GetModState() & (KMOD_CTRL | KMOD_GUI)) {
        // CTRL-V or CMD-V bc I have a Mac thanks
        char* clipboardText = SDL_GetClipboardText();
        debugText_.append(clipboardText);
        SDL_free(clipboardText);
      } else if (!debugConsole_ && e.key.keysym.sym == SDLK_m) {
        if (game_.getMixer().isMusicMuted()) {
          game_.getMixer().unmuteMusic();
        } else {
          game_.getMixer().muteMusic();
        }
      } else if (e.key.keysym.sym == SDLK_LSHIFT || e.key.keysym.sym == SDLK_RSHIFT) {
        if (game_.isGameplayPaused()) continue;

        for (int spriteId : game_.getSprites()) {
          Sprite& sprite = game_.getSprite(spriteId);
          if (sprite.controllable) {
            game_.getSystem<CharacterSystem>().beginCrouch(spriteId);
          }
        }
      } else if (e.key.keysym.sym == SDLK_SPACE) {
        if (game_.isGameplayPaused()) continue;

        // If there is text on screen, try to advance it.
        if (game_.getSystem<MessageSystem>().getCutsceneBarsProgress() != 0.0) {
          game_.getSystem<MessageSystem>().advanceText();
        } else {
          // Otherwise, check if there is a sprite in front of the player.
          bool inFrontOfSomething = false;
          bool activated = false;

          for (int spriteId : game_.getSprites()) {
            Sprite& sprite = game_.getSprite(spriteId);
            if (sprite.controllable) {
              // Interacting with objects always uses Lucas's movement speed.
              vec2i checkLoc = sprite.loc + (unitVecInDirection(sprite.dir) * LUCAS_MOVEMENT_SPEED);
              CollisionResult collision = game_.getSystem<TransformSystem>().checkCollision(spriteId, sprite.loc, checkLoc, sprite.dir);

              if (collision.blocked) {
                inFrontOfSomething = true;
              }

              // If there is a sprite to be interacted with, rotate that sprite so it is facing the player.
              // Then, run its interaction script if present.
              for (int colliderSpriteId : collision.colliders) {
                game_.getSystem<AnimationSystem>().setSpriteDirection(colliderSpriteId, oppositeDirection(sprite.dir));

                Sprite& collider = game_.getSprite(colliderSpriteId);
                if (!collider.interactionScript.empty()) {
                  game_.getSystem<ScriptSystem>().runScript(game_.getMap().getName(), collider.interactionScript);
                  activated = true;
                }
              }
            }
          }

          if (inFrontOfSomething && !activated) {
            game_.getSystem<ScriptSystem>().runScript("global", "no_problem_here");
          }
        }
      } else if (e.key.keysym.sym == SDLK_LEFT) {
        if (game_.isGameplayPaused()) continue;

        if (game_.getSystem<MessageSystem>().isChoiceActive()) {
          game_.getSystem<MessageSystem>().selectFirstChoice();
        }
      } else if (e.key.keysym.sym == SDLK_RIGHT) {
        if (game_.isGameplayPaused()) continue;

        if (game_.getSystem<MessageSystem>().isChoiceActive()) {
          game_.getSystem<MessageSystem>().selectSecondChoice();
        }
      }
    } else if (e.type == SDL_KEYUP && (e.key.keysym.sym == SDLK_LSHIFT || e.key.keysym.sym == SDLK_RSHIFT)) {
      if (game_.isGameplayPaused()) continue;

      for (int spriteId : game_.getSprites()) {
        Sprite& sprite = game_.getSprite(spriteId);
        if (sprite.controllable) {
          game_.getSystem<CharacterSystem>().endCrouch(spriteId);
        }
      }
    }
  }

  if (game_.isGameplayPaused()) return;

  Input keystate;
  const Uint8* state = SDL_GetKeyboardState(NULL);
  keystate.left = state[SDL_SCANCODE_LEFT];
  keystate.right = state[SDL_SCANCODE_RIGHT];
  keystate.up = state[SDL_SCANCODE_UP];
  keystate.down = state[SDL_SCANCODE_DOWN];

  for (int spriteId : game_.getSprites()) {
    Sprite& sprite = game_.getSprite(spriteId);

    if (sprite.controllable) {
      bool directed = false;
      Direction dir = Direction::left;

      if (keystate.up)
      {
        directed = true;
        dir = Direction::up;
      } else if (keystate.down)
      {
        directed = true;
        dir = Direction::down;
      }

      if (keystate.left)
      {
        directed = true;
        if (dir == Direction::up) {
          dir = Direction::up_left;
        } else if (dir == Direction::down) {
          dir = Direction::down_left;
        } else {
          dir = Direction::left;
        }
      } else if (keystate.right)
      {
        directed = true;
        if (dir == Direction::up) {
          dir = Direction::up_right;
        } else if (dir == Direction::down) {
          dir = Direction::down_right;
        } else {
          dir = Direction::right;
        }
      }

      if (directed) {
        game_.getSystem<CharacterSystem>().moveInDirection(spriteId, dir);
      } else {
        game_.getSystem<CharacterSystem>().stopDirecting(spriteId);
      }
    }
  }
}