diff --git a/SHADE_Engine/src/Animation/SHAnimationController.cpp b/SHADE_Engine/src/Animation/SHAnimationController.cpp index 3af70309..86c665fd 100644 --- a/SHADE_Engine/src/Animation/SHAnimationController.cpp +++ b/SHADE_Engine/src/Animation/SHAnimationController.cpp @@ -12,8 +12,67 @@ of DigiPen Institute of Technology is prohibited. *//*************************************************************************************/ #include "SHpch.h" #include "SHAnimationController.h" +#include "SHAnimationSystem.h" +#include "ECS_Base/Managers/SHSystemManager.h" 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::CreateNode() + { + // Get system + auto system = SHSystemManager::GetSystem(); + if (system == nullptr) + throw std::runtime_error("[SHAnimationController] No SHAnimationSystem found!"); + + // Construct + auto node = system->GetResourceHub().Create(); + 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) + { + // 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(); + } } diff --git a/SHADE_Engine/src/Animation/SHAnimationController.h b/SHADE_Engine/src/Animation/SHAnimationController.h index d21c5c01..c6d353e6 100644 --- a/SHADE_Engine/src/Animation/SHAnimationController.h +++ b/SHADE_Engine/src/Animation/SHAnimationController.h @@ -92,7 +92,7 @@ namespace SHADE /* Data Members */ /*-------------------------------------------------------------------------------*/ ConditionType Condition; - Handle Target; + Handle Target; }; /// @@ -103,13 +103,47 @@ namespace SHADE Handle Clip; std::vector Transitions; }; + + /*---------------------------------------------------------------------------------*/ + /* Lifecycle Functions */ + /*---------------------------------------------------------------------------------*/ + /// + /// Runs a single update for the animation controller. + /// + void Update(); + /// + /// Resets the animation controller to its starting node. + /// + void Reset(); + /*---------------------------------------------------------------------------------*/ + /* Usage Functions */ + /*---------------------------------------------------------------------------------*/ + /// + /// Creates a node in the state machine. Created nodes must be destroyed using + /// DestroyNode(). + /// + /// Node that was created. + Handle CreateNode(); + /// + /// Destroys the node that was created in the state machine. + /// + /// Node to destroy. + void DestroyNode(Handle node); + + /*---------------------------------------------------------------------------------*/ + /* Getters */ + /*---------------------------------------------------------------------------------*/ + Handle GetCurrentNode() const noexcept { return currentNode; } private: /*---------------------------------------------------------------------------------*/ /* Data Members */ /*---------------------------------------------------------------------------------*/ + // State machine Handle currentNode; + Handle startNode; std::vector> nodes; + std::unordered_map parameters; }; } diff --git a/SHADE_Engine/src/Animation/SHAnimationSystem.h b/SHADE_Engine/src/Animation/SHAnimationSystem.h index 3d46edc2..81b012d3 100644 --- a/SHADE_Engine/src/Animation/SHAnimationSystem.h +++ b/SHADE_Engine/src/Animation/SHAnimationSystem.h @@ -51,5 +51,16 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ virtual void Init(void) override final; virtual void Exit(void) override final; + + /*---------------------------------------------------------------------------------*/ + /* Getters */ + /*---------------------------------------------------------------------------------*/ + SHResourceHub& GetResourceHub() { return resources; } + + private: + /*---------------------------------------------------------------------------------*/ + /* Data Members */ + /*---------------------------------------------------------------------------------*/ + SHResourceHub resources; }; } \ No newline at end of file diff --git a/SHADE_Engine/src/Animation/SHAnimatorComponent.cpp b/SHADE_Engine/src/Animation/SHAnimatorComponent.cpp index 71db24db..5a6e78ce 100644 --- a/SHADE_Engine/src/Animation/SHAnimatorComponent.cpp +++ b/SHADE_Engine/src/Animation/SHAnimatorComponent.cpp @@ -34,6 +34,7 @@ namespace SHADE void SHAnimatorComponent::Play() { isPlaying = false; + playOnce = false; } void SHAnimatorComponent::Play(Handle clip) @@ -43,6 +44,12 @@ namespace SHADE Play(); } + void SHAnimatorComponent::PlayOneShot(Handle clip) + { + Play(clip); + playOnce = true; + } + void SHAnimatorComponent::PlayFromStart() { isPlaying = true; @@ -110,7 +117,7 @@ namespace SHADE /*-----------------------------------------------------------------------------------*/ void SHAnimatorComponent::Update(float dt) { - //Reset matrices + // Reset matrices std::fill(boneMatrices.begin(), boneMatrices.end(), SHMatrix::Identity); // Nothing to animate @@ -121,7 +128,15 @@ namespace SHADE currPlaybackTime += dt; if (currPlaybackTime > currClip->GetTotalTime()) { - currPlaybackTime = currPlaybackTime - currClip->GetTotalTime(); + if (playOnce) + { + playOnce = false; + isPlaying = false; + } + else + { + currPlaybackTime = currPlaybackTime - currClip->GetTotalTime(); + } } // Play the clip diff --git a/SHADE_Engine/src/Animation/SHAnimatorComponent.h b/SHADE_Engine/src/Animation/SHAnimatorComponent.h index b47106f8..01f1c2eb 100644 --- a/SHADE_Engine/src/Animation/SHAnimatorComponent.h +++ b/SHADE_Engine/src/Animation/SHAnimatorComponent.h @@ -54,9 +54,14 @@ namespace SHADE /// /// Plays the specified animation clip from the start. /// - /// + /// Animation clip to play. void Play(Handle clip); /// + /// Plays the specified animation clip from the start one time only. + /// + /// Animation clip to play. + void PlayOneShot(Handle clip); + /// /// Plays the currently loaded animation clip from the start. /// void PlayFromStart(); @@ -130,6 +135,7 @@ namespace SHADE // Playback Tracking float currPlaybackTime = 0.0f; bool isPlaying = true; + bool playOnce = false; // Useful Cached Data float secsPerTick = 0.0f; // Buffer