From be9ccb73bc20b03f62c77f5d529602a10ef4eda9 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sat, 12 Mar 2022 12:09:58 -0500 Subject: player has a sprite now thanks to world of solaria --- src/animation.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 src/animation.cpp (limited to 'src/animation.cpp') diff --git a/src/animation.cpp b/src/animation.cpp new file mode 100644 index 0000000..fbf7ccf --- /dev/null +++ b/src/animation.cpp @@ -0,0 +1,93 @@ +#include "animation.h" +#include +#include +#include +#include +#include +#include +#include "direction.h" +#include "util.h" + +Animation::Animation(std::string_view path) { + std::ifstream datafile(path.data()); + if (!datafile.is_open()) { + throw std::invalid_argument(std::string("Could not find sprite datafile: ") + path.data()); + } + + std::string animLine; + char ch; + int cellWidth; + int cellHeight; + datafile >> cellWidth; + datafile >> ch; //, + datafile >> cellHeight; + std::getline(datafile, animLine); // cell size + + int framesPerRow; + datafile >> framesPerRow; + std::getline(datafile, animLine); // frames per row + + int numFrames; + datafile >> numFrames; + std::getline(datafile, animLine); // frames + std::getline(datafile, animLine); // blank + + for (int i=0; i anim; + auto framestrs = splitStr>(m[3], ","); + for (const std::string& f : framestrs) { + anim.push_back(std::stoi(f)); + } + + int animId = animations_.size(); + animations_.push_back(std::move(anim)); + + Direction dir = directionFromString(std::string(m[2])); + nameDirToAnim_[animName][dir] = animId; + } + + updateAnim(); +} + +void Animation::setDirection(Direction dir) { + dir_ = dir; + updateAnim(); +} + +void Animation::setAnimation(std::string_view anim) { + animationName_ = anim; + updateAnim(); +} + +void Animation::update(int dt) { + animTimer_.accumulate(dt); + + while (animTimer_.step()) { + animationFrame_++; + + if (animationFrame_ >= animations_.at(animationId_).size()) { + animationFrame_ = 0; + } + } +} + +void Animation::updateAnim() { + if (nameDirToAnim_.at(animationName_).at(dir_) != animationId_) { + animationId_ = nameDirToAnim_.at(animationName_).at(dir_); + animationFrame_ = 0; + } +} -- cgit 1.4.1