85 lines
1.6 KiB
C++
85 lines
1.6 KiB
C++
#include "Animation.hpp"
|
|
|
|
void Animation::play(float delay, bool play_once)
|
|
{
|
|
this->delay = delay;
|
|
playing = true;
|
|
paused = false;
|
|
previous_step_time = timer.elapsed;
|
|
overflow = 0;
|
|
count = 0;
|
|
ending = play_once;
|
|
if (delay <= 0)
|
|
{
|
|
timer.toggle(true);
|
|
}
|
|
}
|
|
|
|
void Animation::set_frame_length(float length)
|
|
{
|
|
frame_length = length;
|
|
}
|
|
|
|
void Animation::play_once(float delay)
|
|
{
|
|
play(delay, true);
|
|
}
|
|
|
|
void Animation::pause()
|
|
{
|
|
timer.toggle(false);
|
|
paused = true;
|
|
}
|
|
|
|
void Animation::unpause()
|
|
{
|
|
timer.toggle(true);
|
|
paused = false;
|
|
}
|
|
|
|
void Animation::reset()
|
|
{
|
|
timer.toggle(false);
|
|
playing = false;
|
|
timer.reset();
|
|
}
|
|
|
|
bool Animation::is_playing(bool include_delay)
|
|
{
|
|
return playing && !paused && (include_delay || delay <= 0);
|
|
}
|
|
|
|
void Animation::update()
|
|
{
|
|
timer.update();
|
|
if (playing && !paused && containing_object->is_active())
|
|
{
|
|
if (delay > 0)
|
|
{
|
|
delay -= timer.frame_duration;
|
|
if (delay <= 0)
|
|
{
|
|
timer.toggle(true);
|
|
}
|
|
}
|
|
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();
|
|
count++;
|
|
if (ending)
|
|
{
|
|
reset();
|
|
}
|
|
if (overflow > frame_length)
|
|
{
|
|
overflow = 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|