Fix some things with the animation class.

The user wouldn't be able to set a custom scale to their sprite if they used the SSMA component due to it resetting the scale when the sprite was flipped.
This commit is contained in:
SeanOMik 2020-07-06 15:05:21 -05:00
parent f237cf9eff
commit 06c17658e6
No known key found for this signature in database
GPG Key ID: FA4D55AC05268A88
4 changed files with 15 additions and 23 deletions

View File

@ -19,18 +19,20 @@ class PlayerEntity : public simpleengine::Entity {
private: private:
sf::Sprite sprite; sf::Sprite sprite;
sf::Texture texture; sf::Texture texture;
float movement_speed = 150; float movement_speed = 130;
sf::Vector2u window_size; sf::Vector2u window_size;
std::unique_ptr<simpleengine::SideScrollerMovementAnimationComponent> move_anim_component; std::unique_ptr<simpleengine::SideScrollerMovementAnimationComponent> move_anim_component;
public: public:
explicit PlayerEntity(sf::Vector2u window_size) : window_size(window_size) { explicit PlayerEntity(sf::Vector2u window_size) : window_size(window_size) {
texture.loadFromFile("player_sheet.png"); texture.loadFromFile("player_sheet.png");
texture.setSmooth(true);
sprite.setTexture(texture); sprite.setTexture(texture);
sprite.setScale(.7, .7);
move_anim_component = std::make_unique<simpleengine::SideScrollerMovementAnimationComponent>(*this, sprite, move_anim_component = std::make_unique<simpleengine::SideScrollerMovementAnimationComponent>(*this, sprite,
texture, movement_speed, 5, 1.1); texture, movement_speed, 5, 1.1);
move_anim_component->SetAnimation(simpleengine::MovementAnimationType::WALK_LEFT, 9, 0, 8, move_anim_component->SetAnimation(simpleengine::MovementAnimationType::WALK_LEFT, 8, 0, 8,
9, 8, 128, 128); 9, 8, 128, 128);
move_anim_component->SetAnimation(simpleengine::MovementAnimationType::IDLE_LEFT, 20, 0, 0, move_anim_component->SetAnimation(simpleengine::MovementAnimationType::IDLE_LEFT, 20, 0, 0,
6, 0, 128, 128); 6, 0, 128, 128);

View File

@ -36,6 +36,7 @@ namespace simpleengine {
sf::IntRect start_rect; sf::IntRect start_rect;
sf::IntRect current_rect; sf::IntRect current_rect;
sf::IntRect end_rect; sf::IntRect end_rect;
sf::Vector2f start_scale;
bool horizontally_flipped = false; bool horizontally_flipped = false;
bool vertically_flipped = false; bool vertically_flipped = false;

View File

@ -16,9 +16,10 @@ simpleengine::Animation::Animation(sf::Sprite &sprite, sf::Texture &texture_shee
// Set start of sprite. // Set start of sprite.
this->sprite.setTexture(texture_sheet, true); this->sprite.setTexture(texture_sheet, true);
this->sprite.setTextureRect(start_rect); this->sprite.setTextureRect(start_rect);
this->start_scale = sprite.getScale();
this->current_rect = start_rect; this->current_rect = start_rect;
sprite.setOrigin({ static_cast<float>(sprite.getLocalBounds().width * .5), 0 }); sprite.setOrigin({ static_cast<float>(sprite.getLocalBounds().width * 0.5), 0 });
} }
void simpleengine::Animation::Update(const float& delta_time) { void simpleengine::Animation::Update(const float& delta_time) {
@ -45,23 +46,15 @@ void simpleengine::Animation::Reset() {
} }
void simpleengine::Animation::FlipHorizontally() { void simpleengine::Animation::FlipHorizontally() {
if (!horizontally_flipped) { sf::Vector2f scale = sprite.getScale();
sprite.setScale(-1, 1); sprite.setScale(-scale.x, scale.y);
horizontally_flipped = true; horizontally_flipped = !horizontally_flipped;
} else {
sprite.setScale(1, 1);
horizontally_flipped = false;
}
} }
void simpleengine::Animation::FlipVertically() { void simpleengine::Animation::FlipVertically() {
if (!vertically_flipped) { sf::Vector2f scale = sprite.getScale();
sprite.setScale(1, -1); sprite.setScale(scale.x, -scale.y);
vertically_flipped = true; vertically_flipped = !vertically_flipped;
} else {
sprite.setScale(1, 1);
vertically_flipped = false;
}
} }
bool simpleengine::Animation::IsHorizontallyFlipped() const { bool simpleengine::Animation::IsHorizontallyFlipped() const {

View File

@ -6,6 +6,8 @@
#include "components/ssma_component.h" #include "components/ssma_component.h"
#include <iostream>
simpleengine::SideScrollerMovementAnimationComponent::SideScrollerMovementAnimationComponent(simpleengine::Entity &owning_entity, simpleengine::SideScrollerMovementAnimationComponent::SideScrollerMovementAnimationComponent(simpleengine::Entity &owning_entity,
sf::Sprite &sprite, sf::Texture &texture_sheet, float max_velocity, float acceleration, float deceleration) sf::Sprite &sprite, sf::Texture &texture_sheet, float max_velocity, float acceleration, float deceleration)
: Component(owning_entity), anim_component(owning_entity, sprite, texture_sheet), : Component(owning_entity), anim_component(owning_entity, sprite, texture_sheet),
@ -73,9 +75,6 @@ void simpleengine::SideScrollerMovementAnimationComponent::Update(const float& d
anim.Update(delta_time); anim.Update(delta_time);
} else { } else {
Animation& anim = anim_component.GetAnimation("IDLE_LEFT"); Animation& anim = anim_component.GetAnimation("IDLE_LEFT");
if (!anim.IsHorizontallyFlipped()) {
anim.FlipHorizontally();
}
anim.Update(delta_time); anim.Update(delta_time);
} }
} else { } else {
@ -89,9 +88,6 @@ void simpleengine::SideScrollerMovementAnimationComponent::Update(const float& d
anim.Update(delta_time); anim.Update(delta_time);
} else { } else {
Animation& anim = anim_component.GetAnimation("IDLE_RIGHT"); Animation& anim = anim_component.GetAnimation("IDLE_RIGHT");
if (!anim.IsHorizontallyFlipped()) {
anim.FlipHorizontally();
}
anim.Update(delta_time); anim.Update(delta_time);
} }
} }