Added implementation of SHAnimationController Transition logic
This commit is contained in:
parent
2679d12afb
commit
e878b7b65a
|
@ -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<bool>(testParam.Value));
|
||||
case AnimParam::Type::Float:
|
||||
return evaluateCondition(std::get<float>(testParam.Value));
|
||||
break;
|
||||
case AnimParam::Type::Int:
|
||||
return evaluateCondition(std::get<int>(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -83,22 +155,11 @@ namespace SHADE
|
|||
}
|
||||
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
/* AnimParam Functions */
|
||||
/* Helper Functions */
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
SHAnimationController::AnimParam::AnimParam(Type type)
|
||||
: ParamType { type }
|
||||
void SHAnimationController::changeNode(InstanceData& instData, Handle<Node> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
/// </summary>
|
||||
/// <param name="type">Type of AnimParam.</param>
|
||||
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<Node> Target;
|
||||
ConditionType Condition;
|
||||
AnimParam Param;
|
||||
std::string ParamName;
|
||||
Handle<Node> Target;
|
||||
|
||||
/*-------------------------------------------------------------------------------*/
|
||||
/* Usage Functions */
|
||||
/*-------------------------------------------------------------------------------*/
|
||||
/// <summary>
|
||||
/// Checks the condition of this Transition against an animation paramter.
|
||||
/// </summary>
|
||||
/// <param name="testParam">Parameter to test with.</param>
|
||||
/// <returns>Whether the condition passed.</returns>
|
||||
bool EvaluateCondition(const AnimParam& testParam) const noexcept;
|
||||
|
||||
private:
|
||||
/*-------------------------------------------------------------------------------*/
|
||||
/* Helper Functions */
|
||||
/*-------------------------------------------------------------------------------*/
|
||||
template<typename T>
|
||||
bool evaluateCondition(T value) const noexcept;
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
|
@ -167,5 +185,12 @@ namespace SHADE
|
|||
Handle<Node> startNode;
|
||||
std::vector<Handle<Node>> nodes;
|
||||
std::unordered_map<std::string, AnimParam::Type> parameters;
|
||||
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
/* Helper Functions */
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
void changeNode(InstanceData& instData, Handle<Node> newNode);
|
||||
};
|
||||
}
|
||||
|
||||
#include "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<typename T>
|
||||
bool SHAnimationController::Transition::evaluateCondition(T value) const noexcept
|
||||
{
|
||||
// Early failure if invalid data
|
||||
if (!std::holds_alternative<T>(Param.Value))
|
||||
return false;
|
||||
|
||||
// Get the value
|
||||
const T PARAM_VAL = std::get<T>(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<T>)
|
||||
{
|
||||
static constexpr T EPSILON = static_cast<T>(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<T>)
|
||||
{
|
||||
static constexpr T EPSILON = static_cast<T>(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;
|
||||
}
|
||||
}
|
|
@ -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 */
|
||||
|
|
Loading…
Reference in New Issue