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) { void Animation::tween(sf::Vector2f& scale_out, sf::Vector2f& pos_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 >");
if(playing && current < frames) {
lerp(scale_out, pos_out); lerp(scale_out, pos_out);
if(!simple) { if(flipped) {
rect_out.position.x += current * frame_width; scale_out.x *= -1;
}
} }
bool Animation::next_frame() {
if(playing && current < frames) {
subframe += speed; subframe += speed;
current = looped ? int(subframe) % frames : int(subframe); current = looped ? int(subframe) % frames : int(subframe);
} else if(toggled) { } else if(toggled) {
playing = false; playing = false;
current = frames - 1; current = frames - 1;
subframe = float(frames - 1); subframe = float(frames - 1);
if(!simple) {
rect_out.position.x += current * frame_width;
}
} else { } else {
lerp(scale_out, pos_out);
playing = false; playing = false;
current = 0; current = 0;
subframe = 0.0f; subframe = 0.0f;
} }
if(flipped) { return playing;
scale_out.x *= -1;
} }
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) { bool Animation::apply(sf::Sprite& sprite, sf::Vector2f pos) {
@ -120,21 +139,23 @@ namespace components {
step(scale, pos, rect); step(scale, pos, rect);
sprite.setTextureRect(rect);
sprite.setPosition(pos); sprite.setPosition(pos);
if(scaled) { if(scaled) {
sprite.setScale(scale); sprite.setScale(scale);
} }
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};
sf::IntRect ignored{{0,0}, {int(size.x), int(size.y)}};
step(scale, pos, ignored); if(next_frame()) {
tween(scale, pos);
view_out.setCenter(pos); view_out.setCenter(pos);
// BUG: is this also needed in the other apply? // BUG: is this also needed in the other apply?
@ -143,6 +164,7 @@ namespace components {
} else { } else {
view_out.setSize(size); view_out.setSize(size);
} }
}
return playing; return playing;
} }

View file

@ -166,7 +166,11 @@ namespace components {
bool apply(sf::Sprite& target, sf::Vector2f pos); bool apply(sf::Sprite& target, sf::Vector2f pos);
bool apply(sf::View& view_out, sf::Vector2f pos, sf::Vector2f size); bool apply(sf::View& view_out, sf::Vector2f pos, sf::Vector2f size);
bool apply(sf::Transformable& target, sf::Vector2f pos);
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);
bool next_frame();
void play(); void play();
float twitching(); float twitching();

View file

@ -89,12 +89,11 @@ namespace scene {
for(auto& fixture : $fixtures) { for(auto& fixture : $fixtures) {
window.draw(*fixture.st.sprite); window.draw(*fixture.st.sprite);
window.draw(fixture.text);
} }
for(auto& actor : $actors) { for(auto& actor : $actors) {
window.draw(*actor.st.sprite); window.draw(*actor.st.sprite);
window.draw(actor.text); if(actor.anim.playing) window.draw(actor.text);
} }
@ -120,22 +119,18 @@ namespace scene {
for(auto& fixture : $fixtures) { for(auto& fixture : $fixtures) {
if(fixture.anim.playing) { if(fixture.anim.playing) {
fixture.anim.apply(*fixture.st.sprite, fixture.pos); fixture.anim.apply(*fixture.st.sprite, fixture.pos);
fixture.text.setPosition(fixture.pos);
fixture.text.setScale({fixture.scale_x, fixture.scale_y});
} }
} }
for(auto& actor : $actors) { for(auto& actor : $actors) {
if(actor.anim.playing) { if(actor.anim.playing) {
actor.anim.apply(*actor.st.sprite, actor.pos); actor.anim.apply(*actor.st.sprite, actor.pos);
actor.text.setPosition(actor.pos); actor.anim.apply(actor.text, actor.pos);
actor.text.setScale({actor.scale_x, actor.scale_y});
} }
} }
} }
sf::Vector2f Engine::position_sprite(textures::SpriteTexture& st, const std::string& cell_name, float scale_x, float scale_y, bool at_mid, float x_diff, float y_diff) { sf::Vector2f Engine::position_sprite(textures::SpriteTexture& st, const std::string& cell_name, float scale_x, float scale_y, bool at_mid, float x_diff, float y_diff) {
auto& cell = $ui.cell_for(cell_name); auto& cell = $ui.cell_for(cell_name);
float x = float(at_mid ? cell.mid_x : cell.x); float x = float(at_mid ? cell.mid_x : cell.x);
float y = float(at_mid ? cell.mid_y : cell.y); float y = float(at_mid ? cell.mid_y : cell.y);