2024-03-04 19:44:06 -05:00
|
|
|
/* ✨ +------------------------------------------------------+
|
|
|
|
____/ \____ ✨/| 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"
|
|
|
|
|
2021-09-08 20:07:11 -04:00
|
|
|
Animation::Animation()
|
|
|
|
{
|
2023-06-07 20:22:20 -04:00
|
|
|
timer.off();
|
2021-09-08 20:07:11 -04:00
|
|
|
}
|
|
|
|
|
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;
|
2023-07-08 16:19:31 -04:00
|
|
|
unpause();
|
2023-06-07 20:22:20 -04:00
|
|
|
previous_step_time = timer.elapsed();
|
2019-05-16 03:51:36 -04:00
|
|
|
overflow = 0;
|
|
|
|
ending = play_once;
|
|
|
|
if (delay <= 0)
|
|
|
|
{
|
2023-06-07 20:22:20 -04:00
|
|
|
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()
|
|
|
|
{
|
2023-06-07 20:22:20 -04:00
|
|
|
timer.off();
|
2019-05-16 03:51:36 -04:00
|
|
|
paused = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Animation::unpause()
|
|
|
|
{
|
2023-06-07 20:22:20 -04:00
|
|
|
timer.on();
|
2019-05-16 03:51:36 -04:00
|
|
|
paused = false;
|
|
|
|
}
|
|
|
|
|
2023-12-05 13:43:16 -05:00
|
|
|
void Animation::toggle(bool state)
|
|
|
|
{
|
|
|
|
state ? unpause() : pause();
|
|
|
|
}
|
|
|
|
|
2019-05-16 03:51:36 -04:00
|
|
|
void Animation::reset()
|
|
|
|
{
|
2023-06-07 20:22:20 -04:00
|
|
|
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
|
2020-08-19 03:29:46 -04:00
|
|
|
{
|
2021-09-08 23:56:06 -04:00
|
|
|
return play_state && !paused && (include_delay || delay <= 0);
|
2020-08-19 03:29:46 -04:00
|
|
|
}
|
|
|
|
|
2023-11-14 00:19:32 -05:00
|
|
|
bool Animation::update(float timestamp)
|
2019-05-16 03:51:36 -04:00
|
|
|
{
|
2023-06-07 20:22:20 -04:00
|
|
|
timer.update(timestamp);
|
2023-06-23 01:45:28 -04:00
|
|
|
if (playing())
|
2019-05-16 03:51:36 -04:00
|
|
|
{
|
|
|
|
if (delay > 0)
|
|
|
|
{
|
2023-06-07 20:22:20 -04:00
|
|
|
delay -= timer.frame();
|
2019-05-16 03:51:36 -04:00
|
|
|
if (delay <= 0)
|
|
|
|
{
|
2023-06-07 20:22:20 -04:00
|
|
|
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;
|
2023-06-07 20:22:20 -04:00
|
|
|
previous_step_time = timer.elapsed();
|
2019-05-16 03:51:36 -04:00
|
|
|
if (ending)
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
}
|
2023-12-28 19:11:35 -05:00
|
|
|
step();
|
2023-06-24 13:05:40 -04:00
|
|
|
if (overflow > _frame_length)
|
2020-06-27 17:47:14 -04:00
|
|
|
{
|
|
|
|
overflow = 0;
|
|
|
|
}
|
2023-11-14 00:19:32 -05:00
|
|
|
return true;
|
|
|
|
} } }
|
|
|
|
return false;
|
2019-05-16 03:51:36 -04:00
|
|
|
}
|