idle in a direction

This commit is contained in:
Josh Deprez 2021-08-05 10:50:13 +10:00 committed by Josh Deprez
parent dc702ae666
commit 196b220c4f
4 changed files with 27 additions and 14 deletions

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 625 B

After

Width:  |  Height:  |  Size: 628 B

View file

@ -20,7 +20,9 @@ type Sprite struct {
ZPos
vx, vy float64 // TODO: refactor
animIdle, animRunLeft, animRunRight *Anim
facingLeft bool
animIdleLeft, animIdleRight, animRunLeft, animRunRight *Anim
}
func (s *Sprite) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
@ -34,12 +36,12 @@ func (s *Sprite) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
frame := s.Anim.CurrentFrame()
src := s.Src.Image()
w, _ := src.Size()
sx, sy := (frame * s.Actor.Size.X) % w, ((frame * s.Actor.Size.X) / w) * s.Actor.Size.Y
sx, sy := (frame*s.Actor.Size.X)%w, ((frame*s.Actor.Size.X)/w)*s.Actor.Size.Y
screen.DrawImage(src.SubImage(image.Rect(sx, sy, sx + s.Actor.Size.X, sy+s.Actor.Size.Y)).(*ebiten.Image), &op)
screen.DrawImage(src.SubImage(image.Rect(sx, sy, sx+s.Actor.Size.X, sy+s.Actor.Size.Y)).(*ebiten.Image), &op)
}
func (s *Sprite) Scan() []interface{}{
func (s *Sprite) Scan() []interface{} {
return []interface{}{&s.Actor}
}
@ -60,12 +62,17 @@ func (s *Sprite) Update() error {
case ebiten.IsKeyPressed(ebiten.KeyLeft):
s.vx = -2
s.Anim = s.animRunLeft
s.facingLeft = true
case ebiten.IsKeyPressed(ebiten.KeyRight):
s.vx = 2
s.Anim = s.animRunRight
s.facingLeft = false
default:
s.vx = 0
s.Anim = s.animIdle
s.Anim = s.animIdleRight
if s.facingLeft {
s.Anim = s.animIdleLeft
}
}
s.Actor.MoveX(s.vx, func() { s.vx = -s.vx * dampen })
s.Actor.MoveY(s.vy, func() { s.vy = -s.vy * dampen })
@ -76,5 +83,6 @@ func (s *Sprite) Build(g *Game) {
// TODO: better than this
s.animRunLeft = &Anim{Def: AnimDefs["aw_run_left"]}
s.animRunRight = &Anim{Def: AnimDefs["aw_run_right"]}
s.animIdle = &Anim{Def: AnimDefs["aw_idle"]}
s.animIdleLeft = &Anim{Def: AnimDefs["aw_idle_left"]}
s.animIdleRight = &Anim{Def: AnimDefs["aw_idle_right"]}
}

15
main.go
View file

@ -37,41 +37,46 @@ func main() {
{Frame: 6, Duration: 12},
},
},
"aw_idle": {
"aw_idle_right": {
Frames: []engine.AnimFrame{
{Frame: 0, Duration: 60},
},
},
"aw_idle_left": {
Frames: []engine.AnimFrame{
{Frame: 1, Duration: 60},
},
},
"aw_walk_right": {
Frames: []engine.AnimFrame{
{Frame: 1, Duration: 6},
{Frame: 2, Duration: 6},
{Frame: 3, Duration: 6},
{Frame: 4, Duration: 6},
{Frame: 5, Duration: 6},
},
},
"aw_walk_left": {
Frames: []engine.AnimFrame{
{Frame: 5, Duration: 6},
{Frame: 6, Duration: 6},
{Frame: 7, Duration: 6},
{Frame: 8, Duration: 6},
{Frame: 9, Duration: 3},
},
},
"aw_run_right": {
Frames: []engine.AnimFrame{
{Frame: 9, Duration: 3},
{Frame: 10, Duration: 3},
{Frame: 11, Duration: 3},
{Frame: 12, Duration: 3},
{Frame: 13, Duration: 3},
},
},
"aw_run_left": {
Frames: []engine.AnimFrame{
{Frame: 13, Duration: 3},
{Frame: 14, Duration: 3},
{Frame: 15, Duration: 3},
{Frame: 16, Duration: 3},
{Frame: 17, Duration: 3},
},
},
}