BoneAttachments now working and BlendTrees are created in the AnimationSystem
This commit is contained in:
@@ -1,66 +1,109 @@
|
||||
#include "Rendering/AnimationSystem.h"
|
||||
|
||||
void AnimationSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& animationComponent, double dt)
|
||||
AnimationSystem::AnimationSystem(SystemParams params)
|
||||
: System(params)
|
||||
{
|
||||
|
||||
EntityWrapper parent = entity.FirstParentWithComponent("Model");
|
||||
|
||||
Model* model;
|
||||
try {
|
||||
model = ResourceManager::Load<::Model, true>(parent["Model"]["Resource"]);
|
||||
} catch (const std::exception&) {
|
||||
}
|
||||
|
||||
void AnimationSystem::Update(double dt)
|
||||
{
|
||||
UpdateAnimations(dt);
|
||||
CreateBlendTrees();
|
||||
}
|
||||
|
||||
void AnimationSystem::CreateBlendTrees()
|
||||
{
|
||||
auto modelComponents = m_World->GetComponents("Model");
|
||||
if (modelComponents == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
Skeleton* skeleton = model->m_RawModel->m_Skeleton;
|
||||
if (skeleton == nullptr) {
|
||||
for (auto& modelC : *modelComponents) {
|
||||
EntityWrapper entity = EntityWrapper(m_World, modelC.EntityID);
|
||||
|
||||
Model* model;
|
||||
try {
|
||||
model = ResourceManager::Load<::Model, true>(entity["Model"]["Resource"]);
|
||||
} catch (const std::exception&) {
|
||||
continue;;
|
||||
}
|
||||
|
||||
Skeleton* skeleton = model->m_RawModel->m_Skeleton;
|
||||
if (skeleton == nullptr) {
|
||||
continue;
|
||||
}
|
||||
|
||||
skeleton->BlendTrees[entity] = std::shared_ptr<BlendTree>(new BlendTree(entity, skeleton));
|
||||
}
|
||||
}
|
||||
|
||||
void AnimationSystem::UpdateAnimations(double dt)
|
||||
{
|
||||
auto animationComponents = m_World->GetComponents("Animation");
|
||||
if(animationComponents == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 1; i <= 1; i++) {
|
||||
const Skeleton::Animation* animation = skeleton->GetAnimation(animationComponent["AnimationName"]);
|
||||
for (auto& animationC : *animationComponents) {
|
||||
EntityWrapper entity = EntityWrapper(m_World, animationC.EntityID);
|
||||
EntityWrapper parent = entity.FirstParentWithComponent("Model");
|
||||
|
||||
Model* model;
|
||||
try {
|
||||
model = ResourceManager::Load<::Model, true>(parent["Model"]["Resource"]);
|
||||
} catch (const std::exception&) {
|
||||
return;
|
||||
}
|
||||
|
||||
Skeleton* skeleton = model->m_RawModel->m_Skeleton;
|
||||
if (skeleton == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
const Skeleton::Animation* animation = skeleton->GetAnimation(animationC["AnimationName"]);
|
||||
|
||||
if (animation == nullptr) {
|
||||
continue;;
|
||||
}
|
||||
|
||||
double animationSpeed = (double)animationComponent["Speed"];
|
||||
double animationSpeed = (double)animationC["Speed"];
|
||||
|
||||
if (animationSpeed != 0.0) {
|
||||
double nextTime = (double)animationComponent["Time"] + animationSpeed * dt;
|
||||
double nextTime = (double)animationC["Time"] + animationSpeed * dt;
|
||||
|
||||
|
||||
if (!(bool)animationComponent["Loop"]) {
|
||||
if (!(bool)animationC["Loop"]) {
|
||||
if (nextTime > animation->Duration) {
|
||||
nextTime = animation->Duration;
|
||||
Events::AnimationComplete e;
|
||||
e.Entity = entity;
|
||||
e.Name = (std::string)animationComponent["AnimationName"];
|
||||
e.Name = (std::string)animationC["AnimationName"];
|
||||
m_EventBroker->Publish(e);
|
||||
} else if (nextTime < 0) {
|
||||
Events::AnimationComplete e;
|
||||
e.Entity = entity;
|
||||
e.Name = (std::string)animationComponent["AnimationName"];
|
||||
e.Name = (std::string)animationC["AnimationName"];
|
||||
m_EventBroker->Publish(e);
|
||||
nextTime = 0;
|
||||
}
|
||||
|
||||
(double&)animationComponent["Speed"] = 0.0;
|
||||
|
||||
(double&)animationC["Speed"] = 0.0;
|
||||
|
||||
} else {
|
||||
if (nextTime > animation->Duration) {
|
||||
Events::AnimationComplete e;
|
||||
e.Entity = entity;
|
||||
e.Name = (std::string)animationComponent["AnimationName"];
|
||||
e.Name = (std::string)animationC["AnimationName"];
|
||||
m_EventBroker->Publish(e);
|
||||
|
||||
while(nextTime > animation->Duration) {
|
||||
while (nextTime > animation->Duration) {
|
||||
nextTime -= animation->Duration;
|
||||
}
|
||||
} else if (nextTime < 0) {
|
||||
Events::AnimationComplete e;
|
||||
e.Entity = entity;
|
||||
e.Name = (std::string)animationComponent["AnimationName"];
|
||||
e.Name = (std::string)animationC["AnimationName"];
|
||||
m_EventBroker->Publish(e);
|
||||
|
||||
while (nextTime < 0) {
|
||||
@@ -69,8 +112,8 @@ void AnimationSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& a
|
||||
}
|
||||
}
|
||||
|
||||
(double&)animationComponent["Time"] = nextTime;
|
||||
(double&)animationC["Time"] = nextTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user