@@ -20,6 +20,7 @@ import type { ClientSettings, InputState } from '../types.ts'
2020
2121const WORLD_UP = vec3 ( 0 , 1 , 0 )
2222const MOVE_SPEED = 4.75
23+ const SPRINT_SPEED = 6.18
2324const JUMP_VELOCITY = 7.5
2425const GRAVITY = 24
2526const BASE_MOUSE_SENSITIVITY = 0.0025
@@ -41,10 +42,13 @@ export class PlayerController {
4142 private grounded = false
4243 private previousJumpDown = false
4344 private timeSinceJumpPress = Number . POSITIVE_INFINITY
45+ private previousForwardDown = false
46+ private timeSinceForwardPress = Number . POSITIVE_INFINITY
4447 private fovDegrees = DEFAULT_FOV_DEGREES
4548 private mouseSensitivityPercent = 100
4649 public gamemode : PlayerGamemode = 0
4750 public flying = false
51+ public sprinting = false
4852
4953 public state : PlayerState = {
5054 position : [ 0 , 10 , 0 ] ,
@@ -120,6 +124,7 @@ export class PlayerController {
120124
121125 public update ( input : InputState , deltaSeconds : number , world : VoxelWorld ) : void {
122126 this . timeSinceJumpPress += deltaSeconds
127+ this . timeSinceForwardPress += deltaSeconds
123128 const jumpPressed = input . moveUp && ! this . previousJumpDown
124129 if ( this . gamemode === 1 && jumpPressed ) {
125130 if ( this . timeSinceJumpPress <= DOUBLE_TAP_WINDOW_SECONDS ) {
@@ -135,9 +140,23 @@ export class PlayerController {
135140 if ( this . flying ) {
136141 this . updateFlying ( input , deltaSeconds , world )
137142 this . previousJumpDown = input . moveUp
143+ this . previousForwardDown = input . moveForward
144+ this . sprinting = false
138145 return
139146 }
140147
148+ const forwardPressed = input . moveForward && ! this . previousForwardDown
149+ if ( forwardPressed ) {
150+ if ( this . timeSinceForwardPress <= DOUBLE_TAP_WINDOW_SECONDS ) {
151+ this . sprinting = true
152+ }
153+ this . timeSinceForwardPress = 0
154+ }
155+ if ( ! input . moveForward ) {
156+ this . sprinting = false
157+ }
158+ this . previousForwardDown = input . moveForward
159+
141160 const forward = this . getWalkForwardVector ( )
142161 const right = normalizeVec3 ( crossVec3 ( forward , WORLD_UP ) )
143162 let movement = vec3 ( )
@@ -158,8 +177,9 @@ export class PlayerController {
158177
159178 this . verticalVelocity -= GRAVITY * deltaSeconds
160179
161- position = this . moveAxis ( world , position , 'x' , normalized . x * MOVE_SPEED * deltaSeconds )
162- position = this . moveAxis ( world , position , 'z' , normalized . z * MOVE_SPEED * deltaSeconds )
180+ const speed = this . sprinting ? SPRINT_SPEED : MOVE_SPEED
181+ position = this . moveAxis ( world , position , 'x' , normalized . x * speed * deltaSeconds )
182+ position = this . moveAxis ( world , position , 'z' , normalized . z * speed * deltaSeconds )
163183
164184 const beforeVertical = position . y
165185 position = this . moveAxis ( world , position , 'y' , this . verticalVelocity * deltaSeconds )
0 commit comments