Updates frame timeout when changing speed scale

This commit is contained in:
groud 2018-05-13 23:29:06 +02:00
parent dc7060973c
commit d7f25f7adb
2 changed files with 19 additions and 11 deletions

View file

@ -361,7 +361,7 @@ void AnimatedSprite::_notification(int p_what) {
if (timeout <= 0) { if (timeout <= 0) {
timeout = 1.0 / speed; timeout = _get_frame_duration();
int fc = frames->get_frame_count(animation); int fc = frames->get_frame_count(animation);
if (frame >= fc - 1) { if (frame >= fc - 1) {
@ -483,7 +483,13 @@ int AnimatedSprite::get_frame() const {
void AnimatedSprite::set_speed_scale(float p_speed_scale) { void AnimatedSprite::set_speed_scale(float p_speed_scale) {
float elapsed = _get_frame_duration() - timeout;
speed_scale = MAX(p_speed_scale, 0.0f); speed_scale = MAX(p_speed_scale, 0.0f);
// We adapt the timeout so that the animation speed adapts as soon as the speed scale is changed
_reset_timeout();
timeout -= elapsed;
} }
float AnimatedSprite::get_speed_scale() const { float AnimatedSprite::get_speed_scale() const {
@ -574,21 +580,22 @@ bool AnimatedSprite::is_playing() const {
return playing; return playing;
} }
float AnimatedSprite::_get_frame_duration() {
if (frames.is_valid() && frames->has_animation(animation)) {
float speed = frames->get_animation_speed(animation) * speed_scale;
if (speed > 0) {
return 1.0 / speed;
}
}
return 0.0;
}
void AnimatedSprite::_reset_timeout() { void AnimatedSprite::_reset_timeout() {
if (!playing) if (!playing)
return; return;
if (frames.is_valid() && frames->has_animation(animation)) { timeout = _get_frame_duration();
float speed = frames->get_animation_speed(animation) * speed_scale;
if (speed > 0) {
timeout = 1.0 / speed;
} else {
timeout = 0;
}
} else {
timeout = 0;
}
} }
void AnimatedSprite::set_animation(const StringName &p_animation) { void AnimatedSprite::set_animation(const StringName &p_animation) {

View file

@ -141,6 +141,7 @@ class AnimatedSprite : public Node2D {
void _res_changed(); void _res_changed();
float _get_frame_duration();
void _reset_timeout(); void _reset_timeout();
void _set_playing(bool p_playing); void _set_playing(bool p_playing);
bool _is_playing() const; bool _is_playing() const;