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"
|
|
|
|
|
|
|
|
"drjosh.dev/gurgle/engine"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
|
|
|
)
|
|
|
|
|
2021-08-05 12:33:23 +10:00
|
|
|
func init() {
|
|
|
|
gob.Register(Awakeman{})
|
|
|
|
}
|
|
|
|
|
2021-08-05 12:26:41 +10:00
|
|
|
type Awakeman struct {
|
|
|
|
engine.Sprite
|
|
|
|
|
2021-08-08 22:07:55 +10:00
|
|
|
CameraID string
|
|
|
|
|
|
|
|
camera *engine.Camera
|
2021-08-07 21:24:15 +10:00
|
|
|
vx, vy float64
|
|
|
|
facingLeft bool
|
|
|
|
coyoteTimer int
|
2021-08-05 12:26:41 +10:00
|
|
|
|
|
|
|
animIdleLeft, animIdleRight, animRunLeft, animRunRight *engine.Anim
|
|
|
|
}
|
|
|
|
|
|
|
|
func (aw *Awakeman) Update() error {
|
|
|
|
const (
|
|
|
|
bounceDampen = 0.5
|
2021-08-05 12:57:22 +10:00
|
|
|
gravity = 0.3
|
2021-08-05 15:16:56 +10:00
|
|
|
jumpVelocity = -3.5
|
2021-08-07 21:24:15 +10:00
|
|
|
runVelocity = 1.4
|
2021-08-05 12:26:41 +10:00
|
|
|
)
|
|
|
|
|
|
|
|
// Standing on something?
|
|
|
|
if aw.CollidesAt(aw.Pos.Add(image.Pt(0, 1))) {
|
|
|
|
// Not falling
|
|
|
|
aw.vy = 0
|
2021-08-07 21:24:15 +10:00
|
|
|
aw.coyoteTimer = 5
|
2021-08-05 12:26:41 +10:00
|
|
|
} else {
|
|
|
|
// Falling
|
|
|
|
aw.vy += gravity
|
2021-08-07 21:24:15 +10:00
|
|
|
if aw.coyoteTimer > 0 {
|
|
|
|
aw.coyoteTimer--
|
|
|
|
}
|
|
|
|
}
|
2021-08-09 12:03:51 +10:00
|
|
|
// NB: spacebar sometimes does things on web pages (scrolls down)
|
|
|
|
if aw.coyoteTimer > 0 && (inpututil.IsKeyJustPressed(ebiten.KeySpace) || inpututil.IsKeyJustPressed(ebiten.KeyZ)) {
|
2021-08-07 21:24:15 +10:00
|
|
|
// Jump
|
|
|
|
aw.vy = jumpVelocity
|
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-10 14:56:01 +10:00
|
|
|
aw.camera.Zoom = 1
|
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyShift) {
|
|
|
|
aw.camera.Zoom = 2
|
2021-08-09 13:58:33 +10:00
|
|
|
}
|
2021-08-12 13:49:22 +10:00
|
|
|
/*
|
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyR) {
|
|
|
|
aw.camera.Rotation += math.Pi / 6
|
|
|
|
}
|
|
|
|
*/
|
2021-08-05 12:26:41 +10:00
|
|
|
aw.MoveX(aw.vx, func() { aw.vx = -aw.vx * bounceDampen })
|
|
|
|
aw.MoveY(aw.vy, func() { aw.vy = -aw.vy * bounceDampen })
|
2021-08-10 15:23:13 +10:00
|
|
|
// aw.Pos is top-left corner, so add half size to get centre
|
2021-08-10 14:56:01 +10:00
|
|
|
aw.camera.Centre = aw.Pos.Add(aw.Size.Div(2))
|
2021-08-05 12:26:41 +10:00
|
|
|
return aw.Sprite.Update()
|
|
|
|
}
|
|
|
|
|
2021-08-08 22:07:55 +10:00
|
|
|
func (aw *Awakeman) Prepare(game *engine.Game) {
|
|
|
|
aw.camera = game.Component(aw.CameraID).(*engine.Camera)
|
|
|
|
|
2021-08-05 12:26:41 +10:00
|
|
|
aw.animIdleLeft = &engine.Anim{Def: engine.AnimDefs["aw_idle_left"]}
|
|
|
|
aw.animIdleRight = &engine.Anim{Def: engine.AnimDefs["aw_idle_right"]}
|
2021-08-05 15:24:16 +10:00
|
|
|
aw.animRunLeft = &engine.Anim{Def: engine.AnimDefs["aw_run_left"]}
|
|
|
|
aw.animRunRight = &engine.Anim{Def: engine.AnimDefs["aw_run_right"]}
|
2021-08-05 12:26:41 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
func (aw *Awakeman) Scan() []interface{} { return []interface{}{&aw.Sprite} }
|