movement, animation, etc

This commit is contained in:
Josh Deprez 2021-09-08 12:24:34 +10:00
parent 94a72ad517
commit 67a8f03cbf
8 changed files with 72 additions and 53 deletions

Binary file not shown.

Binary file not shown.

View file

@ -29,7 +29,7 @@ func init() {
gob.Register(&Prism{})
}
// PrismMap is a generalised tilemap/wallmap/etc.
// PrismMap is a generalised 3D tilemap/wallmap/etc.
type PrismMap struct {
ID
Disabled
@ -95,7 +95,6 @@ func (m *PrismMap) Prepare(g *Game) error {
return fmt.Errorf("inverting PosToWorld: %w", err)
}
m.pwinverse = pwi
// log.Printf("inverted PosToWorld: %v", pwi)
for v, p := range m.Map {
p.pos = v
p.m = m

View file

@ -29,6 +29,15 @@ func (s *Sheet) NewAnim(key string) *Anim {
return s.AnimDefs[key].NewAnim()
}
// NewAnims returns a new Anim for every AnimDef in the AnimDefs map.
func (s *Sheet) NewAnims() map[string]*Anim {
m := make(map[string]*Anim, len(s.AnimDefs))
for k, d := range s.AnimDefs {
m[k] = d.NewAnim()
}
return m
}
// Prepare computes the width of the image (in cells).
func (s *Sheet) Prepare(*Game) error {
s.w, _ = s.Src.Image().Size()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 605 B

After

Width:  |  Height:  |  Size: 868 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

View file

@ -2,7 +2,6 @@ package game
import (
"encoding/gob"
"errors"
"fmt"
"math"
@ -38,7 +37,7 @@ type Awakeman struct {
jumpBuffer int
noclip bool
animIdleLeft, animIdleRight, animRunLeft, animRunRight, animWalkLeft, animWalkRight *engine.Anim
anims map[string]*engine.Anim
}
// Ident returns "awakeman". There should be only one!
@ -97,10 +96,11 @@ func (aw *Awakeman) realUpdate() error {
const (
ε = 0.2
restitution = -0.3
gravity = 0.2
airResistance = -0.02 // ⇒ terminal velocity = 10
jumpVelocity = -4.2
runVelocity = 1.4
gravity = 0.25
airResistance = -0.005 // ⇒ terminal velocity = 10
jumpVelocity = -3.3
sqrt2 = 1.414213562373095
runVelocity = sqrt2
coyoteTime = 5
jumpBufferTime = 5
)
@ -118,7 +118,7 @@ func (aw *Awakeman) realUpdate() error {
ux, uy, uz := aw.vx, aw.vy, aw.vz
// Has traction?
if aw.Sprite.Actor.CollidesAt(aw.Sprite.Actor.Pos.Add(engine.Pt3(0, 1, 0))) {
if aw.vy >= 0 && aw.Sprite.Actor.CollidesAt(aw.Sprite.Actor.Pos.Add(engine.Pt3(0, 1, 0))) {
// Not falling.
// Instantly decelerate (AW absorbs all kinetic E in legs, or something)
if aw.jumpBuffer > 0 {
@ -154,38 +154,54 @@ func (aw *Awakeman) realUpdate() error {
aw.jumpBuffer = jumpBufferTime
}
}
// Left and right
// Left, right, away, toward
aw.vx, aw.vz = 0, 0
switch {
case ebiten.IsKeyPressed(ebiten.KeyLeft) || ebiten.IsKeyPressed(ebiten.KeyA):
case ebiten.IsKeyPressed(ebiten.KeyLeft):
aw.vx = -runVelocity
aw.Sprite.SetAnim(aw.animRunLeft)
aw.facingLeft = true
case ebiten.IsKeyPressed(ebiten.KeyRight) || ebiten.IsKeyPressed(ebiten.KeyD):
case ebiten.IsKeyPressed(ebiten.KeyRight):
aw.vx = runVelocity
aw.Sprite.SetAnim(aw.animRunRight)
aw.facingLeft = false
default:
aw.vx = 0
aw.Sprite.SetAnim(aw.animIdleRight)
if aw.facingLeft {
aw.Sprite.SetAnim(aw.animIdleLeft)
}
}
// Up and down (away and closer)
switch {
case ebiten.IsKeyPressed(ebiten.KeyUp) || ebiten.IsKeyPressed(ebiten.KeyW):
case ebiten.IsKeyPressed(ebiten.KeyUp):
aw.vz = -runVelocity
case ebiten.IsKeyPressed(ebiten.KeyDown) || ebiten.IsKeyPressed(ebiten.KeyS):
case ebiten.IsKeyPressed(ebiten.KeyDown):
aw.vz = runVelocity
default:
aw.vz = 0
}
// Animations and velocity correction
switch {
case aw.vx != 0 && aw.vz != 0: // Diagonal
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.vx /= sqrt2
aw.vz /= sqrt2
case aw.vx == 0 && aw.vz != 0: // Vertical
aw.Sprite.SetAnim(aw.anims["run_vert"])
// vz == 0 for all remaining cases
case aw.vx < 0: // Left
aw.Sprite.SetAnim(aw.anims["run_left"])
aw.facingLeft = true
case aw.vx > 0: // Right
aw.Sprite.SetAnim(aw.anims["run_right"])
aw.facingLeft = false
default: // aw.vx == 0; Idle
aw.Sprite.SetAnim(aw.anims["idle_right"])
if aw.facingLeft {
aw.Sprite.SetAnim(aw.anims["idle_left"])
}
}
// s = (v_0 + v) / 2.
aw.Sprite.Actor.MoveX((ux+aw.vx)/2, nil)
// For Y, on collision, bounce a little bit.
// For Y, on collision from going upwards, bounce a little bit.
// Does not apply to X because controls override it anyway.
aw.Sprite.Actor.MoveY((uy+aw.vy)/2, func() {
if aw.vy > 0 {
return
}
aw.vy *= restitution
if math.Abs(aw.vy) < ε {
aw.vy = 0
@ -206,31 +222,16 @@ func (aw *Awakeman) Prepare(game *engine.Game) error {
return fmt.Errorf("component %q not *engine.DebugToast", aw.ToastID)
}
aw.toast = tst
aw.anims = aw.Sprite.Sheet.NewAnims()
/*
idle_left
idle_right
run_left
run_right
run_vert
*/
aw.animIdleLeft = aw.Sprite.Sheet.NewAnim("idle_left")
if aw.animIdleLeft == nil {
return errors.New("missing anim idle_left")
}
aw.animIdleRight = aw.Sprite.Sheet.NewAnim("idle_right")
if aw.animIdleRight == nil {
return errors.New("missing anim idle_right")
}
aw.animRunLeft = aw.Sprite.Sheet.NewAnim("run_left")
if aw.animRunLeft == nil {
return errors.New("missing anim run_left")
}
aw.animRunRight = aw.Sprite.Sheet.NewAnim("run_right")
if aw.animRunRight == nil {
return errors.New("missing anim run_right")
}
aw.animWalkRight = aw.Sprite.Sheet.NewAnim("walk_left")
if aw.animWalkRight == nil {
return errors.New("missing anim walk_left")
}
aw.animWalkLeft = aw.Sprite.Sheet.NewAnim("walk_right")
if aw.animWalkLeft == nil {
return errors.New("missing anim walk_right")
}
return nil
}

12
main.go
View file

@ -251,7 +251,7 @@ func level1() *engine.Scene {
engine.Pt3(4, 0, 5): {},
engine.Pt3(5, 0, 5): {},
engine.Pt3(6, 0, 5): {},
engine.Pt3(6, -1, 5): {},
engine.Pt3(6, -1, 5): {Cell: 1},
engine.Pt3(7, 0, 5): {},
engine.Pt3(8, 0, 5): {},
engine.Pt3(9, 0, 5): {},
@ -360,6 +360,16 @@ func level1() *engine.Scene {
{Cell: 12, Duration: 3},
{Cell: 13, Duration: 3},
}},
"run_vert": {Steps: []engine.AnimStep{
{Cell: 18, Duration: 3},
{Cell: 19, Duration: 5},
{Cell: 20, Duration: 3},
{Cell: 21, Duration: 3},
{Cell: 22, Duration: 3},
{Cell: 23, Duration: 5},
{Cell: 24, Duration: 3},
{Cell: 25, Duration: 3},
}},
"walk_left": {Steps: []engine.AnimStep{
{Cell: 2, Duration: 6},
{Cell: 3, Duration: 6},