Refine how the raycaster.cpp uses animations so there's less back-and-forth on the intrect.
This commit is contained in:
parent
cded8a937e
commit
594be65f45
3 changed files with 18 additions and 5 deletions
|
|
@ -50,10 +50,23 @@ namespace animation {
|
||||||
dbc::check(sequence.current < $frame_rects.size(), "current frame past $frame_rects");
|
dbc::check(sequence.current < $frame_rects.size(), "current frame past $frame_rects");
|
||||||
// NOTE: pos is not updated yet
|
// NOTE: pos is not updated yet
|
||||||
auto& rect = $frame_rects.at(sequence.current);
|
auto& rect = $frame_rects.at(sequence.current);
|
||||||
fmt::println("setting texture rect: {},{}", rect.position.x, rect.position.y);
|
|
||||||
sprite.setTextureRect(rect);
|
sprite.setTextureRect(rect);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Alternative mostly used in raycaster.cpp that
|
||||||
|
* DOES NOT setTextureRect() but just points
|
||||||
|
* the rect_io at the correct frame, but leaves
|
||||||
|
* it's size and base position alone.
|
||||||
|
*/
|
||||||
|
void Animation::apply(sf::Sprite& sprite, sf::IntRect& rect_io) {
|
||||||
|
dbc::check(sequence.current < $frame_rects.size(), "current frame past $frame_rects");
|
||||||
|
auto& rect = $frame_rects.at(sequence.current);
|
||||||
|
rect_io.position.x += rect.position.x;
|
||||||
|
rect_io.position.y += rect.position.y;
|
||||||
|
}
|
||||||
|
|
||||||
void Animation::motion(sf::View& view_out, sf::Vector2f pos, sf::Vector2f size) {
|
void Animation::motion(sf::View& view_out, sf::Vector2f pos, sf::Vector2f size) {
|
||||||
dbc::check(size.x > 1.0f && size.y > 1.0f, "motion size must be above 1.0 since it's not a ratio");
|
dbc::check(size.x > 1.0f && size.y > 1.0f, "motion size must be above 1.0 since it's not a ratio");
|
||||||
dbc::check(transform.flipped == false, "transform must be false, has no effect on View");
|
dbc::check(transform.flipped == false, "transform must be false, has no effect on View");
|
||||||
|
|
@ -246,6 +259,7 @@ namespace animation {
|
||||||
|
|
||||||
Animation anim;
|
Animation anim;
|
||||||
animation::from_json(data[anim_name], anim);
|
animation::from_json(data[anim_name], anim);
|
||||||
|
anim.name = anim_name;
|
||||||
|
|
||||||
dbc::check(anim.forms.contains("idle"),
|
dbc::check(anim.forms.contains("idle"),
|
||||||
fmt::format("animation {} must have 'idle' form", anim_name));
|
fmt::format("animation {} must have 'idle' form", anim_name));
|
||||||
|
|
|
||||||
|
|
@ -117,6 +117,7 @@ namespace animation {
|
||||||
std::string form_name="idle";
|
std::string form_name="idle";
|
||||||
std::string sequence_name="";
|
std::string sequence_name="";
|
||||||
std::string transform_name="";
|
std::string transform_name="";
|
||||||
|
std::string name="";
|
||||||
|
|
||||||
std::vector<sf::IntRect> calc_frames();
|
std::vector<sf::IntRect> calc_frames();
|
||||||
void play();
|
void play();
|
||||||
|
|
@ -125,6 +126,7 @@ namespace animation {
|
||||||
bool has_form(const std::string& as_form);
|
bool has_form(const std::string& as_form);
|
||||||
void set_form(const std::string& form);
|
void set_form(const std::string& form);
|
||||||
void apply(sf::Sprite& sprite);
|
void apply(sf::Sprite& sprite);
|
||||||
|
void apply(sf::Sprite& sprite, sf::IntRect& rect_io);
|
||||||
void apply_effect(std::shared_ptr<sf::Shader> effect);
|
void apply_effect(std::shared_ptr<sf::Shader> effect);
|
||||||
void update();
|
void update();
|
||||||
void motion(sf::Transformable& sprite, sf::Vector2f pos, sf::Vector2f scale);
|
void motion(sf::Transformable& sprite, sf::Vector2f pos, sf::Vector2f scale);
|
||||||
|
|
|
||||||
|
|
@ -106,11 +106,8 @@ inline void step_animation(DinkyECS::World& world, DinkyECS::Entity entity, sf::
|
||||||
|
|
||||||
if(anim != nullptr && anim->playing) {
|
if(anim != nullptr && anim->playing) {
|
||||||
anim->update();
|
anim->update();
|
||||||
anim->apply(sprite);
|
anim->apply(sprite, in_texture);
|
||||||
anim->motion(sprite, position, scale);
|
anim->motion(sprite, position, scale);
|
||||||
|
|
||||||
auto& old_rect = sprite.getTextureRect();
|
|
||||||
in_texture.position.x += old_rect.position.x;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sprite.setScale(scale);
|
sprite.setScale(scale);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue