Skip to content

Commit

Permalink
Add sprinting
Browse files Browse the repository at this point in the history
When the 'sprint' key is held down, double the maximum speed. Split
the 'maxSpeed' option into walkMaxSpeed (default half) and runMaxSpeed.
  • Loading branch information
deathcap committed Nov 30, 2013
1 parent e13f380 commit 0b85f90
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ when paused.
```javascript
// default values are in terms of 1 voxel width
{ speed: Number(0.0032) // starting speed
, maxSpeed: Number(0.0112) // max speed
, walkMaxSpeed: Number(0.0056) // max walking speed
, runkMaxSpeed: Number(0.0112) // max running speed
, jumpMaxSpeed: Number(0.016) // max jump speed
, jumpMaxTimer: Number(200) // maximum amount of time jump will be applied in MS
, jumpSpeed: Number(0.004) // starting jump speed
Expand Down
24 changes: 13 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ function Control(state, opts) {
this._roll_target =
this._target = null
this.speed = opts.speed || 0.0032
this.max_speed = opts.maxSpeed || 0.0112
this.walk_max_speed = opts.walkMaxSpeed || 0.0056
this.run_max_speed = opts.runMaxSpeed || 0.0112
this.jump_max_speed = opts.jumpMaxSpeed || 0.016
this.jump_max_timer = opts.jumpTimer || 200
this.jump_speed = opts.jumpSpeed || 0.004
Expand Down Expand Up @@ -79,8 +80,9 @@ proto.tick = function(dt) {
, speed = this.speed
, jump_speed = this.jump_speed
, jump_speed_move = this.jump_speed_move
, okay_z = abs(target.velocity.z) < this.max_speed
, okay_x = abs(target.velocity.x) < this.max_speed
, max_speed = this.state.sprint ? this.run_max_speed : this.walk_max_speed
, okay_z = abs(target.velocity.z) < max_speed
, okay_x = abs(target.velocity.x) < max_speed
, at_rest = target.atRestY()

if(!this._target) return
Expand All @@ -91,11 +93,11 @@ proto.tick = function(dt) {
this.z_accel_timer = max(0, this.z_accel_timer - dt)
}
if(state.backward) {
if(target.velocity.z < this.max_speed)
target.velocity.z = max(min(this.max_speed, move_speed * dt * this.acceleration(this.z_accel_timer, this.accel_max_timer)), target.velocity.z)
if(target.velocity.z < max_speed)
target.velocity.z = max(min(max_speed, move_speed * dt * this.acceleration(this.z_accel_timer, this.accel_max_timer)), target.velocity.z)
} else if(state.forward) {
if(target.velocity.z > -this.max_speed)
target.velocity.z = min(max(-this.max_speed, -move_speed * dt * this.acceleration(this.z_accel_timer, this.accel_max_timer)), target.velocity.z)
if(target.velocity.z > -max_speed)
target.velocity.z = min(max(-max_speed, -move_speed * dt * this.acceleration(this.z_accel_timer, this.accel_max_timer)), target.velocity.z)
} else {
this.z_accel_timer = this.accel_max_timer

Expand All @@ -107,11 +109,11 @@ proto.tick = function(dt) {
}

if(state.right) {
if(target.velocity.x < this.max_speed)
target.velocity.x = max(min(this.max_speed, move_speed * dt * this.acceleration(this.x_accel_timer, this.accel_max_timer)), target.velocity.x)
if(target.velocity.x < max_speed)
target.velocity.x = max(min(max_speed, move_speed * dt * this.acceleration(this.x_accel_timer, this.accel_max_timer)), target.velocity.x)
} else if(state.left) {
if(target.velocity.x > -this.max_speed)
target.velocity.x = min(max(-this.max_speed, -move_speed * dt * this.acceleration(this.x_accel_timer, this.accel_max_timer)), target.velocity.x)
if(target.velocity.x > -max_speed)
target.velocity.x = min(max(-max_speed, -move_speed * dt * this.acceleration(this.x_accel_timer, this.accel_max_timer)), target.velocity.x)
} else {
this.x_accel_timer = this.accel_max_timer
}
Expand Down

1 comment on commit 0b85f90

@deathcap
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.