227 lines
6.5 KiB
C++
227 lines
6.5 KiB
C++
#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::lerp(sf::Vector2f& scale_out, sf::Vector2f& pos_out) {
|
|
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;
|
|
case ease::SLIDE: {
|
|
pos_out.x += std::lerp(pos_out.x, pos_out.x + max_x, tick);
|
|
pos_out.y += std::lerp(pos_out.y, pos_out.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);
|
|
}
|
|
}
|
|
|
|
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) {
|
|
lerp(scale_out, pos_out);
|
|
|
|
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 {
|
|
lerp(scale_out, pos_out);
|
|
playing = false;
|
|
current = 0;
|
|
subframe = 0.0f;
|
|
}
|
|
|
|
if(flipped) {
|
|
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);
|
|
|
|
if(scaled) {
|
|
sprite.setScale(scale);
|
|
}
|
|
|
|
return playing;
|
|
}
|
|
|
|
bool Animation::apply(sf::View& view_out, sf::Vector2f pos, sf::Vector2f size) {
|
|
sf::Vector2f scale{min_x, min_y};
|
|
sf::IntRect ignored{{0,0}, {int(size.x), int(size.y)}};
|
|
|
|
step(scale, pos, ignored);
|
|
view_out.setCenter(pos);
|
|
|
|
// BUG: is this also needed in the other apply?
|
|
if(scaled) {
|
|
view_out.setSize({size.x * scale.x, size.y * scale.y});
|
|
} else {
|
|
view_out.setSize(size);
|
|
}
|
|
|
|
return playing;
|
|
}
|
|
}
|
|
|
|
namespace animation {
|
|
using namespace components;
|
|
using namespace textures;
|
|
|
|
static AnimationManager MGR;
|
|
static bool initialized = false;
|
|
|
|
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<Animation>(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<Sprite>(entity);
|
|
if(sprite != nullptr && animation::has(sprite->name)) {
|
|
world.set<Animation>(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<components::Animation>(entity)) {
|
|
if(animation->playing) animation->step(scale_out, pos_out, rect_out);
|
|
}
|
|
}
|
|
|
|
void animate_entity(DinkyECS::World &world, DinkyECS::Entity entity) {
|
|
if(world.has<Animation>(entity)) {
|
|
auto& animation = world.get<Animation>(entity);
|
|
animation.play();
|
|
}
|
|
}
|
|
}
|