From c8ebfde85b5dcb8c37d14d834ccacfd343de4bf5 Mon Sep 17 00:00:00 2001 From: D8H Date: Wed, 17 Jan 2024 18:32:14 +0100 Subject: [PATCH] Fix platformer characters turn back speed when the deceleration is greater than the acceleration (#6208) --- .../PlatformerObjectBehavior.cpp | 1 - .../platformerobjectruntimebehavior.ts | 28 +++++++++++++------ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Extensions/PlatformBehavior/PlatformerObjectBehavior.cpp b/Extensions/PlatformBehavior/PlatformerObjectBehavior.cpp index e4769a94637d..ad1855adc258 100644 --- a/Extensions/PlatformBehavior/PlatformerObjectBehavior.cpp +++ b/Extensions/PlatformBehavior/PlatformerObjectBehavior.cpp @@ -150,7 +150,6 @@ PlatformerObjectBehavior::GetProperties( properties["UseLegacyTrajectory"] .SetLabel(_("Use frame rate dependent trajectories (deprecated, it's " "recommended to leave this unchecked)")) - .SetGroup(_("Deprecated options")) .SetDeprecated() .SetValue(behaviorContent.GetBoolAttribute("useLegacyTrajectory", true) diff --git a/Extensions/PlatformBehavior/platformerobjectruntimebehavior.ts b/Extensions/PlatformBehavior/platformerobjectruntimebehavior.ts index 854a7f1555e7..d5936fd1a2a4 100644 --- a/Extensions/PlatformBehavior/platformerobjectruntimebehavior.ts +++ b/Extensions/PlatformBehavior/platformerobjectruntimebehavior.ts @@ -62,7 +62,7 @@ namespace gdjs { private _yGrabOffset: any; private _xGrabTolerance: any; - _useLegacyTrajectory: boolean = true; + _useLegacyTrajectory: boolean; _canGoDownFromJumpthru: boolean = false; @@ -355,13 +355,25 @@ namespace gdjs { private _updateSpeed(timeDelta: float): float { const previousSpeed = this._currentSpeed; - //Change the speed according to the player's input. - // @ts-ignore - if (this._leftKey) { - this._currentSpeed -= this._acceleration * timeDelta; - } - if (this._rightKey) { - this._currentSpeed += this._acceleration * timeDelta; + // Change the speed according to the player's input. + // TODO Give priority to the last key for faster reaction time. + if (this._leftKey !== this._rightKey) { + if (this._leftKey) { + if (this._currentSpeed <= 0) { + this._currentSpeed -= this._acceleration * timeDelta; + } else { + // Turn back at least as fast as it would stop. + this._currentSpeed -= + Math.max(this._acceleration, this._deceleration) * timeDelta; + } + } else if (this._rightKey) { + if (this._currentSpeed >= 0) { + this._currentSpeed += this._acceleration * timeDelta; + } else { + this._currentSpeed += + Math.max(this._acceleration, this._deceleration) * timeDelta; + } + } } //Take deceleration into account only if no key is pressed.