2023-03-06 03:44:39 +00:00
|
|
|
#include "Animation.h"
|
|
|
|
|
2023-03-07 01:54:23 +00:00
|
|
|
Animation::Animation(float _d, void* _t, std::function<void()> _cb, bool _l)
|
|
|
|
:duration(_d), target(_t), callback(_cb), loop(_l), elapsed(0.0f) {}
|
|
|
|
|
2023-03-06 03:44:39 +00:00
|
|
|
// linear interpolation constructor
|
|
|
|
template<typename T>
|
2023-03-07 01:54:23 +00:00
|
|
|
LerpAnimation<T>::LerpAnimation(float _d, T _ev, T* _t, std::function<void()> _cb, bool _l)
|
|
|
|
:Animation(_d, _t, _cb, _l), //duration(_d), target(_t), callback(_cb), loop(_l),elapsed(0.0f),
|
|
|
|
startvalue(*_t), endvalue(_ev) {}
|
2023-03-06 03:44:39 +00:00
|
|
|
|
|
|
|
// discrete values constructor
|
|
|
|
template<typename T>
|
2023-03-07 01:54:23 +00:00
|
|
|
DiscreteAnimation<T>::DiscreteAnimation(float _d, std::vector<T> _v, T* _t, std::function<void()> _cb, bool _l)
|
|
|
|
:Animation(_d, _t, _cb, _l), //duration(_d), target(_t), callback(_cb), loop(_l), elapsed(0.0f),
|
|
|
|
index(0), nonelapsed(0.0f), values(_v) {
|
2023-03-06 03:44:39 +00:00
|
|
|
timestep = _v.size() / _d;
|
|
|
|
}
|
|
|
|
|
2023-03-07 01:54:23 +00:00
|
|
|
/* // don't call virtual functions (like cancel()) from base class destructor
|
|
|
|
* // child classes destructors' are called first anyway
|
|
|
|
Animation::~Animation() {
|
2023-03-06 03:44:39 +00:00
|
|
|
// deconstructor sets target to desired end state (no partial values)
|
|
|
|
cancel();
|
|
|
|
}
|
2023-03-07 01:54:23 +00:00
|
|
|
*/
|
2023-03-06 03:44:39 +00:00
|
|
|
|
|
|
|
template<>
|
2023-03-07 01:54:23 +00:00
|
|
|
void LerpAnimation<std::string>::lerp() {
|
|
|
|
*(std::string*)target = endvalue.substr(0, endvalue.length() * (elapsed / duration));
|
2023-03-06 03:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2023-03-07 01:54:23 +00:00
|
|
|
void LerpAnimation<int>::lerp() {
|
2023-03-06 03:44:39 +00:00
|
|
|
int delta = endvalue - startvalue;
|
2023-03-07 01:54:23 +00:00
|
|
|
*(int*)target = startvalue + (elapsed / duration * delta);
|
2023-03-06 03:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2023-03-07 01:54:23 +00:00
|
|
|
void LerpAnimation<float>::lerp() {
|
2023-03-06 03:44:39 +00:00
|
|
|
int delta = endvalue - startvalue;
|
2023-03-07 01:54:23 +00:00
|
|
|
*(float*)target = startvalue + (elapsed / duration * delta);
|
2023-03-06 03:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2023-03-07 01:54:23 +00:00
|
|
|
void LerpAnimation<sf::Vector2f>::lerp() {
|
2023-03-06 03:44:39 +00:00
|
|
|
int delta_x = endvalue.x - startvalue.x;
|
|
|
|
int delta_y = endvalue.y - startvalue.y;
|
2023-03-07 01:54:23 +00:00
|
|
|
((sf::Vector2f*)target)->x = startvalue.x + (elapsed / duration * delta_x);
|
|
|
|
((sf::Vector2f*)target)->y = startvalue.y + (elapsed / duration * delta_y);
|
2023-03-06 03:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2023-03-07 01:54:23 +00:00
|
|
|
void LerpAnimation<sf::Vector2i>::lerp() {
|
2023-03-06 03:44:39 +00:00
|
|
|
int delta_x = endvalue.x - startvalue.y;
|
|
|
|
int delta_y = endvalue.y - startvalue.y;
|
2023-03-07 01:54:23 +00:00
|
|
|
((sf::Vector2i*)target)->x = startvalue.x + (elapsed / duration * delta_x);
|
|
|
|
((sf::Vector2i*)target)->y = startvalue.y + (elapsed / duration * delta_y);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void LerpAnimation<T>::step(float delta) {
|
|
|
|
elapsed += delta;
|
|
|
|
lerp();
|
|
|
|
if (isDone()) cancel(); //use the exact value, not my math
|
2023-03-06 03:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-03-07 01:54:23 +00:00
|
|
|
void DiscreteAnimation<T>::step(float delta)
|
2023-03-06 03:44:39 +00:00
|
|
|
{
|
2023-03-07 01:54:23 +00:00
|
|
|
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];
|
|
|
|
if (isDone()) cancel(); //use the exact value, not my math
|
2023-03-06 03:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-03-07 01:54:23 +00:00
|
|
|
void LerpAnimation<T>::cancel() {
|
|
|
|
*target = endvalue;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void DiscreteAnimation<T>::cancel() {
|
|
|
|
*target = values[values.size() - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Animation::isDone() {
|
|
|
|
return elapsed + Animation::EPSILON >= duration;
|
2023-03-06 03:44:39 +00:00
|
|
|
}
|