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:
parent
f237cf9eff
commit
06c17658e6
|
@ -19,18 +19,20 @@ class PlayerEntity : public simpleengine::Entity {
|
|||
private:
|
||||
sf::Sprite sprite;
|
||||
sf::Texture texture;
|
||||
float movement_speed = 150;
|
||||
float movement_speed = 130;
|
||||
sf::Vector2u window_size;
|
||||
|
||||
std::unique_ptr<simpleengine::SideScrollerMovementAnimationComponent> move_anim_component;
|
||||
public:
|
||||
explicit PlayerEntity(sf::Vector2u window_size) : window_size(window_size) {
|
||||
texture.loadFromFile("player_sheet.png");
|
||||
texture.setSmooth(true);
|
||||
sprite.setTexture(texture);
|
||||
sprite.setScale(.7, .7);
|
||||
|
||||
move_anim_component = std::make_unique<simpleengine::SideScrollerMovementAnimationComponent>(*this, sprite,
|
||||
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);
|
||||
move_anim_component->SetAnimation(simpleengine::MovementAnimationType::IDLE_LEFT, 20, 0, 0,
|
||||
6, 0, 128, 128);
|
||||
|
|
|
@ -36,6 +36,7 @@ namespace simpleengine {
|
|||
sf::IntRect start_rect;
|
||||
sf::IntRect current_rect;
|
||||
sf::IntRect end_rect;
|
||||
sf::Vector2f start_scale;
|
||||
|
||||
bool horizontally_flipped = false;
|
||||
bool vertically_flipped = false;
|
||||
|
|
|
@ -16,9 +16,10 @@ simpleengine::Animation::Animation(sf::Sprite &sprite, sf::Texture &texture_shee
|
|||
// Set start of sprite.
|
||||
this->sprite.setTexture(texture_sheet, true);
|
||||
this->sprite.setTextureRect(start_rect);
|
||||
this->start_scale = sprite.getScale();
|
||||
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) {
|
||||
|
@ -45,23 +46,15 @@ void simpleengine::Animation::Reset() {
|
|||
}
|
||||
|
||||
void simpleengine::Animation::FlipHorizontally() {
|
||||
if (!horizontally_flipped) {
|
||||
sprite.setScale(-1, 1);
|
||||
horizontally_flipped = true;
|
||||
} else {
|
||||
sprite.setScale(1, 1);
|
||||
horizontally_flipped = false;
|
||||
}
|
||||
sf::Vector2f scale = sprite.getScale();
|
||||
sprite.setScale(-scale.x, scale.y);
|
||||
horizontally_flipped = !horizontally_flipped;
|
||||
}
|
||||
|
||||
void simpleengine::Animation::FlipVertically() {
|
||||
if (!vertically_flipped) {
|
||||
sprite.setScale(1, -1);
|
||||
vertically_flipped = true;
|
||||
} else {
|
||||
sprite.setScale(1, 1);
|
||||
vertically_flipped = false;
|
||||
}
|
||||
sf::Vector2f scale = sprite.getScale();
|
||||
sprite.setScale(scale.x, -scale.y);
|
||||
vertically_flipped = !vertically_flipped;
|
||||
}
|
||||
|
||||
bool simpleengine::Animation::IsHorizontallyFlipped() const {
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
|
||||
#include "components/ssma_component.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
simpleengine::SideScrollerMovementAnimationComponent::SideScrollerMovementAnimationComponent(simpleengine::Entity &owning_entity,
|
||||
sf::Sprite &sprite, sf::Texture &texture_sheet, float max_velocity, float acceleration, float deceleration)
|
||||
: 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);
|
||||
} else {
|
||||
Animation& anim = anim_component.GetAnimation("IDLE_LEFT");
|
||||
if (!anim.IsHorizontallyFlipped()) {
|
||||
anim.FlipHorizontally();
|
||||
}
|
||||
anim.Update(delta_time);
|
||||
}
|
||||
} else {
|
||||
|
@ -89,9 +88,6 @@ void simpleengine::SideScrollerMovementAnimationComponent::Update(const float& d
|
|||
anim.Update(delta_time);
|
||||
} else {
|
||||
Animation& anim = anim_component.GetAnimation("IDLE_RIGHT");
|
||||
if (!anim.IsHorizontallyFlipped()) {
|
||||
anim.FlipHorizontally();
|
||||
}
|
||||
anim.Update(delta_time);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue