diff --git a/include/Rendering/AnimationSystem.h b/include/Rendering/AnimationSystem.h index 4fe4cb8..2ce1569 100644 --- a/include/Rendering/AnimationSystem.h +++ b/include/Rendering/AnimationSystem.h @@ -4,6 +4,9 @@ #include "Core/System.h" #include "Core/World.h" #include "Rendering/CAnimation.h" +#include "Rendering/EAnimationComplete.h" +#include "Rendering/CModel.h" +#include "Rendering/Model.h" #include "Game/EPause.h" #include "Game/EResume.h" diff --git a/include/Rendering/CAnimation.h b/include/Rendering/CAnimation.h index 1b953e7..d708d55 100644 --- a/include/Rendering/CAnimation.h +++ b/include/Rendering/CAnimation.h @@ -13,6 +13,7 @@ struct Animation : Component std::string Name; double Time = 0.0; double Speed = 0.0; + bool Loop = true; bool NoRootMotion = true; }; diff --git a/include/Rendering/EAnimationComplete.h b/include/Rendering/EAnimationComplete.h new file mode 100644 index 0000000..e39ce57 --- /dev/null +++ b/include/Rendering/EAnimationComplete.h @@ -0,0 +1,21 @@ +#ifndef RENDERING_EANIMATIONCOMPLETE_H__ +#define RENDERING_EANIMATIONCOMPLETE_H__ + +#include "Core/Entity.h" +#include "Core/EventBroker.h" + +namespace dd +{ +namespace Events +{ + +struct AnimationComplete +{ + EntityID Entity; + std::string Animation; +}; + +} +} + +#endif \ No newline at end of file diff --git a/src/game/Rendering/AnimationSystem.cpp b/src/game/Rendering/AnimationSystem.cpp index 35f853a..8a44ebb 100644 --- a/src/game/Rendering/AnimationSystem.cpp +++ b/src/game/Rendering/AnimationSystem.cpp @@ -24,8 +24,29 @@ void dd::Systems::AnimationSystem::Update(double dt) } for (auto& component : *animations) { - auto animation = static_cast(component.get()); - animation->Time += animation->Speed * dt; + EntityID ent = component->Entity; + auto animationComponent = static_cast(component.get()); + + // Get animation information + auto modelComponent = m_World->GetComponent(ent); + auto model = ResourceManager::Load(modelComponent->ModelFile); + auto skeleton = model->m_Skeleton; + auto animation = skeleton->GetAnimation(animationComponent->Name); + + if (animationComponent->Speed != 0.0) { + double nextTime = animationComponent->Time + animationComponent->Speed * dt; + if (glm::abs(nextTime) > animation->Duration) { + if (!animationComponent->Loop) { + animationComponent->Time = glm::sign(nextTime) * animation->Duration; + animationComponent->Speed = 0.0; + LOG_DEBUG("Animation \"%s\" on Entity %i finished.", animationComponent->Name.c_str(), ent); + Events::AnimationComplete e; + e.Entity = ent; + e.Animation = animationComponent->Name; + } + } + animationComponent->Time = nextTime; + } } }