diff --git a/animation.cpp b/animation.cpp index 6eeeda0..31330cc 100644 --- a/animation.cpp +++ b/animation.cpp @@ -67,6 +67,22 @@ namespace animation { rect_io.position.y += rect.position.y; } + void Animation::motion(sf::Transformable& sprite, sf::Vector2f pos, sf::Vector2f scale) { + sequence.INVARIANT(); + + transform.apply(sequence, pos, scale); + + if(transform.flipped) { + scale.x *= -1; + } + + sprite.setPosition(pos); + + if(transform.scaled) { + sprite.setScale(scale); + } + } + void Animation::motion(sf::View& view_out, sf::Vector2f pos, sf::Vector2f size) { dbc::check(size.x > 1.0f && size.y > 1.0f, "motion size must be above 1.0 since it's not a ratio"); dbc::check(transform.flipped == false, "transform must be false, has no effect on View"); @@ -141,22 +157,6 @@ namespace animation { if(frame_change && onFrame != nullptr) onFrame(); } - void Animation::motion(sf::Transformable& sprite, sf::Vector2f pos, sf::Vector2f scale) { - sequence.INVARIANT(); - - transform.apply(sequence, pos, scale); - - if(transform.flipped) { - scale.x *= -1; - } - - sprite.setPosition(pos); - - if(transform.scaled) { - sprite.setScale(scale); - } - } - void Timer::start() { clock.start(); prev_time = clock.getElapsedTime().asSeconds(); diff --git a/animation.hpp b/animation.hpp index 2f313bf..f01de8f 100644 --- a/animation.hpp +++ b/animation.hpp @@ -8,7 +8,7 @@ #include #include #include -#include "ease2.hpp" +#include "easing.hpp" #include #include "json_mods.hpp" #include diff --git a/assets/animation.json b/assets/animation.json index 8f91b49..94c741a 100644 --- a/assets/animation.json +++ b/assets/animation.json @@ -19,7 +19,7 @@ "flipped": false, "scaled": true, "toggled": false, - "looped": true, + "looped": false, "relative": false, "easing": "in_out_back", "motion": "move_rush" @@ -205,7 +205,7 @@ "frame_height": 256 }, "sequences": { - "idle": {"frames": [0,1,2], "durations": [5,5,5] } + "idle": {"frames": [0,1,2,1,0], "durations": [5,5,5,5,5] } }, "transforms": { "basic": { @@ -214,10 +214,10 @@ "max_x": 1.0, "max_y": 1.0, "flipped": false, - "scaled": false, + "scaled": true, "toggled": false, "looped": false, - "relative": false, + "relative": true, "easing": "none", "motion": "move_none" } @@ -280,7 +280,7 @@ "max_x": 1.0, "max_y": 1.0, "flipped": false, - "scaled": false, + "scaled": true, "toggled": false, "looped": false, "relative": false, @@ -304,21 +304,21 @@ "frame_height": 256 }, "sequences": { - "idle": {"frames": [0], "durations": [5] } + "idle": {"frames": [0], "durations": [50] } }, "transforms": { "basic": { - "min_x": 0.9, - "min_y": 0.9, - "max_x": 1.0, - "max_y": 1.0, + "min_x": 1.0, + "min_y": 1.0, + "max_x": 1.22, + "max_y": 1.22, "flipped": false, "scaled": true, "toggled": false, - "looped": true, + "looped": false, "relative": false, - "easing": "sine", - "motion": "move_rush" + "easing": "in_out_back", + "motion": "scale_both" } }, "forms": { @@ -341,17 +341,17 @@ }, "transforms": { "basic": { - "min_x": 0.9, - "min_y": 0.9, - "max_x": 1.0, - "max_y": 1.0, + "min_x": 1.0, + "min_y": 1.0, + "max_x": 1.22, + "max_y": 1.22, "flipped": false, "scaled": true, "toggled": false, - "looped": true, + "looped": false, "relative": false, "easing": "sine", - "motion": "move_rush" + "motion": "scale_both" } }, "forms": { @@ -370,21 +370,21 @@ "frame_height": 256 }, "sequences": { - "idle": {"frames": [0], "durations": [5] } + "idle": {"frames": [0], "durations": [50] } }, "transforms": { "basic": { - "min_x": 0.9, - "min_y": 0.9, - "max_x": 1.0, - "max_y": 1.0, + "min_x": 1.0, + "min_y": 1.0, + "max_x": 1.22, + "max_y": 1.22, "flipped": false, "scaled": true, "toggled": false, - "looped": true, + "looped": false, "relative": false, "easing": "sine", - "motion": "move_rush" + "motion": "scale_both" } }, "forms": { @@ -403,21 +403,21 @@ "frame_height": 256 }, "sequences": { - "idle": {"frames": [0], "durations": [5] } + "idle": {"frames": [0], "durations": [50] } }, "transforms": { "basic": { - "min_x": 0.9, - "min_y": 0.9, - "max_x": 1.0, - "max_y": 1.0, + "min_x": 1.0, + "min_y": 1.0, + "max_x": 1.22, + "max_y": 1.22, "flipped": false, "scaled": true, "toggled": false, - "looped": true, + "looped": false, "relative": false, "easing": "sine", - "motion": "move_rush" + "motion": "scale_both" } }, "forms": { @@ -436,21 +436,21 @@ "frame_height": 256 }, "sequences": { - "idle": {"frames": [0], "durations": [5] } + "idle": {"frames": [0], "durations": [50] } }, "transforms": { "basic": { - "min_x": 0.9, - "min_y": 0.9, - "max_x": 1.0, - "max_y": 1.0, + "min_x": 1.0, + "min_y": 1.0, + "max_x": 1.11, + "max_y": 1.11, "flipped": false, "scaled": true, "toggled": false, - "looped": true, + "looped": false, "relative": false, - "easing": "sine", - "motion": "move_rush" + "easing": "in_out_back", + "motion": "scale_both" } }, "forms": { @@ -469,21 +469,21 @@ "frame_height": 256 }, "sequences": { - "idle": {"frames": [0], "durations": [5] } + "idle": {"frames": [0], "durations": [50] } }, "transforms": { "basic": { "min_x": 1.0, "min_y": 1.0, - "max_x": 1.0, - "max_y": 1.0, + "max_x": 1.22, + "max_y": 1.22, "flipped": false, - "scaled": false, + "scaled": true, "toggled": false, "looped": false, "relative": false, "easing": "none", - "motion": "move_none" + "motion": "scale_both" } }, "forms": { diff --git a/ease2.cpp b/easing.cpp similarity index 97% rename from ease2.cpp rename to easing.cpp index 5741bfd..9278875 100644 --- a/ease2.cpp +++ b/easing.cpp @@ -98,7 +98,7 @@ namespace ease2 { void move_none(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative) { } - void scale_only(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative) { + void scale_both(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative) { scale_out.x = std::lerp(scale_out.x * tr.min_x, scale_out.x * tr.max_x, tick) + (scale_out.x * relative); scale_out.y = std::lerp(scale_out.y * tr.min_y, scale_out.y * tr.max_y, tick) + (scale_out.y * relative); } @@ -123,7 +123,7 @@ namespace ease2 { {"scale_grow", scale_grow}, {"move_slide", move_slide}, {"move_none", move_none}, - {"scale_only", scale_only}, + {"scale_both", scale_both}, {"move_shake", move_shake}, }; diff --git a/ease2.hpp b/easing.hpp similarity index 96% rename from ease2.hpp rename to easing.hpp index c0348a8..20544f4 100644 --- a/ease2.hpp +++ b/easing.hpp @@ -27,6 +27,6 @@ namespace ease2 { void scale_grow(animation::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative); void move_slide(animation::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative); void move_none(animation::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative); - void scale_only(animation::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative); + void scale_both(animation::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative); void move_shake(animation::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative); } diff --git a/meson.build b/meson.build index 5ca416e..27d5cfe 100644 --- a/meson.build +++ b/meson.build @@ -133,7 +133,7 @@ sources = [ 'systems.cpp', 'textures.cpp', 'worldbuilder.cpp', - 'ease2.cpp', + 'easing.cpp', ] executable('runtests', sources + [ diff --git a/raycaster.cpp b/raycaster.cpp index 27dd321..6a70158 100644 --- a/raycaster.cpp +++ b/raycaster.cpp @@ -101,15 +101,15 @@ void Raycaster::apply_sprite_effect(shared_ptr effect, float width, effect->setUniform("u_resolution", u_resolution); } -inline void step_animation(DinkyECS::World& world, DinkyECS::Entity entity, sf::Sprite& sprite, sf::Vector2f& position, sf::Vector2f& scale, sf::IntRect& in_texture, sf::Vector2f& origin) { - auto anim = world.get_if(entity); - - if(anim != nullptr && anim->playing) { - anim->update(); - anim->apply(sprite, in_texture); - anim->motion(sprite, position, scale); - } +inline void step_animation(animation::Animation& anim, sf::Sprite& sprite, sf::Vector2f& position, sf::Vector2f& scale, sf::IntRect& in_texture, sf::Vector2f& origin) { + anim.update(); + anim.apply(sprite, in_texture); + anim.motion(sprite, position, scale); + sprite.setOrigin(origin); + sprite.setTextureRect(in_texture); +} +inline void set_scale_position(sf::Sprite& sprite, sf::Vector2f& position, sf::Vector2f& scale, sf::IntRect& in_texture, sf::Vector2f& origin) { sprite.setScale(scale); sprite.setPosition(position); sprite.setOrigin(origin); @@ -218,7 +218,13 @@ void Raycaster::sprite_casting(sf::RenderTarget &target) { effect->setUniform("darkness", level); } - step_animation(*world, rec.entity, *sf_sprite, position, scale, in_texture, origin); + auto anim = world->get_if(rec.entity); + + if(anim != nullptr && anim->playing) { + step_animation(*anim, *sf_sprite, position, scale, in_texture, origin); + } else { + set_scale_position(*sf_sprite, position, scale, in_texture, origin); + } target.draw(*sf_sprite, effect.get()); } diff --git a/tools/animator.cpp b/tools/animator.cpp index 781a74c..83ec4b0 100644 --- a/tools/animator.cpp +++ b/tools/animator.cpp @@ -121,7 +121,7 @@ namespace animator { } } - void FSM::check_update() { + void FSM::check_changed() { if($timer.getElapsedTime().toDuration() > 500ms) { try { auto mod_time = std::filesystem::last_write_time("assets/animation.json"); @@ -339,7 +339,7 @@ int main(int argc, char* argv[]) { while(main.active()) { main.update(); main.render(); - main.check_update(); + main.check_changed(); main.handle_keyboard_mouse(); } diff --git a/tools/animator.hpp b/tools/animator.hpp index 2186f2c..c6c06c3 100644 --- a/tools/animator.hpp +++ b/tools/animator.hpp @@ -68,7 +68,7 @@ namespace animator { bool active(); void update(); void reload(); - void check_update(); + void check_changed(); void change_form(int direction); };