Add speed clamping and update player locomotion logic
This commit is contained in:
@@ -24,6 +24,14 @@ const IDLE_SPEED_EPSILON = 0.05;
|
|||||||
const VARIABLE_JUMP_HOLD_GRAVITY_FACTOR = 0.45;
|
const VARIABLE_JUMP_HOLD_GRAVITY_FACTOR = 0.45;
|
||||||
const VARIABLE_JUMP_RELEASE_VELOCITY_FACTOR = 0.45;
|
const VARIABLE_JUMP_RELEASE_VELOCITY_FACTOR = 0.45;
|
||||||
|
|
||||||
|
function clampPlanarSpeed(speed: number, maxSpeed: number): number {
|
||||||
|
if (maxSpeed <= 0) {
|
||||||
|
return speed;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Math.min(speed, maxSpeed);
|
||||||
|
}
|
||||||
|
|
||||||
function clampUnitInterval(value: number): number {
|
function clampUnitInterval(value: number): number {
|
||||||
return Math.max(0, Math.min(1, value));
|
return Math.max(0, Math.min(1, value));
|
||||||
}
|
}
|
||||||
@@ -332,28 +340,22 @@ export function stepPlayerLocomotion(
|
|||||||
!crouched &&
|
!crouched &&
|
||||||
currentlyGrounded &&
|
currentlyGrounded &&
|
||||||
!currentVolumeState.inWater;
|
!currentVolumeState.inWater;
|
||||||
const groundedRequestedPlanarSpeed =
|
const previousRequestedPlanarSpeed = Math.max(
|
||||||
options.movement.moveSpeed *
|
0,
|
||||||
(crouched
|
|
||||||
? options.movement.crouch.speedMultiplier
|
|
||||||
: sprinting
|
|
||||||
? options.movement.sprint.speedMultiplier
|
|
||||||
: 1);
|
|
||||||
const airborneRequestedPlanarSpeed = Math.max(
|
|
||||||
options.movement.moveSpeed,
|
|
||||||
options.previousLocomotionState?.requestedPlanarSpeed ?? 0
|
options.previousLocomotionState?.requestedPlanarSpeed ?? 0
|
||||||
);
|
);
|
||||||
const requestedPlanarSpeed =
|
const groundedRequestedPlanarSpeed = clampPlanarSpeed(
|
||||||
activeShape.mode !== "none" &&
|
options.movement.moveSpeed *
|
||||||
!currentVolumeState.inWater &&
|
(crouched
|
||||||
!currentlyGrounded
|
? options.movement.crouch.speedMultiplier
|
||||||
? airborneRequestedPlanarSpeed
|
: sprinting
|
||||||
: groundedRequestedPlanarSpeed;
|
? options.movement.sprint.speedMultiplier
|
||||||
const planarMotion = computePlanarMotion(
|
: 1),
|
||||||
options.movementYawRadians,
|
options.movement.maxSpeed
|
||||||
options.input,
|
);
|
||||||
requestedPlanarSpeed,
|
const airborneRequestedPlanarSpeed = clampPlanarSpeed(
|
||||||
options.dt
|
Math.max(options.movement.moveSpeed, previousRequestedPlanarSpeed),
|
||||||
|
options.movement.maxSpeed
|
||||||
);
|
);
|
||||||
const jumpTriggered =
|
const jumpTriggered =
|
||||||
options.movement.capabilities.jump &&
|
options.movement.capabilities.jump &&
|
||||||
@@ -361,12 +363,45 @@ export function stepPlayerLocomotion(
|
|||||||
(currentlyGrounded || coyoteTimeRemainingMs > 0) &&
|
(currentlyGrounded || coyoteTimeRemainingMs > 0) &&
|
||||||
!currentVolumeState.inWater &&
|
!currentVolumeState.inWater &&
|
||||||
activeShape.mode !== "none";
|
activeShape.mode !== "none";
|
||||||
|
const bufferedJumpTriggered =
|
||||||
|
jumpTriggered && !jumpJustPressed && jumpBufferRemainingMs > 0;
|
||||||
|
const jumpRequestedPlanarSpeedBase = clampPlanarSpeed(
|
||||||
|
Math.max(
|
||||||
|
options.movement.moveSpeed,
|
||||||
|
groundedRequestedPlanarSpeed,
|
||||||
|
previousRequestedPlanarSpeed
|
||||||
|
),
|
||||||
|
options.movement.maxSpeed
|
||||||
|
);
|
||||||
|
const jumpRequestedPlanarSpeed =
|
||||||
|
bufferedJumpTriggered && options.movement.jump.bunnyHop
|
||||||
|
? clampPlanarSpeed(
|
||||||
|
jumpRequestedPlanarSpeedBase *
|
||||||
|
(1 + options.movement.jump.bunnyHopBoost),
|
||||||
|
options.movement.maxSpeed
|
||||||
|
)
|
||||||
|
: jumpRequestedPlanarSpeedBase;
|
||||||
|
|
||||||
if (jumpTriggered) {
|
if (jumpTriggered) {
|
||||||
jumpBufferRemainingMs = 0;
|
jumpBufferRemainingMs = 0;
|
||||||
coyoteTimeRemainingMs = 0;
|
coyoteTimeRemainingMs = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const requestedPlanarSpeed =
|
||||||
|
activeShape.mode !== "none" && !currentVolumeState.inWater
|
||||||
|
? jumpTriggered
|
||||||
|
? jumpRequestedPlanarSpeed
|
||||||
|
: currentlyGrounded
|
||||||
|
? groundedRequestedPlanarSpeed
|
||||||
|
: airborneRequestedPlanarSpeed
|
||||||
|
: groundedRequestedPlanarSpeed;
|
||||||
|
const planarMotion = computePlanarMotion(
|
||||||
|
options.movementYawRadians,
|
||||||
|
options.input,
|
||||||
|
requestedPlanarSpeed,
|
||||||
|
options.dt
|
||||||
|
);
|
||||||
|
|
||||||
const groundedPlanarMotion =
|
const groundedPlanarMotion =
|
||||||
currentlyGrounded && !jumpTriggered
|
currentlyGrounded && !jumpTriggered
|
||||||
? alignPlanarMotionToGround(
|
? alignPlanarMotionToGround(
|
||||||
|
|||||||
Reference in New Issue
Block a user