ichigo/game/aw.go

269 lines
6.5 KiB
Go
Raw Normal View History

2021-08-05 12:26:41 +10:00
package game
import (
2021-08-05 12:33:23 +10:00
"encoding/gob"
2021-09-02 11:53:04 +10:00
"fmt"
2021-08-12 15:55:42 +10:00
"math"
2021-08-05 12:26:41 +10:00
"drjosh.dev/gurgle/engine"
2021-09-09 09:26:41 +10:00
"drjosh.dev/gurgle/geom"
2021-08-05 12:26:41 +10:00
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
2021-09-17 14:56:00 +10:00
const awakemanProducesBubbles = true
2021-08-27 14:13:55 +10:00
var _ interface {
engine.Identifier
engine.Disabler
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-09-01 09:17:08 +10:00
// Awakeman is a bit of a god object for now...
2021-08-05 12:26:41 +10:00
type Awakeman struct {
2021-09-13 16:50:35 +10:00
engine.Disables
2021-09-01 09:17:08 +10:00
Sprite 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
2021-09-16 14:38:21 +10:00
game *engine.Game
2021-08-08 22:07:55 +10:00
camera *engine.Camera
2021-08-24 19:33:21 +10:00
toast *engine.DebugToast
vel geom.Float3
2021-08-07 21:24:15 +10:00
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-09-09 11:12:07 +10:00
spawnPoint geom.Int3
2021-09-16 14:38:21 +10:00
bubbleTimer int
2021-08-05 12:26:41 +10:00
2021-09-08 12:24:34 +10:00
anims map[string]*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-18 19:17:56 +10:00
// TODO: better cheat for noclip
if inpututil.IsKeyJustPressed(ebiten.KeyN) {
aw.noclip = !aw.noclip
aw.vel = geom.Float3{}
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-09-01 12:07:49 +10:00
// aw.Pos is top-left corner, so add half size to get centre
z := 1.0
2021-08-18 19:17:56 +10:00
if ebiten.IsKeyPressed(ebiten.KeyShift) {
2021-09-01 12:07:49 +10:00
z = 2.0
2021-08-18 19:17:56 +10:00
}
2021-09-09 17:16:00 +10:00
aw.camera.PointAt(aw.Sprite.Actor.BoundingBox().Centre(), z)
2021-09-01 09:17:08 +10:00
return nil
2021-08-18 19:17:56 +10:00
}
func (aw *Awakeman) noclipUpdate() error {
if ebiten.IsKeyPressed(ebiten.KeyUp) {
2021-09-01 09:17:08 +10:00
aw.Sprite.Actor.Pos.Y--
2021-08-18 19:17:56 +10:00
}
if ebiten.IsKeyPressed(ebiten.KeyDown) {
2021-09-01 09:17:08 +10:00
aw.Sprite.Actor.Pos.Y++
2021-08-18 19:17:56 +10:00
}
if ebiten.IsKeyPressed(ebiten.KeyLeft) {
2021-09-01 09:17:08 +10:00
aw.Sprite.Actor.Pos.X--
2021-08-18 19:17:56 +10:00
}
if ebiten.IsKeyPressed(ebiten.KeyRight) {
2021-09-01 09:17:08 +10:00
aw.Sprite.Actor.Pos.X++
2021-08-18 19:17:56 +10:00
}
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
2021-09-08 12:24:34 +10:00
gravity = 0.25
airResistance = -0.005 // ⇒ terminal velocity = 10
jumpVelocity = -3.3
sqrt2 = 1.414213562373095
runVelocity = sqrt2
2021-08-20 13:09:26 +10:00
coyoteTime = 5
jumpBufferTime = 5
2021-09-09 11:12:07 +10:00
respawnY = 1000
2021-09-16 14:38:21 +10:00
bubblePeriod = 6
2021-08-05 12:26:41 +10:00
)
2021-09-17 14:56:00 +10:00
if awakemanProducesBubbles {
2021-09-17 11:13:39 +10:00
// Add a bubble?
aw.bubbleTimer--
if aw.bubbleTimer <= 0 {
aw.bubbleTimer = bubblePeriod
2021-09-17 12:50:29 +10:00
bubble := NewBubble(aw.Sprite.Actor.Pos.Add(geom.Pt3(-3, -20, -1)))
2021-09-22 17:04:27 +10:00
if err := aw.game.Load(bubble, Assets); err != nil {
2021-09-17 11:13:39 +10:00
return err
2021-09-16 14:38:21 +10:00
}
2021-09-21 16:53:04 +10:00
// Add bubble to same parent as aw
2021-09-22 17:04:27 +10:00
aw.game.PathRegister(bubble, aw.game.Parent(aw))
if err := aw.game.Prepare(bubble); err != nil {
2021-09-17 11:13:39 +10:00
return err
2021-09-16 14:38:21 +10:00
}
2021-09-17 11:13:39 +10:00
bubble.Sprite.SetAnim(bubble.Sprite.Sheet.NewAnim("bubble"))
2021-09-16 14:53:23 +10:00
}
2021-09-16 14:38:21 +10:00
}
2021-09-09 11:12:07 +10:00
// Fell below some threshold?
if aw.Sprite.Actor.Pos.Y > respawnY {
aw.Sprite.Actor.Pos = aw.spawnPoint
aw.vel = geom.Float3{}
2021-09-09 11:12:07 +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.
v0 := aw.vel
2021-08-12 15:37:09 +10:00
2021-08-12 15:55:42 +10:00
// Has traction?
if aw.vel.Y >= 0 && aw.Sprite.Actor.CollidesAt(aw.Sprite.Actor.Pos.Add(geom.Pt3(0, 1, 0))) {
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.vel.Y = jumpVelocity
2021-08-20 13:09:26 +10:00
aw.jumpBuffer = 0
} else {
// Can jump now or soon.
aw.vel.Y = 0
2021-08-20 13:09:26 +10:00
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)
aw.vel.Y += gravity + airResistance*aw.vel.Y
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.vel.Y = 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-09-08 12:24:34 +10:00
// Left, right, away, toward
aw.vel.X, aw.vel.Z = 0, 0
2021-08-05 12:26:41 +10:00
switch {
2021-09-17 16:47:18 +10:00
case ebiten.IsKeyPressed(ebiten.KeyLeft) || ebiten.IsKeyPressed(ebiten.KeyJ):
aw.vel.X = -runVelocity
2021-09-17 16:47:18 +10:00
case ebiten.IsKeyPressed(ebiten.KeyRight) || ebiten.IsKeyPressed(ebiten.KeyL):
aw.vel.X = runVelocity
2021-08-05 12:26:41 +10:00
}
2021-09-02 17:24:08 +10:00
switch {
2021-09-17 16:47:18 +10:00
case ebiten.IsKeyPressed(ebiten.KeyUp) || ebiten.IsKeyPressed(ebiten.KeyI):
aw.vel.Z = -runVelocity
2021-09-17 16:47:18 +10:00
case ebiten.IsKeyPressed(ebiten.KeyDown) || ebiten.IsKeyPressed(ebiten.KeyK):
aw.vel.Z = runVelocity
2021-09-08 12:24:34 +10:00
}
// Animations and velocity correction
switch {
case aw.vel.X != 0 && aw.vel.Z != 0: // Diagonal
2021-09-08 12:24:34 +10:00
aw.Sprite.SetAnim(aw.anims["run_vert"])
// Pythagorean theorem; |vx| = |vz|, so the hypotenuse is √2 too big
// if we want to run at runVelocity always
aw.vel.X /= sqrt2
aw.vel.Z /= sqrt2
case aw.vel.X == 0 && aw.vel.Z != 0: // Vertical
2021-09-08 12:24:34 +10:00
aw.Sprite.SetAnim(aw.anims["run_vert"])
// vz == 0 for all remaining cases
case aw.vel.X < 0: // Left
2021-09-08 12:24:34 +10:00
aw.Sprite.SetAnim(aw.anims["run_left"])
aw.facingLeft = true
case aw.vel.X > 0: // Right
2021-09-08 12:24:34 +10:00
aw.Sprite.SetAnim(aw.anims["run_right"])
aw.facingLeft = false
default: // aw.velocity.X == 0; Idle
2021-09-08 12:24:34 +10:00
aw.Sprite.SetAnim(aw.anims["idle_right"])
if aw.facingLeft {
aw.Sprite.SetAnim(aw.anims["idle_left"])
}
2021-09-02 17:24:08 +10:00
}
2021-08-12 15:37:09 +10:00
// s = (v_0 + v) / 2.
aw.Sprite.Actor.MoveX((v0.X+aw.vel.X)/2, nil)
2021-09-08 12:24:34 +10:00
// For Y, on collision from going upwards, bounce a little bit.
2021-08-12 16:14:51 +10:00
// Does not apply to X because controls override it anyway.
aw.Sprite.Actor.MoveY((v0.Y+aw.vel.Y)/2, func() {
if aw.vel.Y > 0 {
2021-09-08 12:24:34 +10:00
return
}
aw.vel.Y *= restitution
if math.Abs(aw.vel.Y) < ε {
aw.vel.Y = 0
2021-08-12 16:14:51 +10:00
}
})
aw.Sprite.Actor.MoveZ((v0.Z+aw.vel.Z)/2, nil)
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-09-16 14:38:21 +10:00
aw.game = game
2021-09-02 11:53:04 +10:00
cam, ok := game.Component(aw.CameraID).(*engine.Camera)
if !ok {
return fmt.Errorf("component %q not *engine.Camera", aw.CameraID)
}
aw.camera = cam
tst, ok := game.Component(aw.ToastID).(*engine.DebugToast)
if !ok {
return fmt.Errorf("component %q not *engine.DebugToast", aw.ToastID)
}
aw.toast = tst
2021-09-08 12:24:34 +10:00
aw.anims = aw.Sprite.Sheet.NewAnims()
2021-09-09 11:12:07 +10:00
aw.spawnPoint = aw.Sprite.Actor.Pos
2021-09-02 11:53:04 +10:00
2021-08-27 14:52:24 +10:00
return nil
2021-08-05 12:26:41 +10:00
}
2021-09-22 15:48:02 +10:00
func (aw *Awakeman) Scan(visit func(interface{}) error) error {
return visit(&aw.Sprite)
2021-09-21 14:42:18 +10:00
}
2021-09-17 11:13:39 +10:00
func (aw *Awakeman) String() string {
return fmt.Sprintf("Awakeman@%v", aw.Sprite.Actor.Pos)
}