spacebox/src/Animation.cpp

103 lines
2.2 KiB
C++

/* ✨ +------------------------------------------------------+
____/ \____ ✨/| Open source game framework licensed to freely use, |
✨\ / / | copy, and modify. Created for 🌠dank.game🌠 |
+--\ . . /--+ | |
| ~/ ︶ \👍| | 🌐 https://open.shampoo.ooo/shampoo/spacebox |
| ~~~🌊~~~~🌊~ | +------------------------------------------------------+
| SPACE 🪐🅱 OX | /
| 🌊 ~ ~~~~ ~~ |/
+-------------*/
#include "Animation.hpp"
Animation::Animation()
{
timer.off();
}
void Animation::play(float delay, bool play_once)
{
this->delay = delay;
play_state = true;
unpause();
previous_step_time = timer.elapsed();
overflow = 0;
ending = play_once;
if (delay <= 0)
{
timer.on();
}
}
void Animation::frame_length(float length)
{
_frame_length = length;
}
void Animation::play_once(float delay)
{
play(delay, true);
}
void Animation::pause()
{
timer.off();
paused = true;
}
void Animation::unpause()
{
timer.on();
paused = false;
}
void Animation::toggle(bool state)
{
state ? unpause() : pause();
}
void Animation::reset()
{
timer.off();
play_state = false;
timer.reset();
}
bool Animation::playing(bool include_delay) const
{
return play_state && !paused && (include_delay || delay <= 0);
}
bool Animation::update(float timestamp)
{
timer.update(timestamp);
if (playing())
{
if (delay > 0)
{
delay -= timer.frame();
if (delay <= 0)
{
timer.on();
}
}
if (delay <= 0)
{
if (timer.elapsed() - previous_step_time + overflow > _frame_length)
{
overflow = timer.elapsed() - previous_step_time + overflow - _frame_length;
previous_step_time = timer.elapsed();
if (ending)
{
reset();
}
step();
if (overflow > _frame_length)
{
overflow = 0;
}
return true;
} } }
return false;
}