diff --git a/SHADE_Engine/src/Animation/SHAnimationController.cpp b/SHADE_Engine/src/Animation/SHAnimationController.cpp index 3230b2d9..b3105e64 100644 --- a/SHADE_Engine/src/Animation/SHAnimationController.cpp +++ b/SHADE_Engine/src/Animation/SHAnimationController.cpp @@ -17,6 +17,52 @@ of DigiPen Institute of Technology is prohibited. namespace SHADE { + /*-----------------------------------------------------------------------------------*/ + /* AnimParam Functions */ + /*-----------------------------------------------------------------------------------*/ + SHAnimationController::AnimParam::AnimParam(Type type) + : ParamType { type } + { + switch (ParamType) + { + case Type::Bool: + Value = false; + break; + case Type::Float: + Value = 0.0f; + break; + case Type::Int: + Value = 0; + break; + } + } + + /*-----------------------------------------------------------------------------------*/ + /* Transition - Usage Functions */ + /*-----------------------------------------------------------------------------------*/ + bool SHAnimationController::Transition::EvaluateCondition(const AnimParam& testParam) const noexcept + { + // Don't match, instant fail + if (testParam.ParamType != Param.ParamType) + return false; + + // Evaluate them accordingly + switch (Param.ParamType) + { + case AnimParam::Type::Bool: + case AnimParam::Type::Trigger: + return evaluateCondition(std::get(testParam.Value)); + case AnimParam::Type::Float: + return evaluateCondition(std::get(testParam.Value)); + break; + case AnimParam::Type::Int: + return evaluateCondition(std::get(testParam.Value)); + break; + } + + return false; + } + /*-----------------------------------------------------------------------------------*/ /* Lifecycle Functions */ /*-----------------------------------------------------------------------------------*/ @@ -38,7 +84,33 @@ namespace SHADE // Go to next state for (const auto& transition : instData.CurrentNode->Transitions) { - // TODO + // Check for no condition special case + if (transition.Condition == Transition::ConditionType::None) + { + changeNode(instData, transition.Target); + break; + } + else + { + // Check if we have the parameter + if (!instData.Params.contains(transition.ParamName)) + continue; + + // If evaluation success, we transition + AnimParam& param = instData.Params[transition.ParamName]; + if (transition.EvaluateCondition(param)) + { + changeNode(instData, transition.Target); + + // If trigger, we need to unset it + if (param.ParamType == AnimParam::Type::Trigger) + { + param.Value = false; + } + + break; + } + } } } } @@ -81,24 +153,13 @@ namespace SHADE // Clear node node.Free(); } - + /*-----------------------------------------------------------------------------------*/ - /* AnimParam Functions */ + /* Helper Functions */ /*-----------------------------------------------------------------------------------*/ - SHAnimationController::AnimParam::AnimParam(Type type) - : ParamType { type } + void SHAnimationController::changeNode(InstanceData& instData, Handle newNode) { - switch (ParamType) - { - case Type::Bool: - Value = false; - break; - case Type::Float: - Value = 0.0f; - break; - case Type::Int: - Value = 0; - break; - } + instData.CurrentNode = newNode; + instData.ClipPlaybackTime = 0.0f; } } diff --git a/SHADE_Engine/src/Animation/SHAnimationController.h b/SHADE_Engine/src/Animation/SHAnimationController.h index 4efb1116..2aa46f3c 100644 --- a/SHADE_Engine/src/Animation/SHAnimationController.h +++ b/SHADE_Engine/src/Animation/SHAnimationController.h @@ -55,7 +55,7 @@ namespace SHADE enum class Type { Bool, - Trigger, + Trigger, // Variant of bool that can only be set to true and will be unset when consumed. Float, Int }; @@ -69,7 +69,7 @@ namespace SHADE /// on the specified type. /// /// Type of AnimParam. - explicit AnimParam(Type type); + explicit AnimParam(Type type = Type::Int); /*-------------------------------------------------------------------------------*/ /* Data Members */ @@ -103,9 +103,27 @@ namespace SHADE /*-------------------------------------------------------------------------------*/ /* Data Members */ /*-------------------------------------------------------------------------------*/ - ConditionType Condition; - AnimParam::ValueType Threshold; - Handle Target; + ConditionType Condition; + AnimParam Param; + std::string ParamName; + Handle Target; + + /*-------------------------------------------------------------------------------*/ + /* Usage Functions */ + /*-------------------------------------------------------------------------------*/ + /// + /// Checks the condition of this Transition against an animation paramter. + /// + /// Parameter to test with. + /// Whether the condition passed. + bool EvaluateCondition(const AnimParam& testParam) const noexcept; + + private: + /*-------------------------------------------------------------------------------*/ + /* Helper Functions */ + /*-------------------------------------------------------------------------------*/ + template + bool evaluateCondition(T value) const noexcept; }; /// @@ -167,5 +185,12 @@ namespace SHADE Handle startNode; std::vector> nodes; std::unordered_map parameters; + + /*---------------------------------------------------------------------------------*/ + /* Helper Functions */ + /*---------------------------------------------------------------------------------*/ + void changeNode(InstanceData& instData, Handle newNode); }; } + +#include "SHAnimationController.hpp" \ No newline at end of file diff --git a/SHADE_Engine/src/Animation/SHAnimationController.hpp b/SHADE_Engine/src/Animation/SHAnimationController.hpp new file mode 100644 index 00000000..054f066e --- /dev/null +++ b/SHADE_Engine/src/Animation/SHAnimationController.hpp @@ -0,0 +1,67 @@ +/************************************************************************************//*! +\file SHAnimationController.hpp +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Mar 1, 2023 +\brief Contains the definition of template functions SHAnimationController. + +Copyright (C) 2023 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ +#pragma once +#include "SHAnimationController.h" + +namespace SHADE +{ + template + bool SHAnimationController::Transition::evaluateCondition(T value) const noexcept + { + // Early failure if invalid data + if (!std::holds_alternative(Param.Value)) + return false; + + // Get the value + const T PARAM_VAL = std::get(Param.Value); + + // Handle condition type + switch (Condition) + { + case SHAnimationController::Transition::ConditionType::None: + return true; + case SHAnimationController::Transition::ConditionType::Equals: + if constexpr (std::is_floating_point_v) + { + static constexpr T EPSILON = static_cast(0.001); + return std::abs(std::abs(value) - std::abs(PARAM_VAL)) < EPSILON; + } + else + { + return value == PARAM_VAL; + } + break; + case SHAnimationController::Transition::ConditionType::NotEquals: + if constexpr (std::is_floating_point_v) + { + static constexpr T EPSILON = static_cast(0.001); + return std::abs(std::abs(value) - std::abs(PARAM_VAL)) > EPSILON; + } + else + { + return value != PARAM_VAL; + } + break; + case SHAnimationController::Transition::ConditionType::LessThan: + return PARAM_VAL < value; + case SHAnimationController::Transition::ConditionType::LessThanOrEqual: + return PARAM_VAL <= value; + case SHAnimationController::Transition::ConditionType::GreaterThan: + return PARAM_VAL > value; + case SHAnimationController::Transition::ConditionType::GreaterThanOrEqual: + return PARAM_VAL >= value; + } + + // Neither of the existing cases + return false; + } +} \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Textures/SHTextureLibrary.h b/SHADE_Engine/src/Graphics/MiddleEnd/Textures/SHTextureLibrary.h index bbc72a1a..e1c93349 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Textures/SHTextureLibrary.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Textures/SHTextureLibrary.h @@ -71,7 +71,7 @@ namespace SHADE //! of the texture library would mean the recreation of the desc set that also //! involves the generic data, which is bad bad bad. Solution is to separate the //! 2 desc sets. - static constexpr uint32_t DEFAULT_MAX_TEXTURES = 2000; + static constexpr uint32_t DEFAULT_MAX_TEXTURES = 1000; /*-----------------------------------------------------------------------------*/ /* Usage Functions */