summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/animation_system.cpp44
-rw-r--r--src/animation_system.h3
-rw-r--r--src/sprite.h1
3 files changed, 33 insertions, 15 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 }
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 {