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;
paused = false;
previous_step_time = timer.elapsed();
overflow = 0;
ending = play_once;
if (delay <= 0)
{
timer.on();
}
}
void Animation::set_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);
}
void Animation::update(float timestamp)
{
timer.update(timestamp);
if (playing() && containing_object->is_active())
{
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;
}
}
}
}
}