From b15e08fc3fbfd69ce70e1b6374af3973a1024373 Mon Sep 17 00:00:00 2001 From: ephphatha Date: Mon, 11 Oct 2021 00:29:03 +1100 Subject: [PATCH] Deduplicate logic in GetAnimationProgress --- Source/engine/animationinfo.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Source/engine/animationinfo.cpp b/Source/engine/animationinfo.cpp index 0fd07017f..9cfc3e59c 100644 --- a/Source/engine/animationinfo.cpp +++ b/Source/engine/animationinfo.cpp @@ -59,19 +59,20 @@ int AnimationInfo::GetFrameToUseForRendering() const float AnimationInfo::GetAnimationProgress() const { + int ticksSinceSequenceStarted = TicksSinceSequenceStarted; + float tickModifier = TickModifier; + if (RelevantFramesForDistributing <= 0) { - // This logic is used if animation distrubtion is not active (see GetFrameToUseForRendering). + // This logic is used if animation distribution 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 information. - int passedTicks = ((CurrentFrame - 1) * TicksPerFrame) + TickCounterOfCurrentFrame; - float totalTicksForCurrentAnimationSequence = GetProgressToNextGameTick() + (float)passedTicks; - float fAnimationFraction = totalTicksForCurrentAnimationSequence / (float)(NumberOfFrames * TicksPerFrame); - return fAnimationFraction; + ticksSinceSequenceStarted = ((CurrentFrame - 1) * TicksPerFrame) + TickCounterOfCurrentFrame; + tickModifier = 1.f / TicksPerFrame; } - float totalTicksForCurrentAnimationSequence = GetProgressToNextGameTick() + TicksSinceSequenceStarted; - float fProgressInAnimationFrames = totalTicksForCurrentAnimationSequence * TickModifier; - float fAnimationFraction = fProgressInAnimationFrames / NumberOfFrames; - return fAnimationFraction; + float totalTicksForCurrentAnimationSequence = GetProgressToNextGameTick() + ticksSinceSequenceStarted; + float progressInAnimationFrames = totalTicksForCurrentAnimationSequence * tickModifier; + float animationFraction = progressInAnimationFrames / NumberOfFrames; + return animationFraction; } void AnimationInfo::SetNewAnimation(const CelSprite *celSprite, int numberOfFrames, int ticksPerFrame, AnimationDistributionFlags flags /*= AnimationDistributionFlags::None*/, int numSkippedFrames /*= 0*/, int distributeFramesBeforeFrame /*= 0*/)