diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-13 12:14:58 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-13 12:14:58 -0500 |
commit | ed933607765a6e010689aaaf85184053ff6e8a2b (patch) | |
tree | 9c59ddfa9c7ddb221788485ad96cad116f8b0c28 /src/animation_system.cpp | |
parent | e9602acecd1c5f63c6cf72a270a1bd09eba1e7e1 (diff) | |
download | tanetane-ed933607765a6e010689aaaf85184053ff6e8a2b.tar.gz tanetane-ed933607765a6e010689aaaf85184053ff6e8a2b.tar.bz2 tanetane-ed933607765a6e010689aaaf85184053ff6e8a2b.zip |
Added non-looping animations
Lucas can get electrocuted now.
Diffstat (limited to 'src/animation_system.cpp')
-rw-r--r-- | src/animation_system.cpp | 29 |
1 files changed, 21 insertions, 8 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 | } |