#include "animation.hpp" #include "rand.hpp" namespace components { void Animation::play() { if(!playing) { current = 0; subframe = 0.0f; playing = true; } } float Animation::twitching() { float tick = ease::sine(float(frames) / (subframe + 0.0001) * ease_rate); switch(easing) { case ease::NONE: return 0.0; case ease::SINE: return std::abs(std::sin(subframe * ease_rate)); case ease::OUT_CIRC: return ease::out_circ(tick); case ease::OUT_BOUNCE: return ease::sine(ease::out_bounce(tick)); case ease::IN_OUT_BACK: return ease::sine(ease::in_out_back(tick)); case ease::RANDOM: return Random::uniform_real(0.0001f, 1.0f); case ease::NORM_DIST: return Random::normal(0.5f, 0.1f); default: dbc::sentinel( fmt::format("Invalid easing {} given to animation", int(easing))); } } void Animation::step(sf::Vector2f& scale_out, sf::Vector2f& pos_out, sf::IntRect& rect_out) { dbc::check(rect_out.size.x > 0, "step given rect_out with size.x <= 0, must be >"); dbc::check(rect_out.size.y > 0, "step given rect_out with size.y <= 0, must be >"); if(playing && current < frames) { float tick = twitching(); if(stationary) { switch(motion) { case ease::SHAKE: { pos_out.x += std::lerp(min_x, max_x, tick); } break; case ease::BOUNCE: { pos_out.y -= std::lerp(min_y, max_y, tick); } break; case ease::RUSH: { scale_out.x = std::lerp(min_x, max_x, tick); scale_out.y = std::lerp(min_y, max_y, tick); pos_out.y = pos_out.y - (pos_out.y * scale_out.y - pos_out.y); } break; case ease::SQUEEZE: { scale_out.x *= std::lerp(min_x, max_x, tick); } break; case ease::SQUASH: { scale_out.y *= std::lerp(min_y, max_y, tick); } break; case ease::STRETCH: { scale_out.x = std::lerp(min_x, max_x, tick); } break; case ease::GROW: { scale_out.y = std::lerp(min_y, max_y, tick); } break; default: dbc::sentinel("Unknown animation.motion setting."); } } else { scale_out.x = std::lerp(scale_out.x * min_x, scale_out.x * max_x, tick); scale_out.y = std::lerp(scale_out.y * min_y, scale_out.y * max_y, tick); } if(!simple) { rect_out.position.x += current * frame_width; } subframe += speed; current = looped ? int(subframe) % frames : int(subframe); } else if(toggled) { playing = false; current = frames - 1; subframe = float(frames - 1); if(!simple) { rect_out.position.x += current * frame_width; } } else { scale_out.x = min_x; scale_out.y = min_y; playing = false; current = 0; subframe = 0.0f; } if(flipped) { scale_out.x *= -1; } } } namespace animation { using namespace components; using namespace textures; 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)); } void center(sf::Sprite& target, sf::Vector2f pos) { auto bounds = target.getLocalBounds(); target.setPosition({pos.x + bounds.size.x / 2, pos.y + bounds.size.y / 2}); target.setOrigin({bounds.size.x / 2, bounds.size.y / 2}); } void init() { if(!initialized) { auto animations = settings::get("animations"); auto config = settings::get("config"); auto& sprites = config["sprites"]; for(auto& [name, data] : animations.json().items()) { try { auto anim = components::convert(data); if(!sprites.contains(name)) { fmt::println("animation '{}' doesn't have sprite, spelled wrong in config.json?", name); } else { 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; } } bool has(const std::string& name) { return MGR.animations.contains(name); } Animation load(const std::string& name, const std::string& state) { dbc::check(initialized, "You forgot to initialize animation."); return MGR.animations.at(name + state); } void configure(DinkyECS::World& world, DinkyECS::Entity entity) { auto sprite = world.get_if(entity); if(sprite != nullptr && animation::has(sprite->name)) { world.set(entity, animation::load(sprite->name)); } } void step_animation(DinkyECS::World& world, DinkyECS::Entity entity, sf::Vector2f& scale_out, sf::Vector2f& pos_out, sf::IntRect& rect_out) { if(auto animation = world.get_if(entity)) { if(animation->playing) animation->step(scale_out, pos_out, rect_out); } } void animate_entity(DinkyECS::World &world, DinkyECS::Entity entity) { if(world.has(entity)) { auto& animation = world.get(entity); animation.play(); } } }