summary refs log tree commit diff stats
path: root/src/animation_system.cpp
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 /src/animation_system.cpp
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!
Diffstat (limited to 'src/animation_system.cpp')
-rw-r--r--src/animation_system.cpp44
1 files changed, 30 insertions, 14 deletions
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 }