Implemented Animation Clip asset and animation controller #410

Merged
XiaoQiDigipen merged 66 commits from SP3-22-AnimationController into main 2023-03-09 16:19:40 +08:00
5 changed files with 129 additions and 4 deletions
Showing only changes of commit ecc88dc142 - Show all commits

View File

@ -12,8 +12,67 @@ of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/ *//*************************************************************************************/
#include "SHpch.h" #include "SHpch.h"
#include "SHAnimationController.h" #include "SHAnimationController.h"
#include "SHAnimationSystem.h"
#include "ECS_Base/Managers/SHSystemManager.h"
namespace SHADE namespace SHADE
{ {
/*-----------------------------------------------------------------------------------*/
/* Lifecycle Functions */
/*-----------------------------------------------------------------------------------*/
void SHAnimationController::Update()
{
// Is there a valid node
if (!currentNode)
return;
// Update
for (const auto& transition : currentNode->Transitions)
{
}
}
void SHAnimationController::Reset()
{
currentNode = startNode;
}
/*-----------------------------------------------------------------------------------*/
/* Usage Functions */
/*-----------------------------------------------------------------------------------*/
Handle<SHAnimationController::Node> SHAnimationController::CreateNode()
{
// Get system
auto system = SHSystemManager::GetSystem<SHAnimationSystem>();
if (system == nullptr)
throw std::runtime_error("[SHAnimationController] No SHAnimationSystem found!");
// Construct
auto node = system->GetResourceHub().Create<Node>();
nodes.emplace_back(node);
// If there is no start node, this is the first node so make it the starting node
if (!startNode)
startNode = node;
return node;
}
void SHAnimationController::DestroyNode(Handle<Node> node)
{
// Remove from storage
auto iter = std::find(nodes.begin(), nodes.end(), node);
if (iter == nodes.end())
throw std::invalid_argument("[SHAnimationController] Attempted to delete a node that doesn't belong.");
// Remove if it is a start node
if (startNode == node)
startNode = {};
// Remove from nodes
nodes.erase(iter);
// Clear node
node.Free();
}
} }

View File

@ -92,7 +92,7 @@ namespace SHADE
/* Data Members */ /* Data Members */
/*-------------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------------*/
ConditionType Condition; ConditionType Condition;
Handle<Node> Target; Handle<Node> Target;
}; };
/// <summary> /// <summary>
@ -103,13 +103,47 @@ namespace SHADE
Handle<SHAnimationClip> Clip; Handle<SHAnimationClip> Clip;
std::vector<Transition> Transitions; std::vector<Transition> Transitions;
}; };
/*---------------------------------------------------------------------------------*/
/* Lifecycle Functions */
/*---------------------------------------------------------------------------------*/
/// <summary>
/// Runs a single update for the animation controller.
/// </summary>
void Update();
/// <summary>
/// Resets the animation controller to its starting node.
/// </summary>
void Reset();
/*---------------------------------------------------------------------------------*/
/* Usage Functions */
/*---------------------------------------------------------------------------------*/
/// <summary>
/// Creates a node in the state machine. Created nodes must be destroyed using
/// DestroyNode().
/// </summary>
/// <returns>Node that was created.</returns>
Handle<Node> CreateNode();
/// <summary>
/// Destroys the node that was created in the state machine.
/// </summary>
/// <param name="node">Node to destroy.</param>
void DestroyNode(Handle<Node> node);
/*---------------------------------------------------------------------------------*/
/* Getters */
/*---------------------------------------------------------------------------------*/
Handle<Node> GetCurrentNode() const noexcept { return currentNode; }
private: private:
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Data Members */ /* Data Members */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
// State machine
Handle<Node> currentNode; Handle<Node> currentNode;
Handle<Node> startNode;
std::vector<Handle<Node>> nodes; std::vector<Handle<Node>> nodes;
std::unordered_map<std::string, AnimParam> parameters;
}; };
} }

View File

@ -51,5 +51,16 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
virtual void Init(void) override final; virtual void Init(void) override final;
virtual void Exit(void) override final; virtual void Exit(void) override final;
/*---------------------------------------------------------------------------------*/
/* Getters */
/*---------------------------------------------------------------------------------*/
SHResourceHub& GetResourceHub() { return resources; }
private:
/*---------------------------------------------------------------------------------*/
/* Data Members */
/*---------------------------------------------------------------------------------*/
SHResourceHub resources;
}; };
} }

View File

@ -34,6 +34,7 @@ namespace SHADE
void SHAnimatorComponent::Play() void SHAnimatorComponent::Play()
{ {
isPlaying = false; isPlaying = false;
playOnce = false;
} }
void SHAnimatorComponent::Play(Handle<SHAnimationClip> clip) void SHAnimatorComponent::Play(Handle<SHAnimationClip> clip)
@ -43,6 +44,12 @@ namespace SHADE
Play(); Play();
} }
void SHAnimatorComponent::PlayOneShot(Handle<SHAnimationClip> clip)
{
Play(clip);
playOnce = true;
}
void SHAnimatorComponent::PlayFromStart() void SHAnimatorComponent::PlayFromStart()
{ {
isPlaying = true; isPlaying = true;
@ -110,7 +117,7 @@ namespace SHADE
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
void SHAnimatorComponent::Update(float dt) void SHAnimatorComponent::Update(float dt)
{ {
//Reset matrices // Reset matrices
std::fill(boneMatrices.begin(), boneMatrices.end(), SHMatrix::Identity); std::fill(boneMatrices.begin(), boneMatrices.end(), SHMatrix::Identity);
// Nothing to animate // Nothing to animate
@ -121,7 +128,15 @@ namespace SHADE
currPlaybackTime += dt; currPlaybackTime += dt;
if (currPlaybackTime > currClip->GetTotalTime()) if (currPlaybackTime > currClip->GetTotalTime())
{ {
currPlaybackTime = currPlaybackTime - currClip->GetTotalTime(); if (playOnce)
{
playOnce = false;
isPlaying = false;
}
else
{
currPlaybackTime = currPlaybackTime - currClip->GetTotalTime();
}
} }
// Play the clip // Play the clip

View File

@ -54,9 +54,14 @@ namespace SHADE
/// <summary> /// <summary>
/// Plays the specified animation clip from the start. /// Plays the specified animation clip from the start.
/// </summary> /// </summary>
/// <param name="clip"></param> /// <param name="clip">Animation clip to play.</param>
void Play(Handle<SHAnimationClip> clip); void Play(Handle<SHAnimationClip> clip);
/// <summary> /// <summary>
/// Plays the specified animation clip from the start one time only.
/// </summary>
/// <param name="clip">Animation clip to play.</param>
void PlayOneShot(Handle<SHAnimationClip> clip);
/// <summary>
/// Plays the currently loaded animation clip from the start. /// Plays the currently loaded animation clip from the start.
/// </summary> /// </summary>
void PlayFromStart(); void PlayFromStart();
@ -130,6 +135,7 @@ namespace SHADE
// Playback Tracking // Playback Tracking
float currPlaybackTime = 0.0f; float currPlaybackTime = 0.0f;
bool isPlaying = true; bool isPlaying = true;
bool playOnce = false;
// Useful Cached Data // Useful Cached Data
float secsPerTick = 0.0f; float secsPerTick = 0.0f;
// Buffer // Buffer