From 19615c69765e3b4577c29bd009ea19c6d60e5442 Mon Sep 17 00:00:00 2001 From: obligaron Date: Thu, 13 May 2021 09:37:55 +0200 Subject: [PATCH] AnimationInfo: Add GetAnimationProgress() --- Source/engine/animationinfo.cpp | 17 +++++++++++++++++ Source/engine/animationinfo.h | 5 +++++ 2 files changed, 22 insertions(+) diff --git a/Source/engine/animationinfo.cpp b/Source/engine/animationinfo.cpp index 7f2ee13ea..52ee177d8 100644 --- a/Source/engine/animationinfo.cpp +++ b/Source/engine/animationinfo.cpp @@ -52,6 +52,23 @@ int AnimationInfo::GetFrameToUseForRendering() const return absoluteAnimationFrame; } +float AnimationInfo::GetAnimationProgress() const +{ + if (RelevantFramesForDistributing <= 0) { + // This logic is used if animation distrubtion is not active (see GetFrameToUseForRendering). + // In this case the variables calculated with animation distribution are not initialized and we have to calculate them on the fly with the given informations. + float ticksPerFrame = (DelayLen + 1); + float totalTicksForCurrentAnimationSequence = gfProgressToNextGameTick + (float)CurrentFrame + (DelayCounter / ticksPerFrame); + float fAnimationFraction = totalTicksForCurrentAnimationSequence / ((float)NumberOfFrames * ticksPerFrame); + return fAnimationFraction; + } + + float totalTicksForCurrentAnimationSequence = gfProgressToNextGameTick + (float)TicksSinceSequenceStarted; + float fProgressInAnimationFrames = totalTicksForCurrentAnimationSequence * TickModifier; + float fAnimationFraction = fProgressInAnimationFrames / (float)NumberOfFrames; + return fAnimationFraction; +} + void AnimationInfo::SetNewAnimation(byte *pData, int numberOfFrames, int delayLen, AnimationDistributionFlags flags /*= AnimationDistributionFlags::None*/, int numSkippedFrames /*= 0*/, int distributeFramesBeforeFrame /*= 0*/) { if ((flags & AnimationDistributionFlags::RepeatedAction) == AnimationDistributionFlags::RepeatedAction && distributeFramesBeforeFrame != 0 && NumberOfFrames == numberOfFrames && CurrentFrame >= distributeFramesBeforeFrame && CurrentFrame != NumberOfFrames) { diff --git a/Source/engine/animationinfo.h b/Source/engine/animationinfo.h index bbb3220bc..b1db43b47 100644 --- a/Source/engine/animationinfo.h +++ b/Source/engine/animationinfo.h @@ -63,6 +63,11 @@ public: */ int GetFrameToUseForRendering() const; + /** + * @brief Calculates the progress of the current animation as a fraction (0.0f to 1.0f) + */ + float GetAnimationProgress() const; + /** * @brief Sets the new Animation with all relevant information for rendering * @param pData Pointer to Animation Data