diff --git a/animation.cpp b/animation.cpp index 49ffb3a..c6e6d2c 100644 --- a/animation.cpp +++ b/animation.cpp @@ -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; diff --git a/components.hpp b/components.hpp index f008cdf..bcde96b 100644 --- a/components.hpp +++ b/components.hpp @@ -166,7 +166,11 @@ namespace components { bool apply(sf::Sprite& target, sf::Vector2f pos); 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 tween(sf::Vector2f& scale_out, sf::Vector2f& pos_out); + bool next_frame(); void play(); float twitching(); diff --git a/scene.cpp b/scene.cpp index bb0911a..09fc2f1 100644 --- a/scene.cpp +++ b/scene.cpp @@ -89,12 +89,11 @@ namespace scene { for(auto& fixture : $fixtures) { window.draw(*fixture.st.sprite); - window.draw(fixture.text); } for(auto& actor : $actors) { 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) { if(fixture.anim.playing) { 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) { if(actor.anim.playing) { actor.anim.apply(*actor.st.sprite, actor.pos); - actor.text.setPosition(actor.pos); - actor.text.setScale({actor.scale_x, actor.scale_y}); + actor.anim.apply(actor.text, actor.pos); } } } 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); float x = float(at_mid ? cell.mid_x : cell.x); float y = float(at_mid ? cell.mid_y : cell.y);