2021-08-04 15:08:26 +10:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
|
|
|
)
|
|
|
|
|
2021-08-04 17:19:17 +10:00
|
|
|
const dampen = 0.5
|
|
|
|
const gravity = 0.2
|
|
|
|
|
2021-08-04 15:08:26 +10:00
|
|
|
// Sprite combines an Actor with the ability to Draw...
|
|
|
|
type Sprite struct {
|
|
|
|
Actor
|
2021-08-05 10:50:13 +10:00
|
|
|
*Anim // TODO: better
|
2021-08-04 15:08:26 +10:00
|
|
|
Hidden bool
|
|
|
|
ID
|
|
|
|
Src ImageRef
|
|
|
|
ZPos
|
|
|
|
|
2021-08-05 10:50:13 +10:00
|
|
|
vx, vy float64 // TODO: refactor
|
|
|
|
facingLeft bool
|
|
|
|
|
|
|
|
animIdleLeft, animIdleRight, animRunLeft, animRunRight *Anim
|
2021-08-04 15:08:26 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sprite) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
|
|
|
|
if s.Hidden {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var op ebiten.DrawImageOptions
|
|
|
|
op.GeoM.Translate(float64(s.Actor.Position.X), float64(s.Actor.Position.Y))
|
|
|
|
op.GeoM.Concat(geom)
|
|
|
|
|
2021-08-04 17:19:17 +10:00
|
|
|
frame := s.Anim.CurrentFrame()
|
2021-08-04 15:08:26 +10:00
|
|
|
src := s.Src.Image()
|
|
|
|
w, _ := src.Size()
|
2021-08-05 10:50:13 +10:00
|
|
|
sx, sy := (frame*s.Actor.Size.X)%w, ((frame*s.Actor.Size.X)/w)*s.Actor.Size.Y
|
2021-08-04 15:08:26 +10:00
|
|
|
|
2021-08-05 10:50:13 +10:00
|
|
|
screen.DrawImage(src.SubImage(image.Rect(sx, sy, sx+s.Actor.Size.X, sy+s.Actor.Size.Y)).(*ebiten.Image), &op)
|
2021-08-04 15:08:26 +10:00
|
|
|
}
|
|
|
|
|
2021-08-05 10:50:13 +10:00
|
|
|
func (s *Sprite) Scan() []interface{} {
|
2021-08-04 15:08:26 +10:00
|
|
|
return []interface{}{&s.Actor}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sprite) Update() error {
|
|
|
|
// TODO: delegate updating to something else
|
|
|
|
if s.Actor.CollidesAt(s.Actor.Position.Add(image.Pt(0, 1))) {
|
|
|
|
// Not falling
|
|
|
|
s.vy = 0
|
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeySpace) {
|
|
|
|
// Jump?
|
2021-08-04 17:19:17 +10:00
|
|
|
s.vy = -5
|
2021-08-04 15:08:26 +10:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Falling
|
|
|
|
s.vy += gravity
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case ebiten.IsKeyPressed(ebiten.KeyLeft):
|
2021-08-04 17:19:17 +10:00
|
|
|
s.vx = -2
|
2021-08-05 10:26:45 +10:00
|
|
|
s.Anim = s.animRunLeft
|
2021-08-05 10:50:13 +10:00
|
|
|
s.facingLeft = true
|
2021-08-04 15:08:26 +10:00
|
|
|
case ebiten.IsKeyPressed(ebiten.KeyRight):
|
2021-08-04 17:19:17 +10:00
|
|
|
s.vx = 2
|
2021-08-05 10:26:45 +10:00
|
|
|
s.Anim = s.animRunRight
|
2021-08-05 10:50:13 +10:00
|
|
|
s.facingLeft = false
|
2021-08-04 15:08:26 +10:00
|
|
|
default:
|
|
|
|
s.vx = 0
|
2021-08-05 10:50:13 +10:00
|
|
|
s.Anim = s.animIdleRight
|
|
|
|
if s.facingLeft {
|
|
|
|
s.Anim = s.animIdleLeft
|
|
|
|
}
|
2021-08-04 15:08:26 +10:00
|
|
|
}
|
2021-08-04 17:19:17 +10:00
|
|
|
s.Actor.MoveX(s.vx, func() { s.vx = -s.vx * dampen })
|
|
|
|
s.Actor.MoveY(s.vy, func() { s.vy = -s.vy * dampen })
|
|
|
|
return s.Anim.Update()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sprite) Build(g *Game) {
|
|
|
|
// TODO: better than this
|
2021-08-05 10:26:45 +10:00
|
|
|
s.animRunLeft = &Anim{Def: AnimDefs["aw_run_left"]}
|
|
|
|
s.animRunRight = &Anim{Def: AnimDefs["aw_run_right"]}
|
2021-08-05 10:50:13 +10:00
|
|
|
s.animIdleLeft = &Anim{Def: AnimDefs["aw_idle_left"]}
|
|
|
|
s.animIdleRight = &Anim{Def: AnimDefs["aw_idle_right"]}
|
|
|
|
}
|