#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; } }