summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2021-02-24 10:35:56 -0500
committerStar Rauchenberger <fefferburbia@gmail.com>2021-02-24 16:00:53 -0500
commitdf093323e48426e8d54a504aeae4155fa3c8e605 (patch)
treeec8f5d4abb898a69607e3f29e7572ae7841675a5
parentbf39e32f2ec9008e48b0dd3ad53d640434e0738a (diff)
downloadtanetane-df093323e48426e8d54a504aeae4155fa3c8e605.tar.gz
tanetane-df093323e48426e8d54a504aeae4155fa3c8e605.tar.bz2
tanetane-df093323e48426e8d54a504aeae4155fa3c8e605.zip
Added variable animation frame rates
Lucas's climbing animation now accurately uses 60fps and looks correct finally!
-rw-r--r--res/sprites/lucas_anim.txt4
-rw-r--r--src/animation_system.cpp44
-rw-r--r--src/animation_system.h3
-rw-r--r--src/sprite.h1
4 files changed, 35 insertions, 17 deletions
diff --git a/res/sprites/lucas_anim.txt b/res/sprites/lucas_anim.txt index 0579e94..f6ed06e 100644 --- a/res/sprites/lucas_anim.txt +++ b/res/sprites/lucas_anim.txt
@@ -42,5 +42,5 @@ run[right]: 73,81,89,97
42run[down_right]: 74,82,90,98 42run[down_right]: 74,82,90,98
43lightning_electrocute![down]: 116,117,118,116,117,118 43lightning_electrocute![down]: 116,117,118,116,117,118
44lightning_collapse![down]: 119,120,121,122 44lightning_collapse![down]: 119,120,121,122
45climb[up]: 123,124,125,128,127,127,129,130,131,128,127,126 45climb[up]%60: 123#10,124,125,128#10,127,126,129#10,130,131,128#10,127,126
46climb[down]: 123,124,125,128,127,127,129,130,131,128,127,126 \ No newline at end of file 46climb[down]%60: 123#10,124,125,128#10,127,126,129#10,130,131,128#10,127,126 \ No newline at end of file
diff --git a/src/animation_system.cpp b/src/animation_system.cpp index b57816b..baf94a4 100644 --- a/src/animation_system.cpp +++ b/src/animation_system.cpp
@@ -65,13 +65,13 @@ 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]+)?: ([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::string animName = m[1]; 72 std::string animName = m[1];
73 Animation anim; 73 Animation anim;
74 auto framestrs = splitStr<std::list<std::string>>(m[3], ","); 74 auto framestrs = splitStr<std::list<std::string>>(m[4], ",");
75 for (const std::string& f : framestrs) { 75 for (const std::string& f : framestrs) {
76 int times = 1; 76 int times = 1;
77 size_t repeat_it = f.find("#"); 77 size_t repeat_it = f.find("#");
@@ -88,6 +88,15 @@ void AnimationSystem::initSprite(int spriteId, std::string_view filename) {
88 anim.looping = false; 88 anim.looping = false;
89 } 89 }
90 90
91 if (m[3] != "") {
92 int frameRate = std::stoi(std::string(m[3]).substr(1));
93 switch (frameRate) {
94 case 5: anim.timerNum = 0; break;
95 case 60: anim.timerNum = 1; break;
96 default: throw std::invalid_argument("Invalid frame rate");
97 }
98 }
99
91 int animId = sprite.animations.size(); 100 int animId = sprite.animations.size();
92 sprite.animations.push_back(std::move(anim)); 101 sprite.animations.push_back(std::move(anim));
93 102
@@ -101,18 +110,25 @@ void AnimationSystem::initSprite(int spriteId, std::string_view filename) {
101void AnimationSystem::tick(double dt) { 110void AnimationSystem::tick(double dt) {
102 if (game_.isGameplayPaused()) return; 111 if (game_.isGameplayPaused()) return;
103 112
104 animTimer_.accumulate(dt); 113 for (int timerNum = 0; timerNum < animTimers_.size(); timerNum++) {
105 while (animTimer_.step()) { 114 Timer& animTimer = animTimers_[timerNum];
106 for (Sprite& sprite : game_.getSprites() | game_.spriteView()) { 115
107 if (sprite.isAnimated && !sprite.animFinished && !sprite.animPaused) { 116 animTimer.accumulate(dt);
108 sprite.animationFrame++; 117 while (animTimer.step()) {
109 118 for (Sprite& sprite : game_.getSprites() | game_.spriteView()) {
110 if (sprite.animationFrame >= sprite.animations[sprite.animationId].frameIndices.size()) { 119 if (sprite.isAnimated &&
111 if (sprite.animations[sprite.animationId].looping) { 120 sprite.animations[sprite.animationId].timerNum == timerNum &&
112 sprite.animationFrame = 0; 121 !sprite.animFinished &&
113 } else { 122 !sprite.animPaused) {
114 sprite.animFinished = true; 123 sprite.animationFrame++;
115 sprite.animationFrame = sprite.animations[sprite.animationId].frameIndices.size() - 1; 124
125 if (sprite.animationFrame >= sprite.animations[sprite.animationId].frameIndices.size()) {
126 if (sprite.animations[sprite.animationId].looping) {
127 sprite.animationFrame = 0;
128 } else {
129 sprite.animFinished = true;
130 sprite.animationFrame = sprite.animations[sprite.animationId].frameIndices.size() - 1;
131 }
116 } 132 }
117 } 133 }
118 } 134 }
diff --git a/src/animation_system.h b/src/animation_system.h index a116673..c64c2dc 100644 --- a/src/animation_system.h +++ b/src/animation_system.h
@@ -2,6 +2,7 @@
2#define ANIMATION_SYSTEM_H_CCCC7CB8 2#define ANIMATION_SYSTEM_H_CCCC7CB8
3 3
4#include <string_view> 4#include <string_view>
5#include <vector>
5#include "direction.h" 6#include "direction.h"
6#include "system.h" 7#include "system.h"
7#include "timer.h" 8#include "timer.h"
@@ -28,7 +29,7 @@ private:
28 void updateAnimation(int spriteId); 29 void updateAnimation(int spriteId);
29 30
30 Game& game_; 31 Game& game_;
31 Timer animTimer_ {1000/5};//30fps * 1000 t/s;; 32 std::vector<Timer> animTimers_ = {{1000/5}, {1000/60}};//30fps * 1000 t/s;;
32}; 33};
33 34
34#endif /* end of include guard: ANIMATION_SYSTEM_H_CCCC7CB8 */ 35#endif /* end of include guard: ANIMATION_SYSTEM_H_CCCC7CB8 */
diff --git a/src/sprite.h b/src/sprite.h index 5750308..2bbc4ea 100644 --- a/src/sprite.h +++ b/src/sprite.h
@@ -24,6 +24,7 @@ struct SpriteFrame {
24struct Animation { 24struct Animation {
25 bool looping = true; 25 bool looping = true;
26 std::vector<int> frameIndices; 26 std::vector<int> frameIndices;
27 int timerNum = 0;
27}; 28};
28 29
29enum class CharacterState { 30enum class CharacterState {