spacebox/src/Animation.cpp

88 lines
1.6 KiB
C++

#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::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();
step();
if (ending)
{
reset();
}
if (overflow > _frame_length)
{
overflow = 0;
}
return true;
} } }
return false;
}