Created a nice utility library for doing animations, and used it in the ritual crafting UI.

This commit is contained in:
Zed A. Shaw 2025-03-20 01:10:01 -04:00
parent 0a40135f5d
commit 1aa6674e42
14 changed files with 213 additions and 150 deletions

View file

@ -28,62 +28,4 @@ namespace components {
components::enroll<Animation>(component_map);
components::enroll<Sound>(component_map);
}
void Animation::play() {
if(!playing) {
current = 0;
subframe = 0.0f;
playing = true;
}
}
float Animation::twitching() {
switch(easing) {
case ease::NONE:
return 0.0;
case ease::SINE:
return ease::sine(float(frames) / subframe * ease_rate);
case ease::OUT_CIRC:
return ease::out_circ(ease::sine(float(frames) / subframe * ease_rate));
case ease::OUT_BOUNCE:
return ease::out_bounce(ease::sine(float(frames) / subframe * ease_rate));
case ease::IN_OUT_BACK:
return ease::in_out_back(ease::sine(float(frames) / subframe * ease_rate));
default:
dbc::sentinel(
fmt::format("Invalid easing {} given to animation",
int(easing)));
}
}
void Animation::step(sf::Vector2f& scale_out, sf::Vector2f& pos_out, sf::IntRect& rect_out) {
if(playing && current < frames) {
float tick = twitching();
scale_out.x = std::lerp(scale_out.x, scale_out.x + scale, tick);
scale_out.y = std::lerp(scale_out.y, scale_out.y + scale, tick);
if(stationary) {
pos_out.y = pos_out.y - (pos_out.y * scale_out.y - pos_out.y);
}
if(!simple) {
rect_out.position.x += current * texture_width;
}
subframe += speed;
current = int(subframe);
} else if(!looped) {
playing = false;
current = frames - 1;
subframe = float(frames - 1);
if(!simple) {
rect_out.position.x += current * texture_width;
}
} else {
playing = false;
current = 0;
subframe = 0.0f;
}
}
}