Animations are now being pulled correctly but I need to add a timing concept to animations instead of ticks.

This commit is contained in:
Zed A. Shaw 2025-10-10 00:25:37 -04:00
parent 517de91a5b
commit c33f370572
10 changed files with 43 additions and 22 deletions

View file

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

View file

@ -11,7 +11,7 @@ namespace animation {
std::unordered_map<std::string, components::Animation> 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);

View file

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

View file

@ -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"}
]

View file

@ -1,5 +1,6 @@
#include "boss/fight.hpp"
#include "boss/system.hpp"
#include "animation.hpp"
namespace boss {
Fight::Fight(shared_ptr<World> 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<int>(data);
boss::System::combat(attack_id);
state(State::BOSS_TURN);
} break;
case TICK:
$ui.play_animations();
break;
default:
// skip it
break;

View file

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

View file

@ -5,10 +5,7 @@
#include <guecs/ui.hpp>
#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> 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();
};
}

View file

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

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)) {
if(!animation::apply($ritual_anim, *$ritual_ui.sprite, {0,0})) {
state(State::OPENED);
}
}

View file

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