summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2021-02-13 12:14:58 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2021-02-13 12:14:58 -0500
commited933607765a6e010689aaaf85184053ff6e8a2b (patch)
tree9c59ddfa9c7ddb221788485ad96cad116f8b0c28 /src
parente9602acecd1c5f63c6cf72a270a1bd09eba1e7e1 (diff)
downloadtanetane-ed933607765a6e010689aaaf85184053ff6e8a2b.tar.gz
tanetane-ed933607765a6e010689aaaf85184053ff6e8a2b.tar.bz2
tanetane-ed933607765a6e010689aaaf85184053ff6e8a2b.zip
Added non-looping animations
Lucas can get electrocuted now.
Diffstat (limited to 'src')
-rw-r--r--src/animation_system.cpp29
-rw-r--r--src/renderer.cpp2
-rw-r--r--src/script_system.cpp3
-rw-r--r--src/sprite.h8
4 files changed, 31 insertions, 11 deletions
diff --git a/src/animation_system.cpp b/src/animation_system.cpp index 997b7f7..3f3f22a 100644 --- a/src/animation_system.cpp +++ b/src/animation_system.cpp
@@ -65,21 +65,26 @@ void AnimationSystem::initSprite(int spriteId, std::string_view filename) {
65 std::string animLine; 65 std::string animLine;
66 std::getline(datafile, animLine); // blank 66 std::getline(datafile, animLine); // blank
67 while (std::getline(datafile, animLine)) { 67 while (std::getline(datafile, animLine)) {
68 std::regex re(R"(([a-z]+)\[([a-z_]+)\]: ([0-9,]+))"); 68 std::regex re(R"(([a-z!]+)\[([a-z_]+)\]: ([0-9,]+))");
69 std::smatch m; 69 std::smatch m;
70 std::regex_match(animLine, m, re); 70 std::regex_match(animLine, m, re);
71 71
72 std::vector<int> frames; 72 std::string animName = m[1];
73 Animation anim;
73 auto framestrs = splitStr<std::list<std::string>>(m[3], ","); 74 auto framestrs = splitStr<std::list<std::string>>(m[3], ",");
74 for (const std::string& f : framestrs) { 75 for (const std::string& f : framestrs) {
75 frames.push_back(std::stoi(f)); 76 anim.frameIndices.push_back(std::stoi(f));
77 }
78
79 if (animName.back() == '!') {
80 anim.looping = false;
76 } 81 }
77 82
78 int animId = sprite.animations.size(); 83 int animId = sprite.animations.size();
79 sprite.animations.push_back(std::move(frames)); 84 sprite.animations.push_back(std::move(anim));
80 85
81 Direction dir = directionFromString(std::string(m[2])); 86 Direction dir = directionFromString(std::string(m[2]));
82 sprite.nameDirToAnim[m[1]][dir] = animId; 87 sprite.nameDirToAnim[animName][dir] = animId;
83 } 88 }
84 89
85 updateAnimation(spriteId); 90 updateAnimation(spriteId);
@@ -89,10 +94,17 @@ void AnimationSystem::tick(double dt) {
89 animTimer_.accumulate(dt); 94 animTimer_.accumulate(dt);
90 while (animTimer_.step()) { 95 while (animTimer_.step()) {
91 for (Sprite& sprite : game_.getSprites() | game_.spriteView()) { 96 for (Sprite& sprite : game_.getSprites() | game_.spriteView()) {
92 if (sprite.isAnimated) { 97 if (sprite.isAnimated && !sprite.animFinished) {
93 sprite.animationFrame++; 98 sprite.animationFrame++;
94 if (sprite.animationFrame >= sprite.animations[sprite.animationId].size()) { 99 if (sprite.animations[sprite.animationId].looping) {
95 sprite.animationFrame = 0; 100 if (sprite.animationFrame >= sprite.animations[sprite.animationId].frameIndices.size()) {
101 sprite.animationFrame = 0;
102 }
103 } else {
104 if (sprite.animationFrame >= sprite.animations[sprite.animationId].frameIndices.size() - 1) {
105 sprite.animationFrame = sprite.animations[sprite.animationId].frameIndices.size() - 1;
106 sprite.animFinished = true;
107 }
96 } 108 }
97 } 109 }
98 } 110 }
@@ -119,4 +131,5 @@ void AnimationSystem::updateAnimation(int spriteId) {
119 Sprite& sprite = game_.getSprite(spriteId); 131 Sprite& sprite = game_.getSprite(spriteId);
120 sprite.animationId = sprite.nameDirToAnim[sprite.animationName][sprite.dir]; 132 sprite.animationId = sprite.nameDirToAnim[sprite.animationName][sprite.dir];
121 sprite.animationFrame = 0; 133 sprite.animationFrame = 0;
134 sprite.animFinished = false;
122} 135}
diff --git a/src/renderer.cpp b/src/renderer.cpp index f07931b..a7169e9 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp
@@ -130,7 +130,7 @@ void Renderer::render(Game& game) {
130 SDL_RenderCopy(ren_.get(), textures_.at(shadowTexId).get(), nullptr, &shadowDest); 130 SDL_RenderCopy(ren_.get(), textures_.at(shadowTexId).get(), nullptr, &shadowDest);
131 } 131 }
132 132
133 const SpriteFrame& frame = sprite.frames.at(sprite.animations.at(sprite.animationId).at(sprite.animationFrame)); 133 const SpriteFrame& frame = sprite.frames.at(sprite.animations.at(sprite.animationId).frameIndices.at(sprite.animationFrame));
134 const SDL_Rect& src = frame.srcRect; 134 const SDL_Rect& src = frame.srcRect;
135 SDL_Rect dest { sprite.loc.x() - frame.center.x(), sprite.loc.y() - frame.center.y(), frame.size.w(), frame.size.h() }; 135 SDL_Rect dest { sprite.loc.x() - frame.center.x(), sprite.loc.y() - frame.center.y(), frame.size.w(), frame.size.h() };
136 SDL_RenderCopy(ren_.get(), textures_.at(loadImageFromFile(sprite.spritesheet)).get(), &src, &dest); 136 SDL_RenderCopy(ren_.get(), textures_.at(loadImageFromFile(sprite.spritesheet)).get(), &src, &dest);
diff --git a/src/script_system.cpp b/src/script_system.cpp index 5b9b987..3f89290 100644 --- a/src/script_system.cpp +++ b/src/script_system.cpp
@@ -16,7 +16,8 @@ ScriptSystem::ScriptSystem(Game& game) : game_(game) {
16 "dir", &Sprite::dir, 16 "dir", &Sprite::dir,
17 "followers", &Sprite::followers, 17 "followers", &Sprite::followers,
18 "characterState", &Sprite::characterState, 18 "characterState", &Sprite::characterState,
19 "controllable", &Sprite::controllable); 19 "controllable", &Sprite::controllable,
20 "animFinished", &Sprite::animFinished);
20 21
21 engine_.new_usertype<MessageSystem>( 22 engine_.new_usertype<MessageSystem>(
22 "message", 23 "message",
diff --git a/src/sprite.h b/src/sprite.h index 84a7b03..fcf7e1d 100644 --- a/src/sprite.h +++ b/src/sprite.h
@@ -16,6 +16,11 @@ struct SpriteFrame {
16 vec2i size; 16 vec2i size;
17}; 17};
18 18
19struct Animation {
20 bool looping = true;
21 std::vector<int> frameIndices;
22};
23
19enum class CharacterState { 24enum class CharacterState {
20 Still, 25 Still,
21 Walking, 26 Walking,
@@ -48,8 +53,9 @@ public:
48 int animationId = 0; 53 int animationId = 0;
49 int animationFrame = 0; 54 int animationFrame = 0;
50 std::vector<SpriteFrame> frames; 55 std::vector<SpriteFrame> frames;
51 std::vector<std::vector<int>> animations; 56 std::vector<Animation> animations;
52 std::map<std::string, std::map<Direction, int>> nameDirToAnim; 57 std::map<std::string, std::map<Direction, int>> nameDirToAnim;
58 bool animFinished = false;
53 bool hasShadow = false; 59 bool hasShadow = false;
54 60
55 // Character 61 // Character