Animations now work better with looped/toggled/normal animations working.

This commit is contained in:
Zed A. Shaw 2025-12-28 23:46:11 -05:00
parent 8208d146de
commit c71566048e
2 changed files with 32 additions and 19 deletions

View file

@ -89,21 +89,30 @@ namespace components {
} }
} }
bool Animation::next_frame() { void Animation::next_frame() {
if(playing && current < frames) { subframe += speed;
subframe += speed;
current = looped ? int(subframe) % frames : int(subframe); if(looped) {
} else if(toggled) { // it never ends so always do this
playing = false; current = int(subframe) % frames;
return;
}
current = int(subframe);
playing = current < frames;
if(playing) {
return;
}
// not looped handle not_playing/playing/toggled
if(toggled) {
current = frames - 1; current = frames - 1;
subframe = float(frames - 1); subframe = float(frames - 1);
} else { } else {
playing = false;
current = 0; current = 0;
subframe = 0.0f; subframe = 0.0f;
} }
return playing;
} }
void Animation::step(sf::Vector2f& scale_out, sf::Vector2f& pos_out, sf::IntRect& rect_out) { void Animation::step(sf::Vector2f& scale_out, sf::Vector2f& pos_out, sf::IntRect& rect_out) {
@ -111,7 +120,7 @@ namespace components {
dbc::check(rect_out.size.y > 0, "step given rect_out with size.y <= 0, must be >"); dbc::check(rect_out.size.y > 0, "step given rect_out with size.y <= 0, must be >");
next_frame(); next_frame();
if(playing && !simple) { if(!simple) {
rect_out.position.x = (current % frames) * frame_width; rect_out.position.x = (current % frames) * frame_width;
} }
@ -121,7 +130,8 @@ namespace components {
bool Animation::apply(sf::Transformable& target, sf::Vector2f pos) { bool Animation::apply(sf::Transformable& target, sf::Vector2f pos) {
sf::Vector2f scale{min_x, min_y}; sf::Vector2f scale{min_x, min_y};
if(next_frame()) { if(playing) {
next_frame();
tween(scale, pos); tween(scale, pos);
target.setPosition(pos); target.setPosition(pos);
@ -137,23 +147,26 @@ namespace components {
sf::IntRect rect{{0,0}, {frame_width, frame_height}}; sf::IntRect rect{{0,0}, {frame_width, frame_height}};
sf::Vector2f scale{min_x, min_y}; sf::Vector2f scale{min_x, min_y};
step(scale, pos, rect); if(playing) {
step(scale, pos, rect);
sprite.setPosition(pos); sprite.setPosition(pos);
if(scaled) { if(scaled) {
sprite.setScale(scale); sprite.setScale(scale);
}
sprite.setTextureRect(rect);
} }
sprite.setTextureRect(rect);
return playing; return playing;
} }
bool Animation::apply(sf::View& view_out, sf::Vector2f pos, sf::Vector2f size) { bool Animation::apply(sf::View& view_out, sf::Vector2f pos, sf::Vector2f size) {
sf::Vector2f scale{min_x, min_y}; sf::Vector2f scale{min_x, min_y};
if(next_frame()) { if(playing) {
next_frame();
tween(scale, pos); tween(scale, pos);
view_out.setCenter(pos); view_out.setCenter(pos);

View file

@ -170,7 +170,7 @@ namespace components {
void lerp(sf::Vector2f& scale_out, sf::Vector2f& pos_out); void lerp(sf::Vector2f& scale_out, sf::Vector2f& pos_out);
void tween(sf::Vector2f& scale_out, sf::Vector2f& pos_out); void tween(sf::Vector2f& scale_out, sf::Vector2f& pos_out);
bool next_frame(); void next_frame();
void play(); void play();
float twitching(); float twitching();