diff options
| author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-03 12:33:03 -0500 |
|---|---|---|
| committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-03 12:33:03 -0500 |
| commit | 683e22c419757744ce853c35d732f607ddb9af16 (patch) | |
| tree | bc31c1c08f19d270f70b27737d4f6944d30759b7 /src/input_system.cpp | |
| parent | 8d7ef2b2ae3ddff204f5934fe67c535d7f1345e9 (diff) | |
| download | tanetane-683e22c419757744ce853c35d732f607ddb9af16.tar.gz tanetane-683e22c419757744ce853c35d732f607ddb9af16.tar.bz2 tanetane-683e22c419757744ce853c35d732f607ddb9af16.zip | |
Added input system
Diffstat (limited to 'src/input_system.cpp')
| -rw-r--r-- | src/input_system.cpp | 89 |
1 files changed, 89 insertions, 0 deletions
| diff --git a/src/input_system.cpp b/src/input_system.cpp new file mode 100644 index 0000000..54a291c --- /dev/null +++ b/src/input_system.cpp | |||
| @@ -0,0 +1,89 @@ | |||
| 1 | #include "input_system.h" | ||
| 2 | #include "game.h" | ||
| 3 | #include "character_system.h" | ||
| 4 | |||
| 5 | struct Input { | ||
| 6 | bool left = false; | ||
| 7 | bool right = false; | ||
| 8 | bool up = false; | ||
| 9 | bool down = false; | ||
| 10 | }; | ||
| 11 | |||
| 12 | void InputSystem::tick(double dt) { | ||
| 13 | SDL_Event e; | ||
| 14 | while (SDL_PollEvent(&e)) { | ||
| 15 | if (e.type == SDL_QUIT || (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_ESCAPE)) { | ||
| 16 | game_.quit(); | ||
| 17 | |||
| 18 | return; | ||
| 19 | } else if (e.type == SDL_KEYDOWN && (e.key.keysym.sym == SDLK_LSHIFT || e.key.keysym.sym == SDLK_RSHIFT)) { | ||
| 20 | for (int spriteId : game_.getSprites()) { | ||
| 21 | Sprite& sprite = game_.getSprite(spriteId); | ||
| 22 | if (sprite.controllable) { | ||
| 23 | game_.getSystem<CharacterSystem>().beginCrouch(spriteId); | ||
| 24 | } | ||
| 25 | } | ||
| 26 | } else if (e.type == SDL_KEYUP && (e.key.keysym.sym == SDLK_LSHIFT || e.key.keysym.sym == SDLK_RSHIFT)) { | ||
| 27 | for (int spriteId : game_.getSprites()) { | ||
| 28 | Sprite& sprite = game_.getSprite(spriteId); | ||
| 29 | if (sprite.controllable) { | ||
| 30 | game_.getSystem<CharacterSystem>().endCrouch(spriteId); | ||
| 31 | } | ||
| 32 | } | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | Input keystate; | ||
| 37 | const Uint8* state = SDL_GetKeyboardState(NULL); | ||
| 38 | keystate.left = state[SDL_SCANCODE_LEFT]; | ||
| 39 | keystate.right = state[SDL_SCANCODE_RIGHT]; | ||
| 40 | keystate.up = state[SDL_SCANCODE_UP]; | ||
| 41 | keystate.down = state[SDL_SCANCODE_DOWN]; | ||
| 42 | |||
| 43 | for (int spriteId : game_.getSprites()) { | ||
| 44 | Sprite& sprite = game_.getSprite(spriteId); | ||
| 45 | |||
| 46 | if (sprite.controllable) { | ||
| 47 | bool directed = false; | ||
| 48 | Direction dir = Direction::left; | ||
| 49 | |||
| 50 | if (keystate.up) | ||
| 51 | { | ||
| 52 | directed = true; | ||
| 53 | dir = Direction::up; | ||
| 54 | } else if (keystate.down) | ||
| 55 | { | ||
| 56 | directed = true; | ||
| 57 | dir = Direction::down; | ||
| 58 | } | ||
| 59 | |||
| 60 | if (keystate.left) | ||
| 61 | { | ||
| 62 | directed = true; | ||
| 63 | if (dir == Direction::up) { | ||
| 64 | dir = Direction::up_left; | ||
| 65 | } else if (dir == Direction::down) { | ||
| 66 | dir = Direction::down_left; | ||
| 67 | } else { | ||
| 68 | dir = Direction::left; | ||
| 69 | } | ||
| 70 | } else if (keystate.right) | ||
| 71 | { | ||
| 72 | directed = true; | ||
| 73 | if (dir == Direction::up) { | ||
| 74 | dir = Direction::up_right; | ||
| 75 | } else if (dir == Direction::down) { | ||
| 76 | dir = Direction::down_right; | ||
| 77 | } else { | ||
| 78 | dir = Direction::right; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | if (directed) { | ||
| 83 | game_.getSystem<CharacterSystem>().moveInDirection(spriteId, dir); | ||
| 84 | } else { | ||
| 85 | game_.getSystem<CharacterSystem>().stopDirecting(spriteId); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | } | ||
| 89 | } | ||
