summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2021-02-03 12:33:03 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2021-02-03 12:33:03 -0500
commit683e22c419757744ce853c35d732f607ddb9af16 (patch)
treebc31c1c08f19d270f70b27737d4f6944d30759b7
parent8d7ef2b2ae3ddff204f5934fe67c535d7f1345e9 (diff)
downloadtanetane-683e22c419757744ce853c35d732f607ddb9af16.tar.gz
tanetane-683e22c419757744ce853c35d732f607ddb9af16.tar.bz2
tanetane-683e22c419757744ce853c35d732f607ddb9af16.zip
Added input system
-rw-r--r--CMakeLists.txt1
-rw-r--r--src/character_system.cpp271
-rw-r--r--src/character_system.h14
-rw-r--r--src/game.h17
-rw-r--r--src/input_system.cpp89
-rw-r--r--src/input_system.h22
-rw-r--r--src/main.cpp43
-rw-r--r--src/sprite.h9
-rw-r--r--src/system.h5
9 files changed, 295 insertions, 176 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 65abfe1..a87b017 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt
@@ -41,6 +41,7 @@ add_executable(tanetane
41 src/camera_system.cpp 41 src/camera_system.cpp
42 src/animation_system.cpp 42 src/animation_system.cpp
43 src/character_system.cpp 43 src/character_system.cpp
44 src/input_system.cpp
44) 45)
45 46
46set_property(TARGET tanetane PROPERTY CXX_STANDARD 17) 47set_property(TARGET tanetane PROPERTY CXX_STANDARD 17)
diff --git a/src/character_system.cpp b/src/character_system.cpp index 64e2f3b..ac0f01a 100644 --- a/src/character_system.cpp +++ b/src/character_system.cpp
@@ -6,6 +6,11 @@
6#include "transform_system.h" 6#include "transform_system.h"
7#include "animation_system.h" 7#include "animation_system.h"
8 8
9void CharacterSystem::initSprite(int spriteId) {
10 Sprite& sprite = game_.getSprite(spriteId);
11 sprite.orientable = true;
12}
13
9void CharacterSystem::addSpriteToParty(int leaderId, int followerId) { 14void CharacterSystem::addSpriteToParty(int leaderId, int followerId) {
10 Sprite& leader = game_.getSprite(leaderId); 15 Sprite& leader = game_.getSprite(leaderId);
11 Sprite& follower = game_.getSprite(followerId); 16 Sprite& follower = game_.getSprite(followerId);
@@ -18,146 +23,124 @@ void CharacterSystem::addSpriteToParty(int leaderId, int followerId) {
18 game_.getSystem<AnimationSystem>().setSpriteAnimation(followerId, "still"); 23 game_.getSystem<AnimationSystem>().setSpriteAnimation(followerId, "still");
19} 24}
20 25
21void CharacterSystem::moveSprite(int spriteId, Mixer& mixer, const Input& keystate) { 26void CharacterSystem::moveInDirection(int spriteId, Direction dir) {
22 Sprite& sprite = game_.getSprite(spriteId); 27 Sprite& sprite = game_.getSprite(spriteId);
23 Direction dir = sprite.dir;
24
25 if (!keystate.up && !keystate.down && !keystate.left && !keystate.right) {
26 if (sprite.characterState != CharacterState::Running) {
27 if (sprite.characterState == CharacterState::Normal) {
28 game_.getSystem<AnimationSystem>().setSpriteAnimation(spriteId, "still");
29 for (int followerId : sprite.followers) {
30 game_.getSystem<AnimationSystem>().setSpriteAnimation(followerId, "still");
31 }
32 }
33
34 return;
35 }
36 } else {
37 if (keystate.up)
38 {
39 dir = Direction::up;
40 } else if (keystate.down)
41 {
42 dir = Direction::down;
43 }
44 28
45 if (keystate.left)
46 {
47 if (dir == Direction::up) {
48 dir = Direction::up_left;
49 } else if (dir == Direction::down) {
50 dir = Direction::down_left;
51 } else {
52 dir = Direction::left;
53 }
54 } else if (keystate.right)
55 {
56 if (dir == Direction::up) {
57 dir = Direction::up_right;
58 } else if (dir == Direction::down) {
59 dir = Direction::down_right;
60 } else {
61 dir = Direction::right;
62 }
63 }
64 }
65
66 vec2i pLoc = sprite.loc;
67 game_.getSystem<AnimationSystem>().setSpriteDirection(spriteId, dir); 29 game_.getSystem<AnimationSystem>().setSpriteDirection(spriteId, dir);
68 30
69 if (sprite.characterState == CharacterState::Crouching) { 31 if (sprite.characterState == CharacterState::Still) {
32 setPartyState(spriteId, CharacterState::Walking);
33 } else if (sprite.characterState == CharacterState::Crouching) {
70 for (int followerId : sprite.followers) { 34 for (int followerId : sprite.followers) {
71 game_.getSystem<AnimationSystem>().setSpriteDirection(followerId, dir); 35 game_.getSystem<AnimationSystem>().setSpriteDirection(followerId, dir);
72 } 36 }
73
74 return;
75 } 37 }
38}
76 39
77 int speed = MOVEMENT_SPEED; 40void CharacterSystem::stopDirecting(int spriteId) {
78 if (sprite.characterState == CharacterState::Running) { 41 Sprite& sprite = game_.getSprite(spriteId);
79 speed *= 2;
80 } else {
81 game_.getSystem<AnimationSystem>().setSpriteAnimation(spriteId, "walk");
82 for (int followerId : sprite.followers) {
83 game_.getSystem<AnimationSystem>().setSpriteAnimation(followerId, "walk");
84 }
85 }
86 42
87 pLoc += (unitVecInDirection(dir) * speed); 43 if (sprite.characterState == CharacterState::Walking) {
88 44 setPartyState(spriteId, CharacterState::Still);
89 // Check collision.
90 const Map& map = game_.getMap();
91 bool blocked = false;
92
93 const vec2i UL_COL_BOX = { 8, 8 };
94 const vec2i DR_COL_BOX = { 4, 0 };
95 vec2i oldColPosUL = (sprite.loc - UL_COL_BOX) / map.getTileSize();
96 vec2i newColPosUL = (pLoc - UL_COL_BOX) / map.getTileSize();
97 vec2i oldColPosDR = (sprite.loc + DR_COL_BOX) / map.getTileSize();
98 vec2i newColPosDR = (pLoc + DR_COL_BOX) / map.getTileSize();
99
100 if (dirHasDir(dir, Direction::right) &&
101 newColPosDR.x() > oldColPosDR.x()) {
102 for (int y = newColPosUL.y(); y <= newColPosDR.y(); y++) {
103 if (map.isBlocked(newColPosDR.x(), y)) {
104 blocked = true;
105 pLoc.x() = sprite.loc.x();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1;
106 break;
107 }
108 }
109 } 45 }
46}
110 47
111 if (dirHasDir(dir, Direction::left) && 48void CharacterSystem::tick(double dt) {
112 newColPosUL.x() < oldColPosUL.x()) { 49 inputTimer_.accumulate(dt);
113 for (int y = newColPosUL.y(); y <= newColPosDR.y(); y++) { 50 while (inputTimer_.step()) {
114 if (map.isBlocked(newColPosUL.x(), y)) { 51 for (int spriteId : game_.getSprites()) {
115 blocked = true; 52 Sprite& sprite = game_.getSprite(spriteId);
116 pLoc.x() = sprite.loc.x();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1;
117 break;
118 }
119 }
120 }
121 53
122 if (dirHasDir(dir, Direction::down) && 54 if (sprite.orientable) {
123 newColPosDR.y() > oldColPosDR.y()) { 55 vec2i pLoc = sprite.loc;
124 for (int x = newColPosUL.x(); x <= newColPosDR.x(); x++) {
125 if (map.isBlocked(x, newColPosDR.y())) {
126 blocked = true;
127 pLoc.y() = sprite.loc.y();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1;
128 break;
129 }
130 }
131 }
132 56
133 if (dirHasDir(dir, Direction::up) && 57 if (sprite.characterState == CharacterState::Still ||
134 newColPosUL.y() < oldColPosUL.y()) { 58 sprite.characterState == CharacterState::Crouching) {
135 for (int x = newColPosUL.x(); x <= newColPosDR.x(); x++) { 59 continue;
136 if (map.isBlocked(x, newColPosUL.y())) { 60 }
137 blocked = true;
138 pLoc.y() = sprite.loc.y();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1;
139 break;
140 }
141 }
142 }
143 61
144 if (blocked && sprite.characterState == CharacterState::Running) { 62 int speed = MOVEMENT_SPEED;
145 stopRunning(spriteId); 63 if (sprite.characterState == CharacterState::Running) {
146 mixer.playSound("../res/bump.wav"); 64 speed *= 2;
147 } 65 }
148 66
149 // Move everything 67 pLoc += (unitVecInDirection(sprite.dir) * speed);
150 if (pLoc != sprite.loc) { 68
151 game_.getSystem<TransformSystem>().moveSprite(spriteId, pLoc); 69 // Check collision.
70 const Map& map = game_.getMap();
71 bool blocked = false;
72
73 const vec2i UL_COL_BOX = { 8, 8 };
74 const vec2i DR_COL_BOX = { 4, 0 };
75 vec2i oldColPosUL = (sprite.loc - UL_COL_BOX) / map.getTileSize();
76 vec2i newColPosUL = (pLoc - UL_COL_BOX) / map.getTileSize();
77 vec2i oldColPosDR = (sprite.loc + DR_COL_BOX) / map.getTileSize();
78 vec2i newColPosDR = (pLoc + DR_COL_BOX) / map.getTileSize();
79
80 if (dirHasDir(sprite.dir, Direction::right) &&
81 newColPosDR.x() > oldColPosDR.x()) {
82 for (int y = newColPosUL.y(); y <= newColPosDR.y(); y++) {
83 if (map.isBlocked(newColPosDR.x(), y)) {
84 blocked = true;
85 pLoc.x() = sprite.loc.x();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1;
86 break;
87 }
88 }
89 }
152 90
153 for (int followerId : sprite.followers) { 91 if (dirHasDir(sprite.dir, Direction::left) &&
154 Sprite& pNext = game_.getSprite(followerId); 92 newColPosUL.x() < oldColPosUL.x()) {
155 const Movement& posdir = pNext.trail.front(); 93 for (int y = newColPosUL.y(); y <= newColPosDR.y(); y++) {
156 game_.getSystem<TransformSystem>().moveSprite(followerId, posdir.pos); 94 if (map.isBlocked(newColPosUL.x(), y)) {
157 game_.getSystem<AnimationSystem>().setSpriteDirection(followerId, posdir.dir); 95 blocked = true;
96 pLoc.x() = sprite.loc.x();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1;
97 break;
98 }
99 }
100 }
158 101
159 pNext.trail.pop_front(); 102 if (dirHasDir(sprite.dir, Direction::down) &&
160 pNext.trail.push_back({.pos = pLoc, .dir = dir}); 103 newColPosDR.y() > oldColPosDR.y()) {
104 for (int x = newColPosUL.x(); x <= newColPosDR.x(); x++) {
105 if (map.isBlocked(x, newColPosDR.y())) {
106 blocked = true;
107 pLoc.y() = sprite.loc.y();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1;
108 break;
109 }
110 }
111 }
112
113 if (dirHasDir(sprite.dir, Direction::up) &&
114 newColPosUL.y() < oldColPosUL.y()) {
115 for (int x = newColPosUL.x(); x <= newColPosDR.x(); x++) {
116 if (map.isBlocked(x, newColPosUL.y())) {
117 blocked = true;
118 pLoc.y() = sprite.loc.y();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1;
119 break;
120 }
121 }
122 }
123
124 if (blocked && sprite.characterState == CharacterState::Running) {
125 stopRunning(spriteId);
126 game_.getMixer().playSound("../res/bump.wav");
127 }
128
129 // Move everything
130 if (pLoc != sprite.loc) {
131 game_.getSystem<TransformSystem>().moveSprite(spriteId, pLoc);
132
133 for (int followerId : sprite.followers) {
134 Sprite& pNext = game_.getSprite(followerId);
135 const Movement& posdir = pNext.trail.front();
136 game_.getSystem<TransformSystem>().moveSprite(followerId, posdir.pos);
137 game_.getSystem<AnimationSystem>().setSpriteDirection(followerId, posdir.dir);
138
139 pNext.trail.pop_front();
140 pNext.trail.push_back({.pos = pLoc, .dir = sprite.dir});
141 }
142 }
143 }
161 } 144 }
162 } 145 }
163} 146}
@@ -168,12 +151,7 @@ void CharacterSystem::beginCrouch(int spriteId) {
168 if (sprite.characterState == CharacterState::Running) { 151 if (sprite.characterState == CharacterState::Running) {
169 stopRunning(spriteId); 152 stopRunning(spriteId);
170 } else { 153 } else {
171 sprite.characterState = CharacterState::Crouching; 154 setPartyState(spriteId, CharacterState::Crouching);
172
173 game_.getSystem<AnimationSystem>().setSpriteAnimation(spriteId, "crouch");
174 for (int followerId : sprite.followers) {
175 game_.getSystem<AnimationSystem>().setSpriteAnimation(followerId, "crouch");
176 }
177 } 155 }
178} 156}
179 157
@@ -181,12 +159,9 @@ void CharacterSystem::endCrouch(int spriteId) {
181 Sprite& sprite = game_.getSprite(spriteId); 159 Sprite& sprite = game_.getSprite(spriteId);
182 160
183 if (sprite.characterState == CharacterState::Crouching) { 161 if (sprite.characterState == CharacterState::Crouching) {
184 sprite.characterState = CharacterState::Running; 162 setPartyState(spriteId, CharacterState::Running);
185 163
186 game_.getSystem<AnimationSystem>().setSpriteAnimation(spriteId, "run");
187 for (int followerId : sprite.followers) { 164 for (int followerId : sprite.followers) {
188 game_.getSystem<AnimationSystem>().setSpriteAnimation(followerId, "run");
189
190 // Halve the movement buffer for the followers. 165 // Halve the movement buffer for the followers.
191 Sprite& follower = game_.getSprite(followerId); 166 Sprite& follower = game_.getSprite(followerId);
192 std::deque<Movement> newMove; 167 std::deque<Movement> newMove;
@@ -204,7 +179,7 @@ void CharacterSystem::endCrouch(int spriteId) {
204 179
205void CharacterSystem::stopRunning(int spriteId) { 180void CharacterSystem::stopRunning(int spriteId) {
206 Sprite& sprite = game_.getSprite(spriteId); 181 Sprite& sprite = game_.getSprite(spriteId);
207 sprite.characterState = CharacterState::Normal; 182 setPartyState(spriteId, CharacterState::Still);
208 183
209 // Double the movement buffer for the followers. 184 // Double the movement buffer for the followers.
210 for (int followerId : sprite.followers) { 185 for (int followerId : sprite.followers) {
@@ -227,3 +202,33 @@ void CharacterSystem::stopRunning(int spriteId) {
227 follower.trail = std::move(newMove); 202 follower.trail = std::move(newMove);
228 } 203 }
229} 204}
205
206void CharacterSystem::setPartyState(int spriteId, CharacterState state) {
207 std::string animName;
208 switch (state) {
209 case CharacterState::Still: {
210 animName = "still";
211 break;
212 }
213 case CharacterState::Walking: {
214 animName = "walk";
215 break;
216 }
217 case CharacterState::Crouching: {
218 animName = "crouch";
219 break;
220 }
221 case CharacterState::Running: {
222 animName = "run";
223 break;
224 }
225 }
226
227 Sprite& sprite = game_.getSprite(spriteId);
228 sprite.characterState = state;
229
230 game_.getSystem<AnimationSystem>().setSpriteAnimation(spriteId, animName);
231 for (int followerId : sprite.followers) {
232 game_.getSystem<AnimationSystem>().setSpriteAnimation(followerId, animName);
233 }
234}
diff --git a/src/character_system.h b/src/character_system.h index ff5db02..2eea233 100644 --- a/src/character_system.h +++ b/src/character_system.h
@@ -2,6 +2,9 @@
2#define PARTY_H_826F91BA 2#define PARTY_H_826F91BA
3 3
4#include "system.h" 4#include "system.h"
5#include "direction.h"
6#include "timer.h"
7#include "sprite.h"
5 8
6class Mixer; 9class Mixer;
7class Game; 10class Game;
@@ -14,9 +17,15 @@ public:
14 17
15 CharacterSystem(Game& game) : game_(game) {} 18 CharacterSystem(Game& game) : game_(game) {}
16 19
20 void initSprite(int spriteId);
21
22 void tick(double dt) override;
23
17 void addSpriteToParty(int leaderId, int followerId); 24 void addSpriteToParty(int leaderId, int followerId);
18 25
19 void moveSprite(int spriteId, Mixer& mixer, const Input& keystate); 26 void moveInDirection(int spriteId, Direction dir);
27
28 void stopDirecting(int spriteId);
20 29
21 void beginCrouch(int spriteId); 30 void beginCrouch(int spriteId);
22 31
@@ -26,7 +35,10 @@ private:
26 35
27 void stopRunning(int spriteId); 36 void stopRunning(int spriteId);
28 37
38 void setPartyState(int spriteId, CharacterState state);
39
29 Game& game_; 40 Game& game_;
41 Timer inputTimer_ {33};
30}; 42};
31 43
32#endif /* end of include guard: PARTY_H_826F91BA */ 44#endif /* end of include guard: PARTY_H_826F91BA */
diff --git a/src/game.h b/src/game.h index f8e4b0d..36398bc 100644 --- a/src/game.h +++ b/src/game.h
@@ -10,17 +10,17 @@
10#include "map.h" 10#include "map.h"
11#include "consts.h" 11#include "consts.h"
12#include "system.h" 12#include "system.h"
13 13#include "mixer.h"
14struct Input {
15 bool left = false;
16 bool right = false;
17 bool up = false;
18 bool down = false;
19};
20 14
21class Game { 15class Game {
22public: 16public:
23 17
18 Mixer& getMixer() { return mixer_; }
19
20 bool shouldQuit() const { return shouldQuit_; }
21
22 void quit() { shouldQuit_ = true; }
23
24 template <typename T> 24 template <typename T>
25 void emplaceSystem() { 25 void emplaceSystem() {
26 systems_.push_back(std::make_unique<T>(*this)); 26 systems_.push_back(std::make_unique<T>(*this));
@@ -70,6 +70,9 @@ public:
70 70
71private: 71private:
72 72
73 Mixer mixer_;
74 bool shouldQuit_ = false;
75
73 std::list<std::unique_ptr<System>> systems_; 76 std::list<std::unique_ptr<System>> systems_;
74 std::map<SystemKey, System*> systemByKey_; 77 std::map<SystemKey, System*> systemByKey_;
75 78
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
5struct Input {
6 bool left = false;
7 bool right = false;
8 bool up = false;
9 bool down = false;
10};
11
12void 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}
diff --git a/src/input_system.h b/src/input_system.h new file mode 100644 index 0000000..4e5bb22 --- /dev/null +++ b/src/input_system.h
@@ -0,0 +1,22 @@
1#ifndef INPUT_SYSTEM_H_47764575
2#define INPUT_SYSTEM_H_47764575
3
4#include "system.h"
5
6class Game;
7
8class InputSystem : public System {
9public:
10
11 static constexpr SystemKey Key = SystemKey::Input;
12
13 InputSystem(Game& game) : game_(game) {}
14
15 void tick(double dt) override;
16
17private:
18
19 Game& game_;
20};
21
22#endif /* end of include guard: INPUT_SYSTEM_H_47764575 */
diff --git a/src/main.cpp b/src/main.cpp index 3ac4d09..771aa1c 100644 --- a/src/main.cpp +++ b/src/main.cpp
@@ -9,15 +9,15 @@
9#include "camera_system.h" 9#include "camera_system.h"
10#include "animation_system.h" 10#include "animation_system.h"
11#include "character_system.h" 11#include "character_system.h"
12#include "input_system.h"
12 13
13void loop(Renderer& renderer, Mixer& mixer) { 14void loop(Renderer& renderer) {
14 Game game; 15 Game game;
15 game.emplaceSystem<TransformSystem>(); 16 game.emplaceSystem<TransformSystem>();
16 game.emplaceSystem<CameraSystem>(); 17 game.emplaceSystem<InputSystem>();
17 game.emplaceSystem<AnimationSystem>();
18 game.emplaceSystem<CharacterSystem>(); 18 game.emplaceSystem<CharacterSystem>();
19 19 game.emplaceSystem<AnimationSystem>();
20 Input keystate; 20 game.emplaceSystem<CameraSystem>();
21 21
22 auto map = std::make_unique<Map>("../res/map1.tmx", renderer); 22 auto map = std::make_unique<Map>("../res/map1.tmx", renderer);
23 game.setMap(std::move(map)); 23 game.setMap(std::move(map));
@@ -25,6 +25,8 @@ void loop(Renderer& renderer, Mixer& mixer) {
25 int lucasSprite = game.emplaceSprite(); 25 int lucasSprite = game.emplaceSprite();
26 game.getSystem<TransformSystem>().initSprite(lucasSprite, {32, 32}); 26 game.getSystem<TransformSystem>().initSprite(lucasSprite, {32, 32});
27 game.getSystem<AnimationSystem>().initSprite(lucasSprite, "../res/lucas_anim.txt", renderer); 27 game.getSystem<AnimationSystem>().initSprite(lucasSprite, "../res/lucas_anim.txt", renderer);
28 game.getSprite(lucasSprite).controllable = true;
29 game.getSystem<CharacterSystem>().initSprite(lucasSprite);
28 30
29 int kumaSprite = game.emplaceSprite(); 31 int kumaSprite = game.emplaceSprite();
30 game.getSystem<TransformSystem>().initSprite(kumaSprite, {32, 32}); 32 game.getSystem<TransformSystem>().initSprite(kumaSprite, {32, 32});
@@ -46,8 +48,6 @@ void loop(Renderer& renderer, Mixer& mixer) {
46 48
47 renderer.render(game); 49 renderer.render(game);
48 50
49 Timer inputTimer(33);
50
51 size_t lastTime = SDL_GetTicks(); 51 size_t lastTime = SDL_GetTicks();
52 52
53 for (;;) { 53 for (;;) {
@@ -55,33 +55,15 @@ void loop(Renderer& renderer, Mixer& mixer) {
55 size_t frameTime = currentTime - lastTime; 55 size_t frameTime = currentTime - lastTime;
56 lastTime = currentTime; 56 lastTime = currentTime;
57 57
58 SDL_Event e;
59 while (SDL_PollEvent(&e)) {
60 if (e.type == SDL_QUIT || (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_ESCAPE)) {
61 return;
62 } else if (e.type == SDL_KEYDOWN && (e.key.keysym.sym == SDLK_LSHIFT || e.key.keysym.sym == SDLK_RSHIFT)) {
63 game.getSystem<CharacterSystem>().beginCrouch(lucasSprite);
64 } else if (e.type == SDL_KEYUP && (e.key.keysym.sym == SDLK_LSHIFT || e.key.keysym.sym == SDLK_RSHIFT)) {
65 game.getSystem<CharacterSystem>().endCrouch(lucasSprite);
66 }
67 }
68
69 const Uint8* state = SDL_GetKeyboardState(NULL);
70 keystate.left = state[SDL_SCANCODE_LEFT];
71 keystate.right = state[SDL_SCANCODE_RIGHT];
72 keystate.up = state[SDL_SCANCODE_UP];
73 keystate.down = state[SDL_SCANCODE_DOWN];
74
75 inputTimer.accumulate(frameTime);
76 while (inputTimer.step()) {
77 game.getSystem<CharacterSystem>().moveSprite(lucasSprite, mixer, keystate);
78 }
79
80 for (System& system : game.systems()) { 58 for (System& system : game.systems()) {
81 system.tick(frameTime); 59 system.tick(frameTime);
82 } 60 }
83 61
84 renderer.render(game); 62 renderer.render(game);
63
64 if (game.shouldQuit()) {
65 return;
66 }
85 } 67 }
86} 68}
87 69
@@ -89,9 +71,8 @@ int main(int, char**) {
89 try 71 try
90 { 72 {
91 Renderer renderer; 73 Renderer renderer;
92 Mixer mixer;
93 74
94 loop(renderer, mixer); 75 loop(renderer);
95 } catch (const sdl_error& ex) 76 } catch (const sdl_error& ex)
96 { 77 {
97 std::cout << "SDL error (" << ex.what() << ")" << std::endl; 78 std::cout << "SDL error (" << ex.what() << ")" << std::endl;
diff --git a/src/sprite.h b/src/sprite.h index 65a7a66..2dc0ee1 100644 --- a/src/sprite.h +++ b/src/sprite.h
@@ -16,7 +16,8 @@ struct SpriteFrame {
16}; 16};
17 17
18enum class CharacterState { 18enum class CharacterState {
19 Normal, 19 Still,
20 Walking,
20 Crouching, 21 Crouching,
21 Running 22 Running
22}; 23};
@@ -44,9 +45,13 @@ public:
44 std::map<std::string, std::map<Direction, int>> nameDirToAnim; 45 std::map<std::string, std::map<Direction, int>> nameDirToAnim;
45 46
46 // Character 47 // Character
48 bool orientable = false;
47 std::vector<int> followers; 49 std::vector<int> followers;
48 std::deque<Movement> trail; 50 std::deque<Movement> trail;
49 CharacterState characterState = CharacterState::Normal; 51 CharacterState characterState = CharacterState::Still;
52
53 // Input
54 bool controllable = false;
50}; 55};
51 56
52#endif /* end of include guard: SPRITE_H_70503825 */ 57#endif /* end of include guard: SPRITE_H_70503825 */
diff --git a/src/system.h b/src/system.h index 6f09d61..fc89503 100644 --- a/src/system.h +++ b/src/system.h
@@ -3,9 +3,10 @@
3 3
4enum class SystemKey { 4enum class SystemKey {
5 Transform, 5 Transform,
6 Camera, 6 Input,
7 Character,
7 Animation, 8 Animation,
8 Character 9 Camera
9}; 10};
10 11
11class System { 12class System {