From 8c0ad47921354512826977e677f38047054d7d7f Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Fri, 10 Mar 2023 14:55:49 +0800 Subject: [PATCH] Added timescale for animation system --- Assets/Scenes/anim.shade | 4 +- Assets/Scripts/AnimTest.cs | 9 ++++ .../src/Animation/SHAnimationSystem.cpp | 1 + .../src/Animation/SHAnimationSystem.h | 17 +++++++ .../src/Graphics/AnimationSystem.cxx | 37 +++++++++++++++ .../src/Graphics/AnimationSystem.hxx | 47 +++++++++++++++++++ 6 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 SHADE_Managed/src/Graphics/AnimationSystem.cxx create mode 100644 SHADE_Managed/src/Graphics/AnimationSystem.hxx diff --git a/Assets/Scenes/anim.shade b/Assets/Scenes/anim.shade index 986195f6..5c9c7163 100644 --- a/Assets/Scenes/anim.shade +++ b/Assets/Scenes/anim.shade @@ -38,6 +38,7 @@ idleClip: 227450439 runClip: 229125027 pickUpClip: 219605278 + controlAniSys: true - EID: 1 Name: Default IsActive: true @@ -79,4 +80,5 @@ fullClip: 231416496 idleClip: 0 runClip: 0 - pickUpClip: 0 \ No newline at end of file + pickUpClip: 0 + controlAniSys: false \ No newline at end of file diff --git a/Assets/Scripts/AnimTest.cs b/Assets/Scripts/AnimTest.cs index 584d046f..b0761476 100644 --- a/Assets/Scripts/AnimTest.cs +++ b/Assets/Scripts/AnimTest.cs @@ -13,6 +13,8 @@ namespace SHADE.Test private AnimationClipAsset runClip; [SerializeField] private AnimationClipAsset pickUpClip; + [SerializeField] + private bool controlAniSys = false; #endregion #region Components @@ -52,6 +54,13 @@ namespace SHADE.Test if (pickUpClip) playFunc(pickUpClip); } + + // Play and pause + if (controlAniSys && Input.GetKeyUp(Input.KeyCode.Space)) + { + AnimationSystem.TimeScale = AnimationSystem.TimeScale > 0.0f ? 0.0f + : AnimationSystem.DefaultTimeScale; + } } #endregion } diff --git a/SHADE_Engine/src/Animation/SHAnimationSystem.cpp b/SHADE_Engine/src/Animation/SHAnimationSystem.cpp index 6f41e2aa..e38d2287 100644 --- a/SHADE_Engine/src/Animation/SHAnimationSystem.cpp +++ b/SHADE_Engine/src/Animation/SHAnimationSystem.cpp @@ -32,6 +32,7 @@ namespace SHADE void SHAnimationSystem::UpdateRoutine::Execute(double dt) noexcept { auto& animators = SHComponentManager::GetDense(); + dt *= reinterpret_cast(system)->TimeScale; for (auto& animator : animators) { animator.Update(dt); diff --git a/SHADE_Engine/src/Animation/SHAnimationSystem.h b/SHADE_Engine/src/Animation/SHAnimationSystem.h index 9c2cd073..fd972e47 100644 --- a/SHADE_Engine/src/Animation/SHAnimationSystem.h +++ b/SHADE_Engine/src/Animation/SHAnimationSystem.h @@ -42,6 +42,23 @@ namespace SHADE void Execute(double dt) noexcept override final; }; + /*---------------------------------------------------------------------------------*/ + /* Constants */ + /*---------------------------------------------------------------------------------*/ + /// + /// Default time scale used by the system. + /// + static constexpr double DEFAULT_TIME_SCALE = 1.0; + + /*---------------------------------------------------------------------------------*/ + /* Public Data Members */ + /*---------------------------------------------------------------------------------*/ + /// + /// Used by the system to multiply the given delta time to scale the animation + /// speed. + /// + double TimeScale = DEFAULT_TIME_SCALE; + /*---------------------------------------------------------------------------------*/ /* Constructors */ /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Managed/src/Graphics/AnimationSystem.cxx b/SHADE_Managed/src/Graphics/AnimationSystem.cxx new file mode 100644 index 00000000..8ed11c28 --- /dev/null +++ b/SHADE_Managed/src/Graphics/AnimationSystem.cxx @@ -0,0 +1,37 @@ +/************************************************************************************//*! +\file AnimationSystem.cxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Mar 10, 2023 +\brief Contains the definition of the functions of the managed AnimationSystem + static class. + + Note: This file is written in C++17/CLI. + +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. +*//*************************************************************************************/ +// Precompiled Headers +#include "SHpch.h" +// Primary Header +#include "AnimationSystem.hxx" +// External Dependencies +#include "ECS_Base/Managers/SHSystemManager.h" + +namespace SHADE +{ + /*---------------------------------------------------------------------------------*/ + /* Properties */ + /*---------------------------------------------------------------------------------*/ + double AnimationSystem::TimeScale::get() + { + auto sys = SHSystemManager::GetSystem(); + return sys->TimeScale; + } + void AnimationSystem::TimeScale::set(double value) + { + auto sys = SHSystemManager::GetSystem(); + sys->TimeScale = value; + } +} diff --git a/SHADE_Managed/src/Graphics/AnimationSystem.hxx b/SHADE_Managed/src/Graphics/AnimationSystem.hxx new file mode 100644 index 00000000..fe3bcd59 --- /dev/null +++ b/SHADE_Managed/src/Graphics/AnimationSystem.hxx @@ -0,0 +1,47 @@ +/************************************************************************************//*! +\file AnimationSystem.hxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Mar 10, 2023 +\brief Contains the definition of the managed AnimationSystem static class. + + Note: This file is written in C++17/CLI. + +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 "Animation/SHAnimationSystem.h" + +namespace SHADE +{ + /// + /// Static class for interfacing with the animation system. + /// + public ref class AnimationSystem abstract sealed + { + public: + /*-----------------------------------------------------------------------------*/ + /* Constants */ + /*-----------------------------------------------------------------------------*/ + /// + /// Default time scale used by the system. + /// + literal double DefaultTimeScale = SHAnimationSystem::DEFAULT_TIME_SCALE; + + /*-----------------------------------------------------------------------------*/ + /* Properties */ + /*-----------------------------------------------------------------------------*/ + /// + /// Used by the system to multiply the given delta time to scale the animation + /// speed. + /// + static property double TimeScale + { + double get(); + void set(double value); + } + }; +}