2021-08-05 12:26:41 +10:00
|
|
|
package game
|
|
|
|
|
|
|
|
import (
|
2021-08-05 12:33:23 +10:00
|
|
|
"encoding/gob"
|
2021-08-05 12:26:41 +10:00
|
|
|
"image"
|
2021-08-12 15:55:42 +10:00
|
|
|
"math"
|
2021-08-05 12:26:41 +10:00
|
|
|
|
|
|
|
"drjosh.dev/gurgle/engine"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
|
|
|
)
|
|
|
|
|
2021-08-27 14:13:55 +10:00
|
|
|
var _ interface {
|
|
|
|
engine.Identifier
|
2021-08-28 17:50:54 +10:00
|
|
|
engine.Drawer // provided by Sprite
|
2021-08-27 14:13:55 +10:00
|
|
|
engine.Disabler
|
|
|
|
engine.Hider // provided by Sprite
|
|
|
|
engine.Prepper
|
|
|
|
engine.Scanner
|
|
|
|
engine.Updater
|
|
|
|
} = &Awakeman{}
|
2021-08-25 17:22:22 +10:00
|
|
|
|
2021-08-05 12:33:23 +10:00
|
|
|
func init() {
|
2021-08-25 15:04:38 +10:00
|
|
|
gob.Register(&Awakeman{})
|
2021-08-05 12:33:23 +10:00
|
|
|
}
|
|
|
|
|
2021-08-05 12:26:41 +10:00
|
|
|
type Awakeman struct {
|
2021-08-25 17:22:22 +10:00
|
|
|
engine.Disabled
|
2021-08-05 12:26:41 +10:00
|
|
|
engine.Sprite
|
|
|
|
|
2021-08-08 22:07:55 +10:00
|
|
|
CameraID string
|
2021-08-24 19:33:21 +10:00
|
|
|
ToastID string
|
2021-08-08 22:07:55 +10:00
|
|
|
|
|
|
|
camera *engine.Camera
|
2021-08-24 19:33:21 +10:00
|
|
|
toast *engine.DebugToast
|
2021-08-07 21:24:15 +10:00
|
|
|
vx, vy float64
|
|
|
|
facingLeft bool
|
|
|
|
coyoteTimer int
|
2021-08-20 13:09:26 +10:00
|
|
|
jumpBuffer int
|
2021-08-18 19:17:56 +10:00
|
|
|
noclip bool
|
2021-08-05 12:26:41 +10:00
|
|
|
|
2021-08-23 20:28:49 +10:00
|
|
|
animIdleLeft, animIdleRight, animRunLeft, animRunRight, animWalkLeft, animWalkRight *engine.Anim
|
2021-08-05 12:26:41 +10:00
|
|
|
}
|
|
|
|
|
2021-08-26 11:31:39 +10:00
|
|
|
// Ident returns "awakeman". There should be only one!
|
|
|
|
func (aw *Awakeman) Ident() string { return "awakeman" }
|
|
|
|
|
2021-08-05 12:26:41 +10:00
|
|
|
func (aw *Awakeman) Update() error {
|
2021-08-25 17:22:22 +10:00
|
|
|
if aw.Disabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-18 19:17:56 +10:00
|
|
|
// TODO: better cheat for noclip
|
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyN) {
|
|
|
|
aw.noclip = !aw.noclip
|
2021-08-24 19:33:21 +10:00
|
|
|
if aw.toast != nil {
|
|
|
|
if aw.noclip {
|
|
|
|
aw.toast.Toast("noclip enabled")
|
|
|
|
} else {
|
|
|
|
aw.toast.Toast("noclip disabled")
|
|
|
|
}
|
|
|
|
}
|
2021-08-18 19:17:56 +10:00
|
|
|
}
|
|
|
|
upd := aw.realUpdate
|
|
|
|
if aw.noclip {
|
|
|
|
upd = aw.noclipUpdate
|
|
|
|
}
|
|
|
|
if err := upd(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-20 13:09:26 +10:00
|
|
|
|
|
|
|
// Update the camera
|
2021-08-18 19:17:56 +10:00
|
|
|
aw.camera.Zoom = 1
|
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyShift) {
|
|
|
|
aw.camera.Zoom = 2
|
|
|
|
}
|
2021-08-20 13:09:26 +10:00
|
|
|
// aw.Pos is top-left corner, so add half size to get centre
|
2021-08-18 19:17:56 +10:00
|
|
|
aw.camera.Centre = aw.Pos.Add(aw.Size.Div(2))
|
2021-08-27 14:12:31 +10:00
|
|
|
return aw.Sprite.Update()
|
2021-08-18 19:17:56 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
func (aw *Awakeman) noclipUpdate() error {
|
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyUp) {
|
|
|
|
aw.Pos.Y--
|
|
|
|
}
|
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyDown) {
|
|
|
|
aw.Pos.Y++
|
|
|
|
}
|
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyLeft) {
|
|
|
|
aw.Pos.X--
|
|
|
|
}
|
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyRight) {
|
|
|
|
aw.Pos.X++
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (aw *Awakeman) realUpdate() error {
|
2021-08-05 12:26:41 +10:00
|
|
|
const (
|
2021-08-20 13:09:26 +10:00
|
|
|
ε = 0.2
|
|
|
|
restitution = -0.3
|
|
|
|
gravity = 0.3
|
|
|
|
airResistance = -0.01 // ⇒ terminal velocity = 30
|
|
|
|
jumpVelocity = -4.2
|
|
|
|
runVelocity = 1.4
|
|
|
|
coyoteTime = 5
|
|
|
|
jumpBufferTime = 5
|
2021-08-05 12:26:41 +10:00
|
|
|
)
|
|
|
|
|
2021-08-12 15:37:09 +10:00
|
|
|
// High-school physics time! Under constant acceleration:
|
|
|
|
// v = v_0 + a*t
|
|
|
|
// and
|
|
|
|
// s = t * (v_0 + v) / 2
|
|
|
|
// (note t is in ticks and s is in world units)
|
|
|
|
// and since we get one Update per tick (t = 1),
|
|
|
|
// v = v_0 + a,
|
|
|
|
// and
|
|
|
|
// s = (v_0 + v) / 2.
|
|
|
|
// Capture current v_0 to use later.
|
|
|
|
ux, uy := aw.vx, aw.vy
|
|
|
|
|
2021-08-12 15:55:42 +10:00
|
|
|
// Has traction?
|
2021-08-12 16:14:51 +10:00
|
|
|
if aw.CollidesAt(aw.Pos.Add(image.Pt(0, 1))) {
|
2021-08-14 17:22:14 +10:00
|
|
|
// Not falling.
|
|
|
|
// Instantly decelerate (AW absorbs all kinetic E in legs, or something)
|
2021-08-20 13:09:26 +10:00
|
|
|
if aw.jumpBuffer > 0 {
|
|
|
|
// Tried to jump recently -- so jump
|
|
|
|
aw.vy = jumpVelocity
|
|
|
|
aw.jumpBuffer = 0
|
|
|
|
} else {
|
|
|
|
// Can jump now or soon.
|
|
|
|
aw.vy = 0
|
|
|
|
aw.coyoteTimer = coyoteTime
|
|
|
|
}
|
2021-08-05 12:26:41 +10:00
|
|
|
} else {
|
2021-08-12 16:24:59 +10:00
|
|
|
// Falling. v = v_0 + a, and a = gravity + airResistance(v_0)
|
2021-08-12 16:23:52 +10:00
|
|
|
aw.vy += gravity + airResistance*aw.vy
|
2021-08-07 21:24:15 +10:00
|
|
|
if aw.coyoteTimer > 0 {
|
|
|
|
aw.coyoteTimer--
|
|
|
|
}
|
2021-08-20 13:09:26 +10:00
|
|
|
if aw.jumpBuffer > 0 {
|
|
|
|
aw.jumpBuffer--
|
|
|
|
}
|
2021-08-07 21:24:15 +10:00
|
|
|
}
|
2021-08-12 15:37:09 +10:00
|
|
|
|
|
|
|
// Handle controls
|
|
|
|
|
2021-08-09 12:03:51 +10:00
|
|
|
// NB: spacebar sometimes does things on web pages (scrolls down)
|
2021-08-20 13:09:26 +10:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeySpace) || inpututil.IsKeyJustPressed(ebiten.KeyZ) {
|
|
|
|
// On ground or recently on ground?
|
|
|
|
if aw.coyoteTimer > 0 {
|
2021-08-20 13:17:17 +10:00
|
|
|
// Jump. One frame of v = jumpVelocity (ignoring any gravity already applied this tick).
|
|
|
|
aw.vy = jumpVelocity
|
2021-08-20 13:09:26 +10:00
|
|
|
} else {
|
|
|
|
// Buffer the jump in case aw hits the ground soon.
|
|
|
|
aw.jumpBuffer = jumpBufferTime
|
|
|
|
}
|
2021-08-05 12:26:41 +10:00
|
|
|
}
|
2021-08-20 13:09:26 +10:00
|
|
|
// Left and right
|
2021-08-05 12:26:41 +10:00
|
|
|
switch {
|
2021-08-07 16:38:02 +10:00
|
|
|
case ebiten.IsKeyPressed(ebiten.KeyLeft) || ebiten.IsKeyPressed(ebiten.KeyA):
|
2021-08-05 12:26:41 +10:00
|
|
|
aw.vx = -runVelocity
|
|
|
|
aw.SetAnim(aw.animRunLeft)
|
|
|
|
aw.facingLeft = true
|
2021-08-07 16:38:02 +10:00
|
|
|
case ebiten.IsKeyPressed(ebiten.KeyRight) || ebiten.IsKeyPressed(ebiten.KeyD):
|
2021-08-05 12:26:41 +10:00
|
|
|
aw.vx = runVelocity
|
|
|
|
aw.SetAnim(aw.animRunRight)
|
|
|
|
aw.facingLeft = false
|
|
|
|
default:
|
|
|
|
aw.vx = 0
|
|
|
|
aw.SetAnim(aw.animIdleRight)
|
|
|
|
if aw.facingLeft {
|
|
|
|
aw.SetAnim(aw.animIdleLeft)
|
|
|
|
}
|
|
|
|
}
|
2021-08-12 15:37:09 +10:00
|
|
|
|
|
|
|
// s = (v_0 + v) / 2.
|
2021-08-12 16:14:51 +10:00
|
|
|
aw.MoveX((ux+aw.vx)/2, nil)
|
|
|
|
// For Y, on collision, bounce a little bit.
|
|
|
|
// Does not apply to X because controls override it anyway.
|
|
|
|
aw.MoveY((uy+aw.vy)/2, func() {
|
|
|
|
aw.vy *= restitution
|
|
|
|
if math.Abs(aw.vy) < ε {
|
|
|
|
aw.vy = 0
|
|
|
|
}
|
|
|
|
})
|
2021-08-27 14:12:31 +10:00
|
|
|
return nil
|
2021-08-05 12:26:41 +10:00
|
|
|
}
|
|
|
|
|
2021-08-27 14:52:24 +10:00
|
|
|
func (aw *Awakeman) Prepare(game *engine.Game) error {
|
2021-08-08 22:07:55 +10:00
|
|
|
aw.camera = game.Component(aw.CameraID).(*engine.Camera)
|
2021-08-24 19:33:21 +10:00
|
|
|
aw.toast, _ = game.Component(aw.ToastID).(*engine.DebugToast)
|
2021-08-08 22:07:55 +10:00
|
|
|
|
2021-08-23 20:28:49 +10:00
|
|
|
aw.animIdleLeft = &engine.Anim{Frames: []engine.AnimFrame{
|
|
|
|
{Frame: 1, Duration: 60},
|
|
|
|
}}
|
|
|
|
aw.animIdleRight = &engine.Anim{Frames: []engine.AnimFrame{
|
|
|
|
{Frame: 0, Duration: 60},
|
|
|
|
}}
|
|
|
|
aw.animRunLeft = &engine.Anim{Frames: []engine.AnimFrame{
|
|
|
|
{Frame: 14, Duration: 3},
|
|
|
|
{Frame: 15, Duration: 5},
|
|
|
|
{Frame: 16, Duration: 3},
|
|
|
|
{Frame: 17, Duration: 3},
|
|
|
|
}}
|
|
|
|
aw.animRunRight = &engine.Anim{Frames: []engine.AnimFrame{
|
|
|
|
{Frame: 10, Duration: 3},
|
|
|
|
{Frame: 11, Duration: 5},
|
|
|
|
{Frame: 12, Duration: 3},
|
|
|
|
{Frame: 13, Duration: 3},
|
|
|
|
}}
|
2021-08-23 20:41:13 +10:00
|
|
|
aw.animWalkRight = &engine.Anim{Frames: []engine.AnimFrame{
|
|
|
|
{Frame: 2, Duration: 6},
|
|
|
|
{Frame: 3, Duration: 6},
|
|
|
|
{Frame: 4, Duration: 6},
|
|
|
|
{Frame: 5, Duration: 6},
|
|
|
|
}}
|
|
|
|
aw.animWalkLeft = &engine.Anim{Frames: []engine.AnimFrame{
|
|
|
|
{Frame: 6, Duration: 6},
|
|
|
|
{Frame: 7, Duration: 6},
|
|
|
|
{Frame: 8, Duration: 6},
|
|
|
|
{Frame: 9, Duration: 6},
|
|
|
|
}}
|
2021-08-27 14:52:24 +10:00
|
|
|
return nil
|
2021-08-05 12:26:41 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
func (aw *Awakeman) Scan() []interface{} { return []interface{}{&aw.Sprite} }
|