#include "Animation.h" // linear interpolation constructor template Animation::Animation(float _d, T _ev, T* _t, std::function _cb, bool _l) :duration(_d), endvalue(_ev), target(_t), callback(_cb), loop(_l), index(-1), startvalue(*_t), elapsed(0.0f) {} // discrete values constructor template Animation::Animation(float _d, std::vector _v, T* _t, std::function _cb, bool _l) :duration(_d), target(_t), callback(_cb), values(_v), loop(_l), index(0), startvalue(*_t), elapsed(0.0f), nonelapsed(0.0f) { timestep = _v.size() / _d; } template Animation::~Animation() { // deconstructor sets target to desired end state (no partial values) cancel(); } template<> void Animation::lerp() { *target = endvalue.substr(0, endvalue.length() * (elapsed / duration)); } template<> void Animation::lerp() { int delta = endvalue - startvalue; *target = startvalue + (elapsed / duration * delta); } template<> void Animation::lerp() { int delta = endvalue - startvalue; *target = startvalue + (elapsed / duration * delta); } template<> void Animation::lerp() { int delta_x = endvalue.x - startvalue.x; int delta_y = endvalue.y - startvalue.y; target->x = startvalue.x + (elapsed / duration * delta_x); target->y = startvalue.y + (elapsed / duration * delta_y); } template<> void Animation::lerp() { int delta_x = endvalue.x - startvalue.y; int delta_y = endvalue.y - startvalue.y; target->x = startvalue.x + (elapsed / duration * delta_x); target->y = startvalue.y + (elapsed / duration * delta_y); } template void Animation::step(float delta) { if (index == -1) { // lerp function elapsed += delta; lerp(); } else { nonelapsed += delta; if (nonelapsed < timestep) return; if (index == values.size() - 1) return; elapsed += nonelapsed; // or should it be += timestep? nonelapsed = 0; // or should it -= timestep? index++; *target = values[index]; } } template void Animation::cancel() { if (index == -1) *target = endvalue; else *target = values[values.size() - 1]; }