Animations can work on Sprites,Transformables, and Views and the arena shows the hit/miss text so it follows the entity and hides when the animation stops.
This commit is contained in:
parent
0d23cf9537
commit
f175a5d4aa
3 changed files with 53 additions and 32 deletions
|
|
@ -81,37 +81,56 @@ namespace components {
|
|||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
void Animation::tween(sf::Vector2f& scale_out, sf::Vector2f& pos_out) {
|
||||
lerp(scale_out, pos_out);
|
||||
|
||||
if(!simple) {
|
||||
rect_out.position.x += current * frame_width;
|
||||
if(flipped) {
|
||||
scale_out.x *= -1;
|
||||
}
|
||||
}
|
||||
|
||||
bool Animation::next_frame() {
|
||||
if(playing && current < frames) {
|
||||
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;
|
||||
return playing;
|
||||
}
|
||||
|
||||
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 >");
|
||||
next_frame();
|
||||
|
||||
if(playing && !simple) {
|
||||
rect_out.position.x = (current % frames) * frame_width;
|
||||
}
|
||||
|
||||
tween(scale_out, pos_out);
|
||||
}
|
||||
|
||||
bool Animation::apply(sf::Transformable& target, sf::Vector2f pos) {
|
||||
sf::Vector2f scale{min_x, min_y};
|
||||
|
||||
if(next_frame()) {
|
||||
tween(scale, pos);
|
||||
target.setPosition(pos);
|
||||
|
||||
if(scaled) {
|
||||
target.setScale(scale);
|
||||
}
|
||||
}
|
||||
|
||||
return playing;
|
||||
}
|
||||
|
||||
bool Animation::apply(sf::Sprite& sprite, sf::Vector2f pos) {
|
||||
|
|
@ -120,21 +139,23 @@ namespace components {
|
|||
|
||||
step(scale, pos, rect);
|
||||
|
||||
sprite.setTextureRect(rect);
|
||||
sprite.setPosition(pos);
|
||||
|
||||
if(scaled) {
|
||||
sprite.setScale(scale);
|
||||
}
|
||||
|
||||
sprite.setTextureRect(rect);
|
||||
|
||||
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);
|
||||
if(next_frame()) {
|
||||
tween(scale, pos);
|
||||
|
||||
view_out.setCenter(pos);
|
||||
|
||||
// BUG: is this also needed in the other apply?
|
||||
|
|
@ -143,6 +164,7 @@ namespace components {
|
|||
} else {
|
||||
view_out.setSize(size);
|
||||
}
|
||||
}
|
||||
|
||||
return playing;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -166,7 +166,11 @@ namespace components {
|
|||
|
||||
bool apply(sf::Sprite& target, sf::Vector2f pos);
|
||||
bool apply(sf::View& view_out, sf::Vector2f pos, sf::Vector2f size);
|
||||
bool apply(sf::Transformable& target, sf::Vector2f pos);
|
||||
|
||||
void lerp(sf::Vector2f& scale_out, sf::Vector2f& pos_out);
|
||||
void tween(sf::Vector2f& scale_out, sf::Vector2f& pos_out);
|
||||
bool next_frame();
|
||||
|
||||
void play();
|
||||
float twitching();
|
||||
|
|
|
|||
|
|
@ -89,12 +89,11 @@ namespace scene {
|
|||
|
||||
for(auto& fixture : $fixtures) {
|
||||
window.draw(*fixture.st.sprite);
|
||||
window.draw(fixture.text);
|
||||
}
|
||||
|
||||
for(auto& actor : $actors) {
|
||||
window.draw(*actor.st.sprite);
|
||||
window.draw(actor.text);
|
||||
if(actor.anim.playing) window.draw(actor.text);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -120,22 +119,18 @@ namespace scene {
|
|||
for(auto& fixture : $fixtures) {
|
||||
if(fixture.anim.playing) {
|
||||
fixture.anim.apply(*fixture.st.sprite, fixture.pos);
|
||||
fixture.text.setPosition(fixture.pos);
|
||||
fixture.text.setScale({fixture.scale_x, fixture.scale_y});
|
||||
}
|
||||
}
|
||||
|
||||
for(auto& actor : $actors) {
|
||||
if(actor.anim.playing) {
|
||||
actor.anim.apply(*actor.st.sprite, actor.pos);
|
||||
actor.text.setPosition(actor.pos);
|
||||
actor.text.setScale({actor.scale_x, actor.scale_y});
|
||||
actor.anim.apply(actor.text, actor.pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sf::Vector2f Engine::position_sprite(textures::SpriteTexture& st, const std::string& cell_name, float scale_x, float scale_y, bool at_mid, float x_diff, float y_diff) {
|
||||
|
||||
auto& cell = $ui.cell_for(cell_name);
|
||||
float x = float(at_mid ? cell.mid_x : cell.x);
|
||||
float y = float(at_mid ? cell.mid_y : cell.y);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue