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-03 01:35:58 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2021-02-03 01:35:58 -0500
commitbe09120d1d044b476ef8b516efbdb526f20d9e2d (patch)
treef935389835d5f94a9cd3bb2059cf55174c9aad69 /src/animation_system.cpp
parent24918837c3ff9026d228657d14852c9cf39a5644 (diff)
downloadtanetane-be09120d1d044b476ef8b516efbdb526f20d9e2d.tar.gz
tanetane-be09120d1d044b476ef8b516efbdb526f20d9e2d.tar.bz2
tanetane-be09120d1d044b476ef8b516efbdb526f20d9e2d.zip
Added animation system
Diffstat (limited to 'src/animation_system.cpp')
-rw-r--r--src/animation_system.cpp122
1 files changed, 122 insertions, 0 deletions
diff --git a/src/animation_system.cpp b/src/animation_system.cpp new file mode 100644 index 0000000..3320e1d --- /dev/null +++ b/src/animation_system.cpp
@@ -0,0 +1,122 @@
1#include "animation_system.h"
2#include <fstream>
3#include <string>
4#include <list>
5#include "game.h"
6#include "vector.h"
7#include "util.h"
8
9void AnimationSystem::initSprite(int spriteId, std::string_view filename, Renderer& renderer) {
10 std::ifstream datafile(filename.data());
11 if (!datafile.is_open()) {
12 throw std::invalid_argument(std::string("Could not find sprite datafile: ") + std::string(filename));
13 }
14
15 Sprite& sprite = game_.getSprite(spriteId);
16 sprite.isAnimated = true;
17
18 char ch;
19 std::string line;
20
21 std::string imagename;
22 datafile >> imagename;
23 sprite.textureId = renderer.loadImageFromFile(imagename);
24
25 std::string framefilename;
26 datafile >> framefilename;
27
28 std::ifstream framefile(framefilename);
29 if (!framefile.is_open()) {
30 throw std::invalid_argument("Could not find frame datafile: " + framefilename);
31 }
32
33 vec2i cellSize;
34 framefile >> cellSize.w();
35 framefile >> ch; //,
36 framefile >> cellSize.h();
37 std::getline(framefile, line); // cell size
38
39 int framesPerRow;
40 framefile >> framesPerRow;
41 std::getline(framefile, line); // frames per row
42
43 int numFrames;
44 framefile >> numFrames;
45 std::getline(framefile, line); // frames
46 std::getline(framefile, line); // blank
47
48 for (int i=0; i<numFrames; i++) {
49 SpriteFrame f;
50 framefile >> f.size.w();
51 framefile >> ch; //,
52 framefile >> f.size.h();
53 framefile >> ch; //,
54 framefile >> f.center.x();
55 framefile >> ch; //,
56 framefile >> f.center.y();
57 std::getline(framefile, line); // blank
58
59 f.srcRect.x = (i % framesPerRow) * cellSize.w();
60 f.srcRect.y = (i / framesPerRow) * cellSize.h();
61 f.srcRect.w = f.size.w();
62 f.srcRect.h = f.size.h();
63
64 sprite.frames.push_back(std::move(f));
65 }
66
67 std::string animLine;
68 std::getline(datafile, animLine); // blank
69 while (std::getline(datafile, animLine)) {
70 std::regex re(R"(([a-z]+)\[([a-z_]+)\]: ([0-9,]+))");
71 std::smatch m;
72 std::regex_match(animLine, m, re);
73
74 std::vector<int> frames;
75 auto framestrs = splitStr<std::list<std::string>>(m[3], ",");
76 for (const std::string& f : framestrs) {
77 frames.push_back(std::stoi(f));
78 }
79
80 int animId = sprite.animations.size();
81 sprite.animations.push_back(std::move(frames));
82
83 Direction dir = directionFromString(std::string(m[2]));
84 sprite.nameDirToAnim[m[1]][dir] = animId;
85 }
86
87 updateAnimation(spriteId);
88}
89
90void AnimationSystem::tick(double dt) {
91 animTimer_.accumulate(dt);
92 while (animTimer_.step()) {
93 for (Sprite& sprite : game_.getSprites() | game_.spriteView()) {
94 sprite.animationFrame++;
95 if (sprite.animationFrame >= sprite.animations[sprite.animationId].size()) {
96 sprite.animationFrame = 0;
97 }
98 }
99 }
100}
101
102void AnimationSystem::setSpriteDirection(int spriteId, Direction dir) {
103 Sprite& sprite = game_.getSprite(spriteId);
104 if (sprite.dir != dir) {
105 sprite.dir = dir;
106 updateAnimation(spriteId);
107 }
108}
109
110void AnimationSystem::setSpriteAnimation(int spriteId, std::string_view name) {
111 Sprite& sprite = game_.getSprite(spriteId);
112 if (sprite.animationName != name) {
113 sprite.animationName = name;
114 updateAnimation(spriteId);
115 }
116}
117
118void AnimationSystem::updateAnimation(int spriteId) {
119 Sprite& sprite = game_.getSprite(spriteId);
120 sprite.animationId = sprite.nameDirToAnim[sprite.animationName][sprite.dir];
121 sprite.animationFrame = 0;
122}