Created a nice utility library for doing animations, and used it in the ritual crafting UI.
This commit is contained in:
parent
0a40135f5d
commit
1aa6674e42
14 changed files with 213 additions and 150 deletions
103
animation.cpp
Normal file
103
animation.cpp
Normal file
|
@ -0,0 +1,103 @@
|
|||
#include "animation.hpp"
|
||||
|
||||
namespace components {
|
||||
void Animation::play() {
|
||||
if(!playing) {
|
||||
current = 0;
|
||||
subframe = 0.0f;
|
||||
playing = true;
|
||||
}
|
||||
}
|
||||
|
||||
float Animation::twitching() {
|
||||
switch(easing) {
|
||||
case ease::NONE:
|
||||
return 0.0;
|
||||
case ease::SINE:
|
||||
return ease::sine(float(frames) / subframe * ease_rate);
|
||||
case ease::OUT_CIRC:
|
||||
return ease::out_circ(ease::sine(float(frames) / subframe * ease_rate));
|
||||
case ease::OUT_BOUNCE:
|
||||
return ease::out_bounce(ease::sine(float(frames) / subframe * ease_rate));
|
||||
case ease::IN_OUT_BACK:
|
||||
return ease::in_out_back(ease::sine(float(frames) / subframe * ease_rate));
|
||||
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) {
|
||||
if(playing && current < frames) {
|
||||
float tick = twitching();
|
||||
scale_out.x = std::lerp(scale_out.x, scale_out.x + scale, tick);
|
||||
scale_out.y = std::lerp(scale_out.y, scale_out.y + scale, tick);
|
||||
|
||||
if(stationary) {
|
||||
pos_out.y = pos_out.y - (pos_out.y * scale_out.y - pos_out.y);
|
||||
}
|
||||
|
||||
if(!simple) {
|
||||
rect_out.position.x += current * frame_width;
|
||||
}
|
||||
|
||||
subframe += speed;
|
||||
current = int(subframe);
|
||||
} else if(!looped) {
|
||||
playing = false;
|
||||
current = frames - 1;
|
||||
subframe = float(frames - 1);
|
||||
|
||||
if(!simple) {
|
||||
rect_out.position.x += current * frame_width;
|
||||
}
|
||||
} else {
|
||||
playing = false;
|
||||
current = 0;
|
||||
subframe = 0.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
namespace animation {
|
||||
using namespace components;
|
||||
using namespace textures;
|
||||
|
||||
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)}};
|
||||
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);
|
||||
|
||||
return anim.playing;
|
||||
}
|
||||
|
||||
void init() {
|
||||
if(!initialized) {
|
||||
Config config("assets/animations.json");
|
||||
|
||||
for(auto& [name, data] : config.json().items()) {
|
||||
auto anim = components::convert<Animation>(data);
|
||||
MGR.animations.insert_or_assign(name, anim);
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
Animation load(std::string name) {
|
||||
return MGR.animations.at(name);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue