A bit of refactor to put apply in Animation where it belongs.

This commit is contained in:
Zed A. Shaw 2025-11-01 11:13:12 -04:00
parent 102c8c36d5
commit d60e1af6df
10 changed files with 32 additions and 31 deletions

View file

@ -102,6 +102,23 @@ namespace components {
scale_out.x *= -1;
}
}
bool Animation::apply(sf::Sprite& sprite, sf::Vector2f pos) {
sf::IntRect rect{{0,0}, {frame_width, frame_height}};
sf::Vector2f scale{min_x, min_y};
step(scale, pos, rect);
sprite.setTextureRect(rect);
sprite.setPosition(pos);
// BUG: make this an option: apply_scale, apply_position and ranges for x y
if(scaled) {
sprite.setScale(scale);
}
return playing;
}
}
namespace animation {
@ -111,23 +128,6 @@ namespace animation {
static AnimationManager MGR;
static bool initialized = false;
bool apply(Animation& anim, sf::Sprite& sprite, sf::Vector2f pos) {
sf::IntRect rect{{0,0}, {anim.frame_width, anim.frame_height}};
sf::Vector2f scale{anim.min_x, anim.min_y};
anim.step(scale, pos, rect);
sprite.setTextureRect(rect);
sprite.setPosition(pos);
// BUG: make this an option: apply_scale, apply_position and ranges for x y
if(anim.scaled) {
sprite.setScale(scale);
}
return anim.playing;
}
void rotate(sf::Sprite& target, float degrees) {
target.rotate(sf::degrees(degrees));
}

View file

@ -11,7 +11,6 @@ namespace animation {
std::unordered_map<std::string, components::Animation> animations;
};
bool apply(components::Animation& anim, sf::Sprite& target, sf::Vector2f pos);
void rotate(sf::Sprite& target, float degrees);
void center(sf::Sprite& target, sf::Vector2f pos);

View file

@ -292,16 +292,16 @@
"test_zoom": {
"_type": "Animation",
"easing": 6,
"motion": 5,
"motion": 1,
"ease_rate": 0.5,
"min_x": 1.0,
"min_y": 1.0,
"max_x": 1.2,
"max_y": 1.2,
"min_x": -10.0,
"min_y": -10.0,
"max_x": 10.0,
"max_y": 10.0,
"simple": true,
"frames": 1,
"speed": 0.01,
"scaled": true,
"scaled": false,
"stationary": true,
"toggled": false,
"flipped": false,

View file

@ -92,7 +92,7 @@ namespace boss {
rect.size.x, rect.size.y);
sf::View zoom{pos,
{float(cell.w * 2), float(cell.h * 2)}};
{BOSS_VIEW_WIDTH/2, BOSS_VIEW_HEIGHT/2}};
if($zoom_anim.scaled) {
zoom.zoom(scale.x);

View file

@ -5,6 +5,7 @@
#include "point.hpp"
#include <SFML/Graphics/Rect.hpp>
#include <SFML/Graphics/Shader.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/System/Vector2.hpp>
#include <functional>
#include <optional>
@ -138,6 +139,7 @@ namespace components {
int frame_width = -1;
int frame_height = -1;
bool apply(sf::Sprite& target, sf::Vector2f pos);
void play();
float twitching();
void step(sf::Vector2f& scale_out, sf::Vector2f& pos_out, sf::IntRect& rect_out);

View file

@ -145,7 +145,7 @@ namespace gui {
}
void MainUI::render_hands() {
if(animation::apply($hand_anim, *$hand.sprite, {0,0})) {
if($hand_anim.apply(*$hand.sprite, {0,0})) {
$hand.sprite->setPosition({RAY_VIEW_X, RAY_VIEW_Y});
$window.draw(*$hand.sprite);
}

View file

@ -94,7 +94,7 @@ namespace gui {
void UI::OPENING(Event ev) {
if(ev == Event::TICK) {
if(!animation::apply($ritual_anim, *$ritual_ui.sprite, {0,0})) {
if(!$ritual_anim.apply(*$ritual_ui.sprite, {0,0})) {
state(State::OPENED);
}
}

View file

@ -100,13 +100,13 @@ namespace scene {
void Engine::play_animations() {
for(auto& fixture : $fixtures) {
if(fixture.anim.playing) {
animation::apply(fixture.anim, *fixture.st.sprite, fixture.pos);
fixture.anim.apply(*fixture.st.sprite, fixture.pos);
}
}
for(auto& actor : $actors) {
if(actor.anim.playing) {
animation::apply(actor.anim, *actor.st.sprite, actor.pos);
actor.anim.apply(*actor.st.sprite, actor.pos);
}
}
}

View file

@ -42,7 +42,7 @@ TEST_CASE("animation utility API", "[animation]") {
anim.play();
while(animation::apply(anim, *blanket.sprite, {0,0})) {
while(anim.apply(*blanket.sprite, {0,0})) {
fmt::println("animation: {}", anim.subframe);
}
}

View file

@ -32,7 +32,7 @@ struct AnimationState {
}
void apply(textures::SpriteTexture& st) {
animation::apply(anim, *st.sprite, pos);
anim.apply(*st.sprite, pos);
}
};