Implement jump buffering
This commit is contained in:
parent
e98b730465
commit
9a3da85999
1 changed files with 34 additions and 13 deletions
47
game/aw.go
47
game/aw.go
|
@ -23,6 +23,7 @@ type Awakeman struct {
|
||||||
vx, vy float64
|
vx, vy float64
|
||||||
facingLeft bool
|
facingLeft bool
|
||||||
coyoteTimer int
|
coyoteTimer int
|
||||||
|
jumpBuffer int
|
||||||
noclip bool
|
noclip bool
|
||||||
|
|
||||||
animIdleLeft, animIdleRight, animRunLeft, animRunRight *engine.Anim
|
animIdleLeft, animIdleRight, animRunLeft, animRunRight *engine.Anim
|
||||||
|
@ -40,10 +41,13 @@ func (aw *Awakeman) Update() error {
|
||||||
if err := upd(); err != nil {
|
if err := upd(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update the camera
|
||||||
aw.camera.Zoom = 1
|
aw.camera.Zoom = 1
|
||||||
if ebiten.IsKeyPressed(ebiten.KeyShift) {
|
if ebiten.IsKeyPressed(ebiten.KeyShift) {
|
||||||
aw.camera.Zoom = 2
|
aw.camera.Zoom = 2
|
||||||
}
|
}
|
||||||
|
// aw.Pos is top-left corner, so add half size to get centre
|
||||||
aw.camera.Centre = aw.Pos.Add(aw.Size.Div(2))
|
aw.camera.Centre = aw.Pos.Add(aw.Size.Div(2))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -66,13 +70,14 @@ func (aw *Awakeman) noclipUpdate() error {
|
||||||
|
|
||||||
func (aw *Awakeman) realUpdate() error {
|
func (aw *Awakeman) realUpdate() error {
|
||||||
const (
|
const (
|
||||||
ε = 0.2
|
ε = 0.2
|
||||||
restitution = -0.3
|
restitution = -0.3
|
||||||
gravity = 0.3
|
gravity = 0.3
|
||||||
airResistance = -0.01 // ⇒ terminal velocity = 30
|
airResistance = -0.01 // ⇒ terminal velocity = 30
|
||||||
jumpVelocity = -4.2
|
jumpVelocity = -4.2
|
||||||
runVelocity = 1.4
|
runVelocity = 1.4
|
||||||
coyoteTime = 5
|
coyoteTime = 5
|
||||||
|
jumpBufferTime = 5
|
||||||
)
|
)
|
||||||
|
|
||||||
// High-school physics time! Under constant acceleration:
|
// High-school physics time! Under constant acceleration:
|
||||||
|
@ -91,23 +96,40 @@ func (aw *Awakeman) realUpdate() error {
|
||||||
if aw.CollidesAt(aw.Pos.Add(image.Pt(0, 1))) {
|
if aw.CollidesAt(aw.Pos.Add(image.Pt(0, 1))) {
|
||||||
// Not falling.
|
// Not falling.
|
||||||
// Instantly decelerate (AW absorbs all kinetic E in legs, or something)
|
// Instantly decelerate (AW absorbs all kinetic E in legs, or something)
|
||||||
aw.vy = 0
|
if aw.jumpBuffer > 0 {
|
||||||
aw.coyoteTimer = coyoteTime
|
// Tried to jump recently -- so jump
|
||||||
|
aw.vy = jumpVelocity
|
||||||
|
aw.jumpBuffer = 0
|
||||||
|
} else {
|
||||||
|
// Can jump now or soon.
|
||||||
|
aw.vy = 0
|
||||||
|
aw.coyoteTimer = coyoteTime
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Falling. v = v_0 + a, and a = gravity + airResistance(v_0)
|
// Falling. v = v_0 + a, and a = gravity + airResistance(v_0)
|
||||||
aw.vy += gravity + airResistance*aw.vy
|
aw.vy += gravity + airResistance*aw.vy
|
||||||
if aw.coyoteTimer > 0 {
|
if aw.coyoteTimer > 0 {
|
||||||
aw.coyoteTimer--
|
aw.coyoteTimer--
|
||||||
}
|
}
|
||||||
|
if aw.jumpBuffer > 0 {
|
||||||
|
aw.jumpBuffer--
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle controls
|
// Handle controls
|
||||||
|
|
||||||
// NB: spacebar sometimes does things on web pages (scrolls down)
|
// NB: spacebar sometimes does things on web pages (scrolls down)
|
||||||
if aw.coyoteTimer > 0 && (inpututil.IsKeyJustPressed(ebiten.KeySpace) || inpututil.IsKeyJustPressed(ebiten.KeyZ)) {
|
if inpututil.IsKeyJustPressed(ebiten.KeySpace) || inpututil.IsKeyJustPressed(ebiten.KeyZ) {
|
||||||
// Jump. One frame of a = jumpVelocity (ignoring any gravity already applied this tick).
|
// On ground or recently on ground?
|
||||||
aw.vy = jumpVelocity
|
if aw.coyoteTimer > 0 {
|
||||||
|
// Jump. One frame of a = jumpVelocity (ignoring any gravity already applied this tick).
|
||||||
|
aw.vy += jumpVelocity
|
||||||
|
} else {
|
||||||
|
// Buffer the jump in case aw hits the ground soon.
|
||||||
|
aw.jumpBuffer = jumpBufferTime
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
// Left and right
|
||||||
switch {
|
switch {
|
||||||
case ebiten.IsKeyPressed(ebiten.KeyLeft) || ebiten.IsKeyPressed(ebiten.KeyA):
|
case ebiten.IsKeyPressed(ebiten.KeyLeft) || ebiten.IsKeyPressed(ebiten.KeyA):
|
||||||
aw.vx = -runVelocity
|
aw.vx = -runVelocity
|
||||||
|
@ -135,7 +157,6 @@ func (aw *Awakeman) realUpdate() error {
|
||||||
aw.vy = 0
|
aw.vy = 0
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
// aw.Pos is top-left corner, so add half size to get centre
|
|
||||||
return aw.Sprite.Update()
|
return aw.Sprite.Update()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue