From c33f3705724de408385a55becc5611c0b0677800 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Fri, 10 Oct 2025 00:25:37 -0400 Subject: [PATCH] Animations are now being pulled correctly but I need to add a timing concept to animations instead of ticks. --- animation.cpp | 7 ++++--- animation.hpp | 2 +- assets/animations.json | 10 ++++++++++ assets/bosses.json | 9 --------- boss/fight.cpp | 8 ++++++++ boss/ui.cpp | 15 +++++++++++++-- boss/ui.hpp | 8 ++++---- gui/main_ui.cpp | 2 +- gui/ritual_ui.cpp | 2 +- tests/animation.cpp | 2 +- 10 files changed, 43 insertions(+), 22 deletions(-) diff --git a/animation.cpp b/animation.cpp index c901119..ea077fe 100644 --- a/animation.cpp +++ b/animation.cpp @@ -31,6 +31,9 @@ namespace components { } void Animation::step(sf::Vector2f& scale_out, sf::Vector2f& pos_out, sf::IntRect& rect_out) { + dbc::check(rect_out.size.x > 0, "invalid frame width in animation"); + dbc::check(rect_out.size.y > 0, "invalid frame height in animation"); + if(playing && current < frames) { float tick = twitching(); scale_out.x = std::lerp(scale_out.x, scale_out.x + scale, tick); @@ -70,10 +73,9 @@ namespace animation { static AnimationManager MGR; static bool initialized = false; - bool apply(Animation& anim, sf::Sprite& sprite) { + 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.scale, anim.scale}; - sf::Vector2f pos{0, 0}; anim.step(scale, pos, rect); @@ -156,5 +158,4 @@ namespace animation { animation.play(); } } - } diff --git a/animation.hpp b/animation.hpp index 8486e29..f539870 100644 --- a/animation.hpp +++ b/animation.hpp @@ -11,7 +11,7 @@ namespace animation { std::unordered_map animations; }; - bool apply(components::Animation& anim, sf::Sprite& target); + 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); diff --git a/assets/animations.json b/assets/animations.json index 16725d3..9540dd0 100644 --- a/assets/animations.json +++ b/assets/animations.json @@ -108,5 +108,15 @@ "frames": 10, "speed": 1.0, "stationary": false + }, + "test_boss": { + "_type": "Animation", + "easing": 3, + "ease_rate": 0.5, + "scale": 0.4, + "simple": true, + "frames": 1, + "speed": 0.02, + "stationary": true } } diff --git a/assets/bosses.json b/assets/bosses.json index 30bf829..ade597e 100644 --- a/assets/bosses.json +++ b/assets/bosses.json @@ -18,15 +18,6 @@ } }, {"_type": "Combat", "hp": 20, "max_hp": 20, "damage": 20, "dead": false}, - {"_type": "Animation", - "easing": 3, - "ease_rate": 0.2, - "simple": false, - "frames": 2, - "speed": 0.02, - "scale": 0.2, - "stationary": false - }, {"_type": "Sprite", "name": "test_boss", "width": 720, "height": 720, "scale": 0.8, "stationary": false}, {"_type": "Sound", "attack": "Marmot_Scream_1", "death": "Creature_Death_1"} ] diff --git a/boss/fight.cpp b/boss/fight.cpp index 39a7543..654982c 100644 --- a/boss/fight.cpp +++ b/boss/fight.cpp @@ -1,5 +1,6 @@ #include "boss/fight.hpp" #include "boss/system.hpp" +#include "animation.hpp" namespace boss { Fight::Fight(shared_ptr world, Entity boss_id) : @@ -87,6 +88,9 @@ namespace boss { boss::System::combat(attack_id); state(State::PLAYER_TURN); } break; + case TICK: + $ui.play_animations(); + break; default: break; // skip it @@ -104,10 +108,14 @@ namespace boss { case ATTACK: { $ui.status(L"BOSS TURN"); $ui.move_boss(run % 10 < 5 ? "boss1" : "boss3"); + $ui.$boss_anim.play(); int attack_id = std::any_cast(data); boss::System::combat(attack_id); state(State::BOSS_TURN); } break; + case TICK: + $ui.play_animations(); + break; default: // skip it break; diff --git a/boss/ui.cpp b/boss/ui.cpp index faa75d9..892ec69 100644 --- a/boss/ui.cpp +++ b/boss/ui.cpp @@ -1,6 +1,7 @@ #include "boss/ui.hpp" #include "constants.hpp" #include "components.hpp" +#include "animation.hpp" namespace boss { using namespace guecs; @@ -18,6 +19,9 @@ namespace boss { $floor_sprite = textures::get_sprite(*$scene.floor); $player_sprite = textures::get_sprite($scene.player["sprite"]); + + dbc::check(animation::has(sprite.name), "add boss animation to animations.json"); + $boss_anim = animation::load(sprite.name); } void UI::init() { @@ -92,12 +96,19 @@ namespace boss { } void UI::move_boss(const std::string& cell_name) { - dbc::log(cell_name); position_sprite($boss_sprite, cell_name, $scene.boss["scale"], $scene.boss["mid_cell"]); + + auto& cell = $arena.cell_for(cell_name); + $boss_pos = {float(cell.mid_x), float(cell.mid_y)}; } void UI::move_player(const std::string& cell_name) { - dbc::log(cell_name); position_sprite($player_sprite, cell_name, $scene.player["scale"], $scene.player["mid_cell"]); } + + void UI::play_animations() { + if($boss_anim.playing) { + animation::apply($boss_anim, *$boss_sprite.sprite, $boss_pos); + } + } } diff --git a/boss/ui.hpp b/boss/ui.hpp index 9e264b6..f701f58 100644 --- a/boss/ui.hpp +++ b/boss/ui.hpp @@ -5,10 +5,7 @@ #include #include "textures.hpp" #include "gui/combat_ui.hpp" - -namespace components { - struct BossFight; -} +#include "components.hpp" namespace boss { using std::shared_ptr; @@ -25,6 +22,8 @@ namespace boss { SpriteTexture $floor_sprite; guecs::UI $arena; guecs::UI $actions; + components::Animation $boss_anim; + sf::Vector2f $boss_pos; UI(shared_ptr world, Entity boss_id); @@ -35,5 +34,6 @@ namespace boss { void status(const std::wstring& msg); void move_boss(const std::string& cell_name); void move_player(const std::string& cell_name); + void play_animations(); }; } diff --git a/gui/main_ui.cpp b/gui/main_ui.cpp index 5968e48..56844aa 100644 --- a/gui/main_ui.cpp +++ b/gui/main_ui.cpp @@ -145,7 +145,7 @@ namespace gui { } void MainUI::render_hands() { - if(animation::apply($hand_anim, *$hand.sprite)) { + if(animation::apply($hand_anim, *$hand.sprite, {0,0})) { $hand.sprite->setPosition({RAY_VIEW_X, RAY_VIEW_Y}); $window.draw(*$hand.sprite); } diff --git a/gui/ritual_ui.cpp b/gui/ritual_ui.cpp index 76e283d..a446305 100644 --- a/gui/ritual_ui.cpp +++ b/gui/ritual_ui.cpp @@ -94,7 +94,7 @@ namespace gui { void UI::OPENING(Event ev) { if(ev == Event::TICK) { - if(!animation::apply($ritual_anim, *$ritual_ui.sprite)) { + if(!animation::apply($ritual_anim, *$ritual_ui.sprite, {0,0})) { state(State::OPENED); } } diff --git a/tests/animation.cpp b/tests/animation.cpp index c8f208f..ad6f6a4 100644 --- a/tests/animation.cpp +++ b/tests/animation.cpp @@ -42,7 +42,7 @@ TEST_CASE("animation utility API", "[animation]") { anim.play(); - while(animation::apply(anim, *blanket.sprite)) { + while(animation::apply(anim, *blanket.sprite, {0,0})) { fmt::println("animation: {}", anim.subframe); } }