spacebox/src/Animation.cpp

103 lines
2.2 KiB
C++
Raw Normal View History

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