diff --git a/include/simpleengine/animation.h b/include/simpleengine/animation.h index afdd23b..435f0ff 100644 --- a/include/simpleengine/animation.h +++ b/include/simpleengine/animation.h @@ -15,12 +15,17 @@ namespace simpleengine { Animation(sf::Sprite &sprite, sf::Texture &texture_sheet, float speed, int start_frame_x, int start_frame_y, int frame_ct_x, int frame_ct_y, int width, int height); - void Update(const float &delta_time); + void Update(const float& delta_time); void Reset(); + void FlipHorizontally(); + void FlipVertically(); + + bool IsHorizontallyFlipped() const; + bool IsVerticallyFlipped() const; + /*void Play(); void Pause();*/ - private: sf::Sprite &sprite; sf::Texture &texture_sheet; @@ -32,6 +37,8 @@ namespace simpleengine { sf::IntRect current_rect; sf::IntRect end_rect; + bool horizontally_flipped = false; + bool vertically_flipped = false; }; } diff --git a/include/simpleengine/components/animation_component.h b/include/simpleengine/components/animation_component.h index b4d562b..fff672c 100644 --- a/include/simpleengine/components/animation_component.h +++ b/include/simpleengine/components/animation_component.h @@ -25,12 +25,20 @@ namespace simpleengine { public: explicit AnimationComponent(Entity& owning_entity, sf::Sprite& sprite, sf::Texture& texture_sheet); - void Update(const float &delta_time) override; + void Update(const float& delta_time) override; void AddAnimation(const std::string& animation_name, float speed, int start_frame_x, int start_frame_y, int frame_ct_x, int frame_ct_y, int width, int height); void AddAnimation(const std::string& animation_name, simpleengine::Animation animation); + bool HasAnimation(const std::string& animation_name); + + // Animation class specific void UpdateAnimation(const std::string& animation_name, const float& delta_time); + Animation& GetAnimation(const std::string& animation_name); + /*void FlipAnimationHorizontally(const std::string& animation_name); + void FlipAnimationVertically(const std::string& animation_name); + bool IsAnimationFlippedHorizontally(const std::string& animation_name); + bool IsAnimationFlippedVertically(const std::string& animation_name);*/ sf::Sprite &GetSprite() const; sf::Texture &GetTextureSheet() const; diff --git a/include/simpleengine/components/movement/movement_component.h b/include/simpleengine/components/movement/movement_component.h index f6b1b64..78ded8e 100644 --- a/include/simpleengine/components/movement/movement_component.h +++ b/include/simpleengine/components/movement/movement_component.h @@ -18,11 +18,15 @@ namespace simpleengine { public: MovementComponent(Entity& owning_entity, float max_velocity, float acceleration = 7, float deceleration = 2.5); - void Move(const float &delta_time, const float& dir_x, const float& dir_y); - void Update(const float &delta_time) override; + void Move(const float& delta_time, const float& dir_x, const float& dir_y); + void Update(const float& delta_time) override; + + const sf::Vector2f& GetVelocity() const; + const sf::Vector2f& GetLastDirection() const; private: float max_velocity; sf::Vector2f velocity; + sf::Vector2f last_direction; float acceleration; float deceleration; }; diff --git a/src/animation.cpp b/src/animation.cpp index 0d6e942..fc54b95 100644 --- a/src/animation.cpp +++ b/src/animation.cpp @@ -17,9 +17,11 @@ simpleengine::Animation::Animation(sf::Sprite &sprite, sf::Texture &texture_shee this->sprite.setTexture(texture_sheet, true); this->sprite.setTextureRect(start_rect); this->current_rect = start_rect; + + sprite.setOrigin({ static_cast(sprite.getLocalBounds().width * .5), 0 }); } -void simpleengine::Animation::Update(const float &delta_time) { +void simpleengine::Animation::Update(const float& delta_time) { // Update timer timer += 100 * delta_time; if (timer >= speed) { @@ -41,3 +43,31 @@ void simpleengine::Animation::Reset() { timer = 0; current_rect = start_rect; } + +void simpleengine::Animation::FlipHorizontally() { + if (!horizontally_flipped) { + sprite.setScale(-1, 1); + horizontally_flipped = true; + } else { + sprite.setScale(1, 1); + horizontally_flipped = false; + } +} + +void simpleengine::Animation::FlipVertically() { + if (!vertically_flipped) { + sprite.setScale(1, -1); + vertically_flipped = true; + } else { + sprite.setScale(1, 1); + vertically_flipped = false; + } +} + +bool simpleengine::Animation::IsHorizontallyFlipped() const { + return horizontally_flipped; +} + +bool simpleengine::Animation::IsVerticallyFlipped() const { + return vertically_flipped; +} diff --git a/src/components/animation_component.cpp b/src/components/animation_component.cpp index e523548..4720d4f 100644 --- a/src/components/animation_component.cpp +++ b/src/components/animation_component.cpp @@ -12,7 +12,7 @@ simpleengine::AnimationComponent::AnimationComponent(Entity& owning_entity, sf:: } -void simpleengine::AnimationComponent::Update(const float &delta_time) { +void simpleengine::AnimationComponent::Update(const float& delta_time) { } @@ -26,7 +26,7 @@ void simpleengine::AnimationComponent::AddAnimation(const std::string& animation animations.emplace(animation_name, animation); } -void simpleengine::AnimationComponent::UpdateAnimation(const std::string &animation_name, const float &delta_time) { +void simpleengine::AnimationComponent::UpdateAnimation(const std::string &animation_name, const float& delta_time) { auto anim = animations.find(animation_name); if (anim != animations.end()) { anim->second.Update(delta_time); @@ -46,3 +46,54 @@ sf::Texture &simpleengine::AnimationComponent::GetTextureSheet() const { const std::map &simpleengine::AnimationComponent::GetAnimations() const { return animations; } + +/*void simpleengine::AnimationComponent::FlipAnimationHorizontally(const std::string &animation_name) { + auto anim = animations.find(animation_name); + if (anim != animations.end()) { + anim->second.FlipHorizontally(); + } else { + throw std::runtime_error("Animation, \"" + animation_name + "\", was not found!"); + } +} + +void simpleengine::AnimationComponent::FlipAnimationVertically(const std::string &animation_name) { + auto anim = animations.find(animation_name); + if (anim != animations.end()) { + anim->second.FlipVertically(); + } else { + throw std::runtime_error("Animation, \"" + animation_name + "\", was not found!"); + } +} + +void simpleengine::AnimationComponent::IsAnimationFlippedHorizontally(const std::string &animation_name) { + auto anim = animations.find(animation_name); + if (anim != animations.end()) { + return anim->second.IsHorizontallyFlipped(); + } else { + throw std::runtime_error("Animation, \"" + animation_name + "\", was not found!"); + } +} + +void simpleengine::AnimationComponent::IsAnimationFlippedVertically(const std::string &animation_name) { + auto anim = animations.find(animation_name); + if (anim != animations.end()) { + return anim->second.IsHorizontallyFlipped(); + } else { + throw std::runtime_error("Animation, \"" + animation_name + "\", was not found!"); + } +}*/ + +simpleengine::Animation& simpleengine::AnimationComponent::GetAnimation(const std::string &animation_name) { + auto anim = animations.find(animation_name); + if (anim != animations.end()) { + return anim->second; + } else { + throw std::runtime_error("Animation, \"" + animation_name + "\", was not found!"); + } +} + + +bool simpleengine::AnimationComponent::HasAnimation(const std::string &animation_name) { + auto anim = animations.find(animation_name); + return anim != animations.end(); +} \ No newline at end of file diff --git a/src/components/movement/movement_component.cpp b/src/components/movement/movement_component.cpp index dafa7ef..87ba228 100644 --- a/src/components/movement/movement_component.cpp +++ b/src/components/movement/movement_component.cpp @@ -15,7 +15,7 @@ simpleengine::MovementComponent::MovementComponent(Entity& owning_entity, float } -void simpleengine::MovementComponent::Move(const float &delta_time, const float& dir_x, const float& dir_y) { +void simpleengine::MovementComponent::Move(const float& delta_time, const float& dir_x, const float& dir_y) { // Acceleration velocity.x += acceleration * dir_x; // Limit the horizontal velocity @@ -46,7 +46,7 @@ void simpleengine::MovementComponent::Move(const float &delta_time, const float& owning_entity.Move(velocity * delta_time); } -void simpleengine::MovementComponent::Update(const float &delta_time) { +void simpleengine::MovementComponent::Update(const float& delta_time) { sf::Vector2f direction; if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) { direction.x = -1; @@ -127,6 +127,7 @@ void simpleengine::MovementComponent::Update(const float &delta_time) { } if (direction.x != 0 || direction.y != 0) { + last_direction = direction; Move(delta_time, direction.x, direction.y); no_movement = false; } @@ -134,4 +135,12 @@ void simpleengine::MovementComponent::Update(const float &delta_time) { if (no_movement) { velocity = sf::Vector2f(); } -} \ No newline at end of file +} + +const sf::Vector2f &simpleengine::MovementComponent::GetVelocity() const { + return velocity; +} + +const sf::Vector2f &simpleengine::MovementComponent::GetLastDirection() const { + return last_direction; +}