summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2021-02-03 01:35:58 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2021-02-03 01:35:58 -0500
commitbe09120d1d044b476ef8b516efbdb526f20d9e2d (patch)
treef935389835d5f94a9cd3bb2059cf55174c9aad69
parent24918837c3ff9026d228657d14852c9cf39a5644 (diff)
downloadtanetane-be09120d1d044b476ef8b516efbdb526f20d9e2d.tar.gz
tanetane-be09120d1d044b476ef8b516efbdb526f20d9e2d.tar.bz2
tanetane-be09120d1d044b476ef8b516efbdb526f20d9e2d.zip
Added animation system
-rw-r--r--CMakeLists.txt2
-rw-r--r--src/animation_system.cpp (renamed from src/sprite.cpp)66
-rw-r--r--src/animation_system.h35
-rw-r--r--src/camera_system.cpp2
-rw-r--r--src/game.cpp4
-rw-r--r--src/game.h18
-rw-r--r--src/main.cpp24
-rw-r--r--src/party.cpp37
-rw-r--r--src/renderer.cpp6
-rw-r--r--src/sprite.h46
-rw-r--r--src/system.h3
-rw-r--r--src/transform_system.cpp8
12 files changed, 137 insertions, 114 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 0fd88ff..f7b6790 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt
@@ -35,12 +35,12 @@ add_executable(tanetane
35 src/main.cpp 35 src/main.cpp
36 src/renderer.cpp 36 src/renderer.cpp
37 src/mixer.cpp 37 src/mixer.cpp
38 src/sprite.cpp
39 src/party.cpp 38 src/party.cpp
40 src/game.cpp 39 src/game.cpp
41 src/map.cpp 40 src/map.cpp
42 src/transform_system.cpp 41 src/transform_system.cpp
43 src/camera_system.cpp 42 src/camera_system.cpp
43 src/animation_system.cpp
44) 44)
45 45
46set_property(TARGET tanetane PROPERTY CXX_STANDARD 17) 46set_property(TARGET tanetane PROPERTY CXX_STANDARD 17)
diff --git a/src/sprite.cpp b/src/animation_system.cpp index b84f4ce..3320e1d 100644 --- a/src/sprite.cpp +++ b/src/animation_system.cpp
@@ -1,22 +1,26 @@
1#include "sprite.h" 1#include "animation_system.h"
2#include <SDL_image.h>
3#include <fstream> 2#include <fstream>
3#include <string>
4#include <list> 4#include <list>
5#include <regex> 5#include "game.h"
6#include "vector.h"
6#include "util.h" 7#include "util.h"
7 8
8Sprite::Sprite(std::string_view filename, Renderer& renderer) { 9void AnimationSystem::initSprite(int spriteId, std::string_view filename, Renderer& renderer) {
9 std::ifstream datafile(filename.data()); 10 std::ifstream datafile(filename.data());
10 if (!datafile.is_open()) { 11 if (!datafile.is_open()) {
11 throw std::invalid_argument(std::string("Could not find sprite datafile: ") + std::string(filename)); 12 throw std::invalid_argument(std::string("Could not find sprite datafile: ") + std::string(filename));
12 } 13 }
13 14
15 Sprite& sprite = game_.getSprite(spriteId);
16 sprite.isAnimated = true;
17
14 char ch; 18 char ch;
15 std::string line; 19 std::string line;
16 20
17 std::string imagename; 21 std::string imagename;
18 datafile >> imagename; 22 datafile >> imagename;
19 textureId_ = renderer.loadImageFromFile(imagename); 23 sprite.textureId = renderer.loadImageFromFile(imagename);
20 24
21 std::string framefilename; 25 std::string framefilename;
22 datafile >> framefilename; 26 datafile >> framefilename;
@@ -57,7 +61,7 @@ Sprite::Sprite(std::string_view filename, Renderer& renderer) {
57 f.srcRect.w = f.size.w(); 61 f.srcRect.w = f.size.w();
58 f.srcRect.h = f.size.h(); 62 f.srcRect.h = f.size.h();
59 63
60 frames_.push_back(std::move(f)); 64 sprite.frames.push_back(std::move(f));
61 } 65 }
62 66
63 std::string animLine; 67 std::string animLine;
@@ -73,38 +77,46 @@ Sprite::Sprite(std::string_view filename, Renderer& renderer) {
73 frames.push_back(std::stoi(f)); 77 frames.push_back(std::stoi(f));
74 } 78 }
75 79
76 int animId = animations_.size(); 80 int animId = sprite.animations.size();
77 animations_.push_back(std::move(frames)); 81 sprite.animations.push_back(std::move(frames));
78 82
79 Direction dir = directionFromString(std::string(m[2])); 83 Direction dir = directionFromString(std::string(m[2]));
80 stateDirToAnim_[m[1]][dir] = animId; 84 sprite.nameDirToAnim[m[1]][dir] = animId;
81 } 85 }
82 86
83 updateAnimation(); 87 updateAnimation(spriteId);
84} 88}
85 89
86void Sprite::setDirection(Direction dir) { 90void AnimationSystem::tick(double dt) {
87 if (curDir_ != dir) { 91 animTimer_.accumulate(dt);
88 curDir_ = dir; 92 while (animTimer_.step()) {
89 updateAnimation(); 93 for (Sprite& sprite : game_.getSprites() | game_.spriteView()) {
94 sprite.animationFrame++;
95 if (sprite.animationFrame >= sprite.animations[sprite.animationId].size()) {
96 sprite.animationFrame = 0;
97 }
98 }
90 } 99 }
91} 100}
92 101
93void Sprite::setState(std::string state) { 102void AnimationSystem::setSpriteDirection(int spriteId, Direction dir) {
94 if (state_ != state) { 103 Sprite& sprite = game_.getSprite(spriteId);
95 state_ = state; 104 if (sprite.dir != dir) {
96 updateAnimation(); 105 sprite.dir = dir;
106 updateAnimation(spriteId);
97 } 107 }
98} 108}
99 109
100void Sprite::updateAnimation() { 110void AnimationSystem::setSpriteAnimation(int spriteId, std::string_view name) {
101 curAnim_ = stateDirToAnim_[state_][curDir_]; 111 Sprite& sprite = game_.getSprite(spriteId);
102 curFrame_ = 0; 112 if (sprite.animationName != name) {
113 sprite.animationName = name;
114 updateAnimation(spriteId);
115 }
103} 116}
104 117
105void Sprite::tickAnim() { 118void AnimationSystem::updateAnimation(int spriteId) {
106 curFrame_++; 119 Sprite& sprite = game_.getSprite(spriteId);
107 if (curFrame_ >= animations_[curAnim_].size()) { 120 sprite.animationId = sprite.nameDirToAnim[sprite.animationName][sprite.dir];
108 curFrame_ = 0; 121 sprite.animationFrame = 0;
109 } 122}
110} \ No newline at end of file
diff --git a/src/animation_system.h b/src/animation_system.h new file mode 100644 index 0000000..2e13784 --- /dev/null +++ b/src/animation_system.h
@@ -0,0 +1,35 @@
1#ifndef ANIMATION_SYSTEM_H_CCCC7CB8
2#define ANIMATION_SYSTEM_H_CCCC7CB8
3
4#include <string_view>
5#include "direction.h"
6#include "system.h"
7#include "timer.h"
8
9class Game;
10class Renderer;
11
12class AnimationSystem : public System {
13public:
14
15 static constexpr SystemKey Key = SystemKey::Animation;
16
17 AnimationSystem(Game& game) : game_(game) {}
18
19 void tick(double dt) override;
20
21 void initSprite(int spriteId, std::string_view filename, Renderer& renderer);
22
23 void setSpriteDirection(int spriteId, Direction dir);
24
25 void setSpriteAnimation(int spriteId, std::string_view name);
26
27private:
28
29 void updateAnimation(int spriteId);
30
31 Game& game_;
32 Timer animTimer_ {1000/5};//30fps * 1000 t/s;;
33};
34
35#endif /* end of include guard: ANIMATION_SYSTEM_H_CCCC7CB8 */
diff --git a/src/camera_system.cpp b/src/camera_system.cpp index abeba86..4709d01 100644 --- a/src/camera_system.cpp +++ b/src/camera_system.cpp
@@ -9,7 +9,7 @@ void CameraSystem::tick(double dt) {
9 const Map& map = game_.getMap(); 9 const Map& map = game_.getMap();
10 vec2i mapBounds = map.getMapSize() * map.getTileSize(); 10 vec2i mapBounds = map.getMapSize() * map.getTileSize();
11 11
12 pos_ = follow.loc() - (fov_ / 2); 12 pos_ = follow.loc - (fov_ / 2);
13 13
14 if (pos_.x() < 0) { 14 if (pos_.x() < 0) {
15 pos_.x() = 0; 15 pos_.x() = 0;
diff --git a/src/game.cpp b/src/game.cpp index bb20d74..4ea7fd2 100644 --- a/src/game.cpp +++ b/src/game.cpp
@@ -1,8 +1,8 @@
1#include "game.h" 1#include "game.h"
2 2
3int Game::addSprite(Sprite sprite) { 3int Game::emplaceSprite() {
4 int id = sprites_.size(); 4 int id = sprites_.size();
5 sprites_.push_back(std::move(sprite)); 5 sprites_.emplace_back();
6 spriteIds_.push_back(id); 6 spriteIds_.push_back(id);
7 return id; 7 return id;
8} 8}
diff --git a/src/game.h b/src/game.h index f89c707..f8e4b0d 100644 --- a/src/game.h +++ b/src/game.h
@@ -38,7 +38,7 @@ public:
38 }); 38 });
39 } 39 }
40 40
41 int addSprite(Sprite sprite); 41 int emplaceSprite();
42 42
43 const Sprite& getSprite(int id) const { 43 const Sprite& getSprite(int id) const {
44 return sprites_.at(id); 44 return sprites_.at(id);
@@ -58,18 +58,10 @@ public:
58 }); 58 });
59 } 59 }
60 60
61 void setSpriteState(int id, std::string state) { 61 auto spriteView() {
62 sprites_[id].setState(std::move(state)); 62 return ranges::views::transform([&] (int id) -> Sprite& {
63 } 63 return sprites_.at(id);
64 64 });
65 void setSpriteDirection(int id, Direction dir) {
66 sprites_[id].setDirection(dir);
67 }
68
69 void tickSpriteAnim() {
70 for (Sprite& sprite : sprites_) {
71 sprite.tickAnim();
72 }
73 } 65 }
74 66
75 void setMap(std::unique_ptr<Map> map) { map_ = std::move(map); } 67 void setMap(std::unique_ptr<Map> map) { map_ = std::move(map); }
diff --git a/src/main.cpp b/src/main.cpp index a092114..ea27511 100644 --- a/src/main.cpp +++ b/src/main.cpp
@@ -8,26 +8,34 @@
8#include "mixer.h" 8#include "mixer.h"
9#include "transform_system.h" 9#include "transform_system.h"
10#include "camera_system.h" 10#include "camera_system.h"
11#include "animation_system.h"
11 12
12void loop(Renderer& renderer, Mixer& mixer) { 13void loop(Renderer& renderer, Mixer& mixer) {
13 Game game; 14 Game game;
14 game.emplaceSystem<TransformSystem>(); 15 game.emplaceSystem<TransformSystem>();
15 game.emplaceSystem<CameraSystem>(); 16 game.emplaceSystem<CameraSystem>();
17 game.emplaceSystem<AnimationSystem>();
16 18
17 Input keystate; 19 Input keystate;
18 20
19 auto map = std::make_unique<Map>("../res/map1.tmx", renderer); 21 auto map = std::make_unique<Map>("../res/map1.tmx", renderer);
20 game.setMap(std::move(map)); 22 game.setMap(std::move(map));
21 23
22 int lucasSprite = game.addSprite(Sprite("../res/lucas_anim.txt", renderer)); 24 int lucasSprite = game.emplaceSprite();
23 int kumaSprite = game.addSprite(Sprite("../res/kuma_anim.txt", renderer));
24 int dusterSprite = game.addSprite(Sprite("../res/duster_anim.txt", renderer));
25 int boneySprite = game.addSprite(Sprite("../res/boney_anim.txt", renderer));
26
27 game.getSystem<TransformSystem>().initSprite(lucasSprite, {32, 32}); 25 game.getSystem<TransformSystem>().initSprite(lucasSprite, {32, 32});
26 game.getSystem<AnimationSystem>().initSprite(lucasSprite, "../res/lucas_anim.txt", renderer);
27
28 int kumaSprite = game.emplaceSprite();
28 game.getSystem<TransformSystem>().initSprite(kumaSprite, {32, 32}); 29 game.getSystem<TransformSystem>().initSprite(kumaSprite, {32, 32});
30 game.getSystem<AnimationSystem>().initSprite(kumaSprite, "../res/kuma_anim.txt", renderer);
31
32 int dusterSprite = game.emplaceSprite();
29 game.getSystem<TransformSystem>().initSprite(dusterSprite, {32, 32}); 33 game.getSystem<TransformSystem>().initSprite(dusterSprite, {32, 32});
34 game.getSystem<AnimationSystem>().initSprite(dusterSprite, "../res/duster_anim.txt", renderer);
35
36 int boneySprite = game.emplaceSprite();
30 game.getSystem<TransformSystem>().initSprite(boneySprite, {32, 32}); 37 game.getSystem<TransformSystem>().initSprite(boneySprite, {32, 32});
38 game.getSystem<AnimationSystem>().initSprite(boneySprite, "../res/boney_anim.txt", renderer);
31 39
32 Party party; 40 Party party;
33 party.addMember(game, lucasSprite); 41 party.addMember(game, lucasSprite);
@@ -41,7 +49,6 @@ void loop(Renderer& renderer, Mixer& mixer) {
41 renderer.render(game); 49 renderer.render(game);
42 50
43 Timer inputTimer(33); 51 Timer inputTimer(33);
44 Timer animTimer(1000/5);//30fps * 1000 t/s;
45 52
46 size_t lastTime = SDL_GetTicks(); 53 size_t lastTime = SDL_GetTicks();
47 54
@@ -72,11 +79,6 @@ void loop(Renderer& renderer, Mixer& mixer) {
72 party.move(game, mixer, keystate); 79 party.move(game, mixer, keystate);
73 } 80 }
74 81
75 animTimer.accumulate(frameTime);
76 while (animTimer.step()) {
77 game.tickSpriteAnim();
78 }
79
80 for (System& system : game.systems()) { 82 for (System& system : game.systems()) {
81 system.tick(frameTime); 83 system.tick(frameTime);
82 } 84 }
diff --git a/src/party.cpp b/src/party.cpp index b717281..8b0c34e 100644 --- a/src/party.cpp +++ b/src/party.cpp
@@ -2,6 +2,7 @@
2#include "consts.h" 2#include "consts.h"
3#include "mixer.h" 3#include "mixer.h"
4#include "transform_system.h" 4#include "transform_system.h"
5#include "animation_system.h"
5 6
6void Party::addMember(Game& game, int spriteId) { 7void Party::addMember(Game& game, int spriteId) {
7 int index = members_.size(); 8 int index = members_.size();
@@ -12,12 +13,12 @@ void Party::addMember(Game& game, int spriteId) {
12 if (index > 0) { 13 if (index > 0) {
13 const Sprite& sprite = game.getSprite(spriteId); 14 const Sprite& sprite = game.getSprite(spriteId);
14 15
15 newMember.movement = std::deque<Movement>(PARTY_FRAME_DELAY * index, {.pos = sprite.loc(), .dir = sprite.getDirection()}); 16 newMember.movement = std::deque<Movement>(PARTY_FRAME_DELAY * index, {.pos = sprite.loc, .dir = sprite.dir});
16 } 17 }
17 18
18 members_.push_back(std::move(newMember)); 19 members_.push_back(std::move(newMember));
19 20
20 game.setSpriteState(spriteId, "still"); 21 game.getSystem<AnimationSystem>().setSpriteAnimation(spriteId, "still");
21} 22}
22 23
23void Party::move(Game& game, Mixer& mixer, const Input& keystate) { 24void Party::move(Game& game, Mixer& mixer, const Input& keystate) {
@@ -33,7 +34,7 @@ void Party::move(Game& game, Mixer& mixer, const Input& keystate) {
33 } else { 34 } else {
34 if (state_ == State::Normal) { 35 if (state_ == State::Normal) {
35 for (int i = 0; i < members_.size(); i++) { 36 for (int i = 0; i < members_.size(); i++) {
36 game.setSpriteState(members_[i].spriteId, "still"); 37 game.getSystem<AnimationSystem>().setSpriteAnimation(members_[i].spriteId, "still");
37 } 38 }
38 } 39 }
39 40
@@ -72,16 +73,16 @@ void Party::move(Game& game, Mixer& mixer, const Input& keystate) {
72 lastDir_ = dir; 73 lastDir_ = dir;
73 74
74 const Sprite& p1 = game.getSprite(members_[0].spriteId); 75 const Sprite& p1 = game.getSprite(members_[0].spriteId);
75 vec2i pLoc = p1.loc(); 76 vec2i pLoc = p1.loc;
76 77
77 if (state_ == State::Crouching) { 78 if (state_ == State::Crouching) {
78 for (int i = 0; i < members_.size(); i++) { 79 for (int i = 0; i < members_.size(); i++) {
79 game.setSpriteDirection(members_[i].spriteId, dir); 80 game.getSystem<AnimationSystem>().setSpriteDirection(members_[i].spriteId, dir);
80 } 81 }
81 82
82 return; 83 return;
83 } else { 84 } else {
84 game.setSpriteDirection(members_[0].spriteId, dir); 85 game.getSystem<AnimationSystem>().setSpriteDirection(members_[0].spriteId, dir);
85 } 86 }
86 87
87 int speed = MOVEMENT_SPEED; 88 int speed = MOVEMENT_SPEED;
@@ -89,7 +90,7 @@ void Party::move(Game& game, Mixer& mixer, const Input& keystate) {
89 speed *= 2; 90 speed *= 2;
90 } else { 91 } else {
91 for (int i = 0; i < members_.size(); i++) { 92 for (int i = 0; i < members_.size(); i++) {
92 game.setSpriteState(members_[i].spriteId, "walk"); 93 game.getSystem<AnimationSystem>().setSpriteAnimation(members_[i].spriteId, "walk");
93 } 94 }
94 } 95 }
95 96
@@ -101,9 +102,9 @@ void Party::move(Game& game, Mixer& mixer, const Input& keystate) {
101 102
102 const vec2i UL_COL_BOX = { 8, 8 }; 103 const vec2i UL_COL_BOX = { 8, 8 };
103 const vec2i DR_COL_BOX = { 4, 0 }; 104 const vec2i DR_COL_BOX = { 4, 0 };
104 vec2i oldColPosUL = (p1.loc() - UL_COL_BOX) / map.getTileSize(); 105 vec2i oldColPosUL = (p1.loc - UL_COL_BOX) / map.getTileSize();
105 vec2i newColPosUL = (pLoc - UL_COL_BOX) / map.getTileSize(); 106 vec2i newColPosUL = (pLoc - UL_COL_BOX) / map.getTileSize();
106 vec2i oldColPosDR = (p1.loc() + DR_COL_BOX) / map.getTileSize(); 107 vec2i oldColPosDR = (p1.loc + DR_COL_BOX) / map.getTileSize();
107 vec2i newColPosDR = (pLoc + DR_COL_BOX) / map.getTileSize(); 108 vec2i newColPosDR = (pLoc + DR_COL_BOX) / map.getTileSize();
108 109
109 if (dirHasDir(dir, Direction::right) && 110 if (dirHasDir(dir, Direction::right) &&
@@ -111,7 +112,7 @@ void Party::move(Game& game, Mixer& mixer, const Input& keystate) {
111 for (int y = newColPosUL.y(); y <= newColPosDR.y(); y++) { 112 for (int y = newColPosUL.y(); y <= newColPosDR.y(); y++) {
112 if (map.isBlocked(newColPosDR.x(), y)) { 113 if (map.isBlocked(newColPosDR.x(), y)) {
113 blocked = true; 114 blocked = true;
114 pLoc.x() = p1.loc().x();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1; 115 pLoc.x() = p1.loc.x();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1;
115 break; 116 break;
116 } 117 }
117 } 118 }
@@ -122,7 +123,7 @@ void Party::move(Game& game, Mixer& mixer, const Input& keystate) {
122 for (int y = newColPosUL.y(); y <= newColPosDR.y(); y++) { 123 for (int y = newColPosUL.y(); y <= newColPosDR.y(); y++) {
123 if (map.isBlocked(newColPosUL.x(), y)) { 124 if (map.isBlocked(newColPosUL.x(), y)) {
124 blocked = true; 125 blocked = true;
125 pLoc.x() = p1.loc().x();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1; 126 pLoc.x() = p1.loc.x();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1;
126 break; 127 break;
127 } 128 }
128 } 129 }
@@ -133,7 +134,7 @@ void Party::move(Game& game, Mixer& mixer, const Input& keystate) {
133 for (int x = newColPosUL.x(); x <= newColPosDR.x(); x++) { 134 for (int x = newColPosUL.x(); x <= newColPosDR.x(); x++) {
134 if (map.isBlocked(x, newColPosDR.y())) { 135 if (map.isBlocked(x, newColPosDR.y())) {
135 blocked = true; 136 blocked = true;
136 pLoc.y() = p1.loc().y();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1; 137 pLoc.y() = p1.loc.y();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1;
137 break; 138 break;
138 } 139 }
139 } 140 }
@@ -144,7 +145,7 @@ void Party::move(Game& game, Mixer& mixer, const Input& keystate) {
144 for (int x = newColPosUL.x(); x <= newColPosDR.x(); x++) { 145 for (int x = newColPosUL.x(); x <= newColPosDR.x(); x++) {
145 if (map.isBlocked(x, newColPosUL.y())) { 146 if (map.isBlocked(x, newColPosUL.y())) {
146 blocked = true; 147 blocked = true;
147 pLoc.y() = p1.loc().y();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1; 148 pLoc.y() = p1.loc.y();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1;
148 break; 149 break;
149 } 150 }
150 } 151 }
@@ -156,14 +157,14 @@ void Party::move(Game& game, Mixer& mixer, const Input& keystate) {
156 } 157 }
157 158
158 // Move everything 159 // Move everything
159 if (pLoc != p1.loc()) { 160 if (pLoc != p1.loc) {
160 game.getSystem<TransformSystem>().moveSprite(members_[0].spriteId, pLoc); 161 game.getSystem<TransformSystem>().moveSprite(members_[0].spriteId, pLoc);
161 162
162 for (int i = 1; i < members_.size(); i++) { 163 for (int i = 1; i < members_.size(); i++) {
163 const Sprite& pNext = game.getSprite(members_[i].spriteId); 164 const Sprite& pNext = game.getSprite(members_[i].spriteId);
164 const Movement& posdir = members_[i].movement.front(); 165 const Movement& posdir = members_[i].movement.front();
165 game.getSystem<TransformSystem>().moveSprite(members_[i].spriteId, posdir.pos); 166 game.getSystem<TransformSystem>().moveSprite(members_[i].spriteId, posdir.pos);
166 game.setSpriteDirection(members_[i].spriteId, posdir.dir); 167 game.getSystem<AnimationSystem>().setSpriteDirection(members_[i].spriteId, posdir.dir);
167 168
168 members_[i].movement.pop_front(); 169 members_[i].movement.pop_front();
169 members_[i].movement.push_back({.pos = pLoc, .dir = dir}); 170 members_[i].movement.push_back({.pos = pLoc, .dir = dir});
@@ -178,7 +179,7 @@ void Party::beginCrouch(Game& game) {
178 state_ = State::Crouching; 179 state_ = State::Crouching;
179 180
180 for (int i = 0; i < members_.size(); i++) { 181 for (int i = 0; i < members_.size(); i++) {
181 game.setSpriteState(members_[i].spriteId, "crouch"); 182 game.getSystem<AnimationSystem>().setSpriteAnimation(members_[i].spriteId, "crouch");
182 } 183 }
183 } 184 }
184} 185}
@@ -188,7 +189,7 @@ void Party::endCrouch(Game& game) {
188 state_ = State::Running; 189 state_ = State::Running;
189 190
190 for (int i = 0; i < members_.size(); i++) { 191 for (int i = 0; i < members_.size(); i++) {
191 game.setSpriteState(members_[i].spriteId, "run"); 192 game.getSystem<AnimationSystem>().setSpriteAnimation(members_[i].spriteId, "run");
192 193
193 // Halve the movement buffer for the followers. 194 // Halve the movement buffer for the followers.
194 if (i > 0) { 195 if (i > 0) {
@@ -212,7 +213,7 @@ void Party::stopRunning(Game& game) {
212 // Double the movement buffer for the followers. 213 // Double the movement buffer for the followers.
213 for (int i = 1; i < members_.size(); i++) { 214 for (int i = 1; i < members_.size(); i++) {
214 std::deque<Movement> newMove; 215 std::deque<Movement> newMove;
215 vec2i lastPos = game.getSprite(members_[i].spriteId).loc(); 216 vec2i lastPos = game.getSprite(members_[i].spriteId).loc;
216 217
217 while (!members_[i].movement.empty()) { 218 while (!members_[i].movement.empty()) {
218 Movement m1 = members_[i].movement.front(); 219 Movement m1 = members_[i].movement.front();
diff --git a/src/renderer.cpp b/src/renderer.cpp index d9ce396..92083db 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp
@@ -112,10 +112,10 @@ void Renderer::render(Game& game) {
112 SDL_RenderCopy(ren_.get(), renLay1_.get(), nullptr, nullptr); 112 SDL_RenderCopy(ren_.get(), renLay1_.get(), nullptr, nullptr);
113 113
114 for (const Sprite& sprite : game.getSystem<TransformSystem>().getSpritesByY() | game.spriteView()) { 114 for (const Sprite& sprite : game.getSystem<TransformSystem>().getSpritesByY() | game.spriteView()) {
115 const SpriteFrame& frame = sprite.getFrame(); 115 const SpriteFrame& frame = sprite.frames.at(sprite.animations.at(sprite.animationId).at(sprite.animationFrame));
116 const SDL_Rect& src = frame.srcRect; 116 const SDL_Rect& src = frame.srcRect;
117 SDL_Rect dest { sprite.loc().x() - frame.center.x(), sprite.loc().y() - frame.center.y(), frame.size.w(), frame.size.h() }; 117 SDL_Rect dest { sprite.loc.x() - frame.center.x(), sprite.loc.y() - frame.center.y(), frame.size.w(), frame.size.h() };
118 SDL_RenderCopy(ren_.get(), textures_.at(sprite.getTextureId()).get(), &src, &dest); 118 SDL_RenderCopy(ren_.get(), textures_.at(sprite.textureId).get(), &src, &dest);
119 } 119 }
120 120
121 SDL_RenderCopy(ren_.get(), renLay0_.get(), nullptr, nullptr); 121 SDL_RenderCopy(ren_.get(), renLay0_.get(), nullptr, nullptr);
diff --git a/src/sprite.h b/src/sprite.h index dc26fad..e842192 100644 --- a/src/sprite.h +++ b/src/sprite.h
@@ -17,39 +17,19 @@ struct SpriteFrame {
17class Sprite { 17class Sprite {
18public: 18public:
19 19
20 Sprite(std::string_view filename, Renderer& renderer); 20 // Transform
21 21 vec2i loc { 0, 0 };
22 int getTextureId() const { 22
23 return textureId_; 23 // Animation
24 } 24 bool isAnimated = false;
25 25 int textureId;
26 const vec2i& loc() const { return loc_; } 26 Direction dir = Direction::down;
27 27 std::string animationName;
28 vec2i& loc() { return loc_; } 28 int animationId = 0;
29 29 int animationFrame = 0;
30 const SpriteFrame& getFrame() const { return frames_.at(animations_[curAnim_][curFrame_]); } 30 std::vector<SpriteFrame> frames;
31 31 std::vector<std::vector<int>> animations;
32 void setDirection(Direction dir); 32 std::map<std::string, std::map<Direction, int>> nameDirToAnim;
33
34 Direction getDirection() const { return curDir_; }
35
36 void setState(std::string state);
37
38 void tickAnim();
39
40private:
41
42 void updateAnimation();
43
44 int textureId_;
45 vec2i loc_ { 0, 0 };
46 Direction curDir_ = Direction::down;
47 std::string state_;
48 int curAnim_ = 0;
49 int curFrame_ = 0;
50 std::vector<SpriteFrame> frames_;
51 std::vector<std::vector<int>> animations_;
52 std::map<std::string, std::map<Direction, int>> stateDirToAnim_;
53}; 33};
54 34
55#endif /* end of include guard: SPRITE_H_70503825 */ 35#endif /* end of include guard: SPRITE_H_70503825 */
diff --git a/src/system.h b/src/system.h index 0fb1bcd..c601bc7 100644 --- a/src/system.h +++ b/src/system.h
@@ -3,7 +3,8 @@
3 3
4enum class SystemKey { 4enum class SystemKey {
5 Transform, 5 Transform,
6 Camera 6 Camera,
7 Animation
7}; 8};
8 9
9class System { 10class System {
diff --git a/src/transform_system.cpp b/src/transform_system.cpp index e53fb7d..0e602e0 100644 --- a/src/transform_system.cpp +++ b/src/transform_system.cpp
@@ -3,17 +3,17 @@
3 3
4void TransformSystem::initSprite(int spriteId, vec2i loc) { 4void TransformSystem::initSprite(int spriteId, vec2i loc) {
5 Sprite& sprite = game_.getSprite(spriteId); 5 Sprite& sprite = game_.getSprite(spriteId);
6 sprite.loc() = loc; 6 sprite.loc = loc;
7 spritesByY_.emplace(loc.y(), spriteId); 7 spritesByY_.emplace(loc.y(), spriteId);
8} 8}
9 9
10void TransformSystem::moveSprite(int spriteId, vec2i newLoc) { 10void TransformSystem::moveSprite(int spriteId, vec2i newLoc) {
11 Sprite& sprite = game_.getSprite(spriteId); 11 Sprite& sprite = game_.getSprite(spriteId);
12 bool changedY = (sprite.loc().y() != newLoc.y()); 12 bool changedY = (sprite.loc.y() != newLoc.y());
13 if (changedY) { 13 if (changedY) {
14 spritesByY_.erase(std::make_tuple(sprite.loc().y(), spriteId)); 14 spritesByY_.erase(std::make_tuple(sprite.loc.y(), spriteId));
15 } 15 }
16 sprite.loc() = newLoc; 16 sprite.loc = newLoc;
17 if (changedY) { 17 if (changedY) {
18 spritesByY_.emplace(newLoc.y(), spriteId); 18 spritesByY_.emplace(newLoc.y(), spriteId);
19 } 19 }