Entities in the world are now animated like before using the new animation system.

This commit is contained in:
Zed A. Shaw 2026-02-25 12:13:43 -05:00
parent 594be65f45
commit 779599f030
9 changed files with 85 additions and 79 deletions

View file

@ -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();

View file

@ -8,7 +8,7 @@
#include <SFML/System/Clock.hpp>
#include <SFML/System/Time.hpp>
#include <functional>
#include "ease2.hpp"
#include "easing.hpp"
#include <fmt/core.h>
#include "json_mods.hpp"
#include <source_location>

View file

@ -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": {

View file

@ -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},
};

View file

@ -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);
}

View file

@ -133,7 +133,7 @@ sources = [
'systems.cpp',
'textures.cpp',
'worldbuilder.cpp',
'ease2.cpp',
'easing.cpp',
]
executable('runtests', sources + [

View file

@ -101,15 +101,15 @@ void Raycaster::apply_sprite_effect(shared_ptr<sf::Shader> 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<animation::Animation>(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<animation::Animation>(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());
}

View file

@ -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();
}

View file

@ -68,7 +68,7 @@ namespace animator {
bool active();
void update();
void reload();
void check_update();
void check_changed();
void change_form(int direction);
};