Animations are refactored to let me spawn in an 'attack animation' but I think the data model is wrong. Rather than spawning in an animation every time I can probably just make one, reposition it, then tell it to play. I'll have to try it.

This commit is contained in:
Zed A. Shaw 2025-09-12 11:56:11 -04:00
parent 8384b11993
commit ad3e580495
15 changed files with 109 additions and 56 deletions

View file

@ -70,18 +70,17 @@ namespace animation {
static AnimationManager MGR;
static bool initialized = false;
bool apply(Animation& anim, SpriteTexture& target) {
auto size = target.texture->getSize();
anim.frame_width = int(size.x) / (unsigned int)anim.frames;
sf::IntRect rect{{0,0}, {anim.frame_width, int(size.y)}};
bool apply(Animation& anim, sf::Sprite& sprite) {
sf::IntRect rect{{0,0}, {anim.frame_width, anim.frame_height}};
sf::Vector2f scale{1.0, 1.0};
sf::Vector2f pos{0, 0};
anim.step(scale, pos, rect);
target.sprite->setTextureRect(rect);
target.sprite->setPosition(pos);
target.sprite->setScale(scale);
sprite.setTextureRect(rect);
sprite.setPosition(pos);
sprite.setScale(scale);
return anim.playing;
}
@ -99,11 +98,27 @@ namespace animation {
void init() {
if(!initialized) {
Config config("assets/animations.json");
Config animations("assets/animations.json");
Config config("assets/config.json");
auto& sprites = config["sprites"];
for(auto& [name, data] : config.json().items()) {
auto anim = components::convert<Animation>(data);
MGR.animations.insert_or_assign(name, anim);
for(auto& [name, data] : animations.json().items()) {
try {
auto anim = components::convert<Animation>(data);
auto& sprite_config = sprites[name];
anim.frame_width = sprite_config["frame_width"];
anim.frame_height = sprite_config["frame_height"];
dbc::check(anim.frame_width > 0 && anim.frame_height > 0,
fmt::format("invalid frame width/height for animation: {}",
name));
MGR.animations.insert_or_assign(name, anim);
} catch(...) {
dbc::log(fmt::format("error in sprite config: {}", name));
throw;
}
}
initialized = true;