diff --git a/examples/animation/src/main.cpp b/examples/animation/src/main.cpp index 1ac3c88..8f5aaaa 100644 --- a/examples/animation/src/main.cpp +++ b/examples/animation/src/main.cpp @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include @@ -22,7 +22,7 @@ private: float movement_speed = 95; sf::Vector2u window_size; - std::unique_ptr anim_component; + std::unique_ptr move_anim_component; public: explicit PlayerEntity(sf::Vector2u window_size) : window_size(window_size) { AddComponent(std::make_unique(*this, movement_speed)); @@ -30,9 +30,10 @@ public: texture.loadFromFile("player_sheet.png"); sprite.setTexture(texture); - anim_component = std::make_unique(*this, sprite, texture); - anim_component->AddAnimation("IDLE_LEFT", 20, 0, 0, 6, 0, 128, 128); - anim_component->AddAnimation("WALK_LEFT_NO_SWORD", 9, 0, 8, 9, 8, 128, 128); + move_anim_component = std::make_unique(*this, sprite, texture, movement_speed, 5, 1.1); + move_anim_component->SetAnimation(simpleengine::MovementAnimationType::WALK_LEFT, 9, 0, 8, 9, 8, 128, 128); + move_anim_component->SetAnimation(simpleengine::MovementAnimationType::IDLE_LEFT, 20, 0, 0, 6, 0, 128, 128); + AddComponent(std::move(move_anim_component)); } ~PlayerEntity() override { @@ -55,9 +56,6 @@ public: void Update(const float& delta_time) override { Entity::Update(delta_time); - - //anim_component->PlayAnimation("IDLE_LEFT", delta_time); - anim_component->PlayAnimation("WALK_LEFT_NO_SWORD", delta_time); } void Render(sf::RenderTarget* target) override { @@ -66,7 +64,7 @@ public: }; int main(int argc, char *argv[]) { - simpleengine::Game game(500, 500, "SimpleEngine - Animation Example"); + simpleengine::Game game(700, 700, "SimpleEngine - Animation Example"); game.AddEvent(new simpleengine::EntityEvent(game.GetWindow(), std::make_unique(game.GetWindow()->getSize()))); return game.Run(); diff --git a/include/simpleengine/components/movement_animation_component.h b/include/simpleengine/components/movement_animation_component.h new file mode 100644 index 0000000..aa2759c --- /dev/null +++ b/include/simpleengine/components/movement_animation_component.h @@ -0,0 +1,49 @@ +// +// Created by SeanOMik on 7/5/2020. +// Github: https://github.com/SeanOMik +// Email: seanomik@gmail.com +// + +#ifndef SIMPLEENGINE_MOVEMENT_ANIMATION_COMPONENT_H +#define SIMPLEENGINE_MOVEMENT_ANIMATION_COMPONENT_H + +#include "../component.h" +#include "animation_component.h" +#include "movement/movement_component.h" + +#include + +namespace simpleengine { + class Entity; + class Animation; + + enum MovementAnimationType { + WALK_LEFT, + WALK_RIGHT, + IDLE_LEFT, + IDLE_RIGHT + }; + + class MovementAnimationComponent : public Component { + public: + MovementAnimationComponent(Entity& owning_entity, sf::Sprite &sprite, sf::Texture &texture_sheet, + float max_velocity, float acceleration, float deceleration); + + void SetAnimation(const MovementAnimationType& type, simpleengine::Animation animation); + void SetAnimation(const MovementAnimationType& type, 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) override; + + simpleengine::AnimationComponent GetAnimationComponent(); + simpleengine::MovementComponent GetMovementComponent(); + private: + std::string MovementAnimationTypeToStr(const MovementAnimationType& type); + + simpleengine::AnimationComponent anim_component; + simpleengine::MovementComponent move_component; + }; +} + + +#endif //SIMPLEENGINE_MOVEMENT_ANIMATION_COMPONENT_H diff --git a/src/components/movement_animation_component.cpp b/src/components/movement_animation_component.cpp new file mode 100644 index 0000000..e179d03 --- /dev/null +++ b/src/components/movement_animation_component.cpp @@ -0,0 +1,124 @@ +// +// Created by SeanOMik on 7/5/2020. +// Github: https://github.com/SeanOMik +// Email: seanomik@gmail.com +// + +#include "components/movement_animation_component.h" + +simpleengine::MovementAnimationComponent::MovementAnimationComponent(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), + move_component(owning_entity, max_velocity, acceleration, deceleration) { +} + +void simpleengine::MovementAnimationComponent::SetAnimation(const simpleengine::MovementAnimationType &type, + simpleengine::Animation animation) { + + anim_component.AddAnimation(MovementAnimationTypeToStr(type), animation); +} + +void simpleengine::MovementAnimationComponent::SetAnimation(const simpleengine::MovementAnimationType &type, + float speed, int start_frame_x, int start_frame_y, int frame_ct_x, int frame_ct_y, int width, int height) { + + anim_component.AddAnimation(MovementAnimationTypeToStr(type), speed, start_frame_x, start_frame_y, + frame_ct_x, frame_ct_y, width, height); +} + + +void simpleengine::MovementAnimationComponent::Update(const float& delta_time) { + move_component.Update(delta_time); + + if (move_component.GetVelocity().x > 0) { // Moving right + // If the user never set a WALK_RIGHT animation, then we need to flip the WALK_LEFT one. + if (anim_component.HasAnimation("WALK_RIGHT")) { + Animation& anim = anim_component.GetAnimation("WALK_RIGHT"); + if (anim.IsHorizontallyFlipped()) { + anim.FlipHorizontally(); + } + + anim.Update(delta_time); + } else { + Animation& anim = anim_component.GetAnimation("WALK_LEFT"); + if (!anim.IsHorizontallyFlipped()) { + anim.FlipHorizontally(); + } + anim.Update(delta_time); + } + } else if (move_component.GetVelocity().x < 0) { // Moving left + // If the user never set a WALK_LEFT animation, then we need to flip the WALK_RIGHT one. + if (anim_component.HasAnimation("WALK_LEFT")) { + Animation& anim = anim_component.GetAnimation("WALK_LEFT"); + if (anim.IsHorizontallyFlipped()) { + anim.FlipHorizontally(); + } + + anim.Update(delta_time); + } else { + Animation& anim = anim_component.GetAnimation("WALK_RIGHT"); + if (!anim.IsHorizontallyFlipped()) { + anim.FlipHorizontally(); + } + anim.Update(delta_time); + } + } else { + // If the user never set a IDLE_RIGHT animation, then we need to flip the IDLE_LEFT one. + if (move_component.GetLastDirection().x > 0) { // Facing right + if (anim_component.HasAnimation("IDLE_RIGHT")) { + Animation& anim = anim_component.GetAnimation("IDLE_RIGHT"); + if (anim.IsHorizontallyFlipped()) { + anim.FlipHorizontally(); + } + + anim.Update(delta_time); + } else { + Animation& anim = anim_component.GetAnimation("IDLE_LEFT"); + if (!anim.IsHorizontallyFlipped()) { + anim.FlipHorizontally(); + } + anim.Update(delta_time); + } + } else { + // If the user never set a IDLE_LEFT animation, then we need to flip the IDLE_RIGHT one. + if (anim_component.HasAnimation("IDLE_LEFT")) { // Facing left + Animation& anim = anim_component.GetAnimation("IDLE_LEFT"); + if (anim.IsHorizontallyFlipped()) { + anim.FlipHorizontally(); + } + + anim.Update(delta_time); + } else { + Animation& anim = anim_component.GetAnimation("IDLE_RIGHT"); + if (!anim.IsHorizontallyFlipped()) { + anim.FlipHorizontally(); + } + anim.Update(delta_time); + } + } + } +} + +std::string simpleengine::MovementAnimationComponent::MovementAnimationTypeToStr( + const simpleengine::MovementAnimationType &type) { + + switch (type) { + case WALK_LEFT: + return "WALK_LEFT"; + case WALK_RIGHT: + return "WALK_RIGHT"; + case IDLE_LEFT: + return "IDLE_LEFT"; + case IDLE_RIGHT: + return "IDLE_RIGHT"; + default: + return "UNKNOWN_MOVEMENT_ANIMATION_TYPE"; + } +} + +simpleengine::AnimationComponent simpleengine::MovementAnimationComponent::GetAnimationComponent() { + return anim_component; +} + +simpleengine::MovementComponent simpleengine::MovementAnimationComponent::GetMovementComponent() { + return move_component; +}