Animations can work on Sprites,Transformables, and Views and the arena shows the hit/miss text so it follows the entity and hides when the animation stops.

This commit is contained in:
Zed A. Shaw 2025-12-24 14:18:41 -05:00
parent 0d23cf9537
commit f175a5d4aa
3 changed files with 53 additions and 32 deletions

View file

@ -81,37 +81,56 @@ namespace components {
}
}
void Animation::step(sf::Vector2f& scale_out, sf::Vector2f& pos_out, sf::IntRect& rect_out) {
dbc::check(rect_out.size.x > 0, "step given rect_out with size.x <= 0, must be >");
dbc::check(rect_out.size.y > 0, "step given rect_out with size.y <= 0, must be >");
void Animation::tween(sf::Vector2f& scale_out, sf::Vector2f& pos_out) {
lerp(scale_out, pos_out);
if(flipped) {
scale_out.x *= -1;
}
}
bool Animation::next_frame() {
if(playing && current < frames) {
lerp(scale_out, pos_out);
if(!simple) {
rect_out.position.x += current * frame_width;
}
subframe += speed;
current = looped ? int(subframe) % frames : int(subframe);
} else if(toggled) {
playing = false;
current = frames - 1;
subframe = float(frames - 1);
if(!simple) {
rect_out.position.x += current * frame_width;
}
} else {
lerp(scale_out, pos_out);
playing = false;
current = 0;
subframe = 0.0f;
}
if(flipped) {
scale_out.x *= -1;
return playing;
}
void Animation::step(sf::Vector2f& scale_out, sf::Vector2f& pos_out, sf::IntRect& rect_out) {
dbc::check(rect_out.size.x > 0, "step given rect_out with size.x <= 0, must be >");
dbc::check(rect_out.size.y > 0, "step given rect_out with size.y <= 0, must be >");
next_frame();
if(playing && !simple) {
rect_out.position.x = (current % frames) * frame_width;
}
tween(scale_out, pos_out);
}
bool Animation::apply(sf::Transformable& target, sf::Vector2f pos) {
sf::Vector2f scale{min_x, min_y};
if(next_frame()) {
tween(scale, pos);
target.setPosition(pos);
if(scaled) {
target.setScale(scale);
}
}
return playing;
}
bool Animation::apply(sf::Sprite& sprite, sf::Vector2f pos) {
@ -120,28 +139,31 @@ namespace components {
step(scale, pos, rect);
sprite.setTextureRect(rect);
sprite.setPosition(pos);
if(scaled) {
sprite.setScale(scale);
}
sprite.setTextureRect(rect);
return playing;
}
bool Animation::apply(sf::View& view_out, sf::Vector2f pos, sf::Vector2f size) {
sf::Vector2f scale{min_x, min_y};
sf::IntRect ignored{{0,0}, {int(size.x), int(size.y)}};
step(scale, pos, ignored);
view_out.setCenter(pos);
if(next_frame()) {
tween(scale, pos);
// BUG: is this also needed in the other apply?
if(scaled) {
view_out.setSize({size.x * scale.x, size.y * scale.y});
} else {
view_out.setSize(size);
view_out.setCenter(pos);
// BUG: is this also needed in the other apply?
if(scaled) {
view_out.setSize({size.x * scale.x, size.y * scale.y});
} else {
view_out.setSize(size);
}
}
return playing;