Skip to content

Commit 70eb7ed

Browse files
committed
fix(sdk): async animation can now be resumed after being paused and also works backwards
1 parent 812eb48 commit 70eb7ed

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

engine/core/src/main/resources/view/core/Drawer.js

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -449,10 +449,15 @@ export class Drawer {
449449

450450
updateScene (scope, question, frames, frameNumber, progress, speed, reason, demo, force) {
451451
const parsedFrame = frames[frameNumber]
452-
if (!force && this.stepByStepAnimateSpeed && this.checkSteppedToNextFrame(scope, parsedFrame)) {
453-
this.startAsynchronousAnimation(scope, progress, parsedFrame)
454-
return
455-
} else if (!force) {
452+
if (!force && this.stepByStepAnimateSpeed) {
453+
if (this.checkSteppedToNextFrame(scope, parsedFrame, progress)) {
454+
this.startAsynchronousAnimation(scope, parsedFrame)
455+
return
456+
} else if (this.checkSteppedToPreviousFrame(scope, parsedFrame, progress)) {
457+
const reversed = true
458+
this.startAsynchronousAnimation(scope, scope.currentFrame, reversed)
459+
return
460+
}
456461
scope.targetProgress = null
457462
}
458463

@@ -476,14 +481,27 @@ export class Drawer {
476481
}
477482
}
478483

479-
startAsynchronousAnimation (scope, progress, currentFrame) {
480-
scope.targetProgress = progress
481-
scope.currentProgress = 0
482-
scope.currentFrame = currentFrame
484+
startAsynchronousAnimation (scope, frameToAnimate, reversed = false) {
485+
scope.targetProgress = reversed ? 0 : 1
486+
if (!reversed && scope.currentProgress === 1) {
487+
scope.currentProgress = 0
488+
}
489+
scope.currentFrame = frameToAnimate
490+
scope.reverseAsynchronousAnimation = reversed
491+
}
492+
493+
checkStepped (scope, selectedFrame, progress) {
494+
return scope.currentFrame && this.speed === 0 && progress === 1
495+
}
496+
497+
checkSteppedToNextFrame (scope, selectedFrame, progress) {
498+
return this.checkStepped(scope, selectedFrame, progress) &&
499+
(scope.currentFrame === selectedFrame.previous || (scope.currentFrame === selectedFrame && scope.currentProgress !== progress))
483500
}
484501

485-
checkSteppedToNextFrame (scope, parsedFrame) {
486-
return scope.currentFrame && scope.currentFrame !== parsedFrame && scope.currentFrame === parsedFrame.previous && this.speed === 0
502+
checkSteppedToPreviousFrame (scope, selectedFrame, progress) {
503+
return this.checkStepped(scope, selectedFrame, progress) &&
504+
scope.currentFrame.previous === selectedFrame
487505
}
488506

489507
initEndScene (scope, failure) {
@@ -527,9 +545,27 @@ export class Drawer {
527545
}
528546

529547
if (this.stepByStepAnimateSpeed && this.isAsynchronousAnimationOngoing(scope)) {
530-
var p = scope.currentProgress + step / 200 * this.getFrameSpeed(this.currentFrame) * this.stepByStepAnimateSpeed
531-
p = Math.min(scope.targetProgress, p)
532-
this.updateScene(this.scope, this.question, this.frames, this.currentFrame, p, this.speed, this.reasons[this.currentFrame], false, true)
548+
let frameToShow = this.currentFrame
549+
let progressToShow
550+
551+
if (scope.reverseAsynchronousAnimation) {
552+
frameToShow++
553+
}
554+
555+
const progressDelta = step / 200 * this.getFrameSpeed(frameToShow) * this.stepByStepAnimateSpeed
556+
557+
if (scope.reverseAsynchronousAnimation) {
558+
progressToShow = scope.currentProgress - progressDelta
559+
if (progressToShow <= 0) {
560+
frameToShow = this.currentFrame
561+
progressToShow = 1
562+
scope.targetProgress = 1
563+
}
564+
} else {
565+
progressToShow = scope.currentProgress + progressDelta
566+
progressToShow = Math.min(scope.targetProgress, progressToShow)
567+
}
568+
this.updateScene(this.scope, this.question, this.frames, frameToShow, progressToShow, this.speed, this.reasons[frameToShow], false, true)
533569
}
534570

535571
for (const moduleName in this.modules) {

playground/misc/misc-3-release-notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The CodinGame SDK is regularly updated and improved. This document lets you know
77
### 🐞 Bug fix
88

99
- Fixed choppy animation when launching games with a `stepByStepAnimateSpeed`
10+
- Fixed situations in which stepping through a game with a `stepByStepAnimateSpeed` would not be animated.
1011

1112
## 3.11.0
1213

0 commit comments

Comments
 (0)