Control over animation looping and event on animation completion

This commit is contained in:
2015-10-21 15:38:34 +02:00
parent e7c1dc20ba
commit 52f7e3f64b
4 changed files with 48 additions and 2 deletions
+3
View File
@@ -4,6 +4,9 @@
#include "Core/System.h" #include "Core/System.h"
#include "Core/World.h" #include "Core/World.h"
#include "Rendering/CAnimation.h" #include "Rendering/CAnimation.h"
#include "Rendering/EAnimationComplete.h"
#include "Rendering/CModel.h"
#include "Rendering/Model.h"
#include "Game/EPause.h" #include "Game/EPause.h"
#include "Game/EResume.h" #include "Game/EResume.h"
+1
View File
@@ -13,6 +13,7 @@ struct Animation : Component
std::string Name; std::string Name;
double Time = 0.0; double Time = 0.0;
double Speed = 0.0; double Speed = 0.0;
bool Loop = true;
bool NoRootMotion = true; bool NoRootMotion = true;
}; };
+21
View File
@@ -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
+23 -2
View File
@@ -24,8 +24,29 @@ void dd::Systems::AnimationSystem::Update(double dt)
} }
for (auto& component : *animations) { for (auto& component : *animations) {
auto animation = static_cast<Components::Animation*>(component.get()); EntityID ent = component->Entity;
animation->Time += animation->Speed * dt; auto animationComponent = static_cast<Components::Animation*>(component.get());
// Get animation information
auto modelComponent = m_World->GetComponent<Components::Model>(ent);
auto model = ResourceManager::Load<Model>(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;
}
} }
} }