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

@ -13,14 +13,16 @@ const gravity = 0.2
// Sprite combines an Actor with the ability to Draw... // Sprite combines an Actor with the ability to Draw...
type Sprite struct { type Sprite struct {
Actor Actor
*Anim // TODO: better *Anim // TODO: better
Hidden bool Hidden bool
ID ID
Src ImageRef Src ImageRef
ZPos ZPos
vx, vy float64 // TODO: refactor 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) { 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() frame := s.Anim.CurrentFrame()
src := s.Src.Image() src := s.Src.Image()
w, _ := src.Size() 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} return []interface{}{&s.Actor}
} }
@ -60,12 +62,17 @@ func (s *Sprite) Update() error {
case ebiten.IsKeyPressed(ebiten.KeyLeft): case ebiten.IsKeyPressed(ebiten.KeyLeft):
s.vx = -2 s.vx = -2
s.Anim = s.animRunLeft s.Anim = s.animRunLeft
s.facingLeft = true
case ebiten.IsKeyPressed(ebiten.KeyRight): case ebiten.IsKeyPressed(ebiten.KeyRight):
s.vx = 2 s.vx = 2
s.Anim = s.animRunRight s.Anim = s.animRunRight
s.facingLeft = false
default: default:
s.vx = 0 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.MoveX(s.vx, func() { s.vx = -s.vx * dampen })
s.Actor.MoveY(s.vy, func() { s.vy = -s.vy * 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 // TODO: better than this
s.animRunLeft = &Anim{Def: AnimDefs["aw_run_left"]} s.animRunLeft = &Anim{Def: AnimDefs["aw_run_left"]}
s.animRunRight = &Anim{Def: AnimDefs["aw_run_right"]} 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}, {Frame: 6, Duration: 12},
}, },
}, },
"aw_idle": { "aw_idle_right": {
Frames: []engine.AnimFrame{ Frames: []engine.AnimFrame{
{Frame: 0, Duration: 60}, {Frame: 0, Duration: 60},
}, },
}, },
"aw_idle_left": {
Frames: []engine.AnimFrame{
{Frame: 1, Duration: 60},
},
},
"aw_walk_right": { "aw_walk_right": {
Frames: []engine.AnimFrame{ Frames: []engine.AnimFrame{
{Frame: 1, Duration: 6},
{Frame: 2, Duration: 6}, {Frame: 2, Duration: 6},
{Frame: 3, Duration: 6}, {Frame: 3, Duration: 6},
{Frame: 4, Duration: 6}, {Frame: 4, Duration: 6},
{Frame: 5, Duration: 6},
}, },
}, },
"aw_walk_left": { "aw_walk_left": {
Frames: []engine.AnimFrame{ Frames: []engine.AnimFrame{
{Frame: 5, Duration: 6},
{Frame: 6, Duration: 6}, {Frame: 6, Duration: 6},
{Frame: 7, Duration: 6}, {Frame: 7, Duration: 6},
{Frame: 8, Duration: 6}, {Frame: 8, Duration: 6},
{Frame: 9, Duration: 3},
}, },
}, },
"aw_run_right": { "aw_run_right": {
Frames: []engine.AnimFrame{ Frames: []engine.AnimFrame{
{Frame: 9, Duration: 3},
{Frame: 10, Duration: 3}, {Frame: 10, Duration: 3},
{Frame: 11, Duration: 3}, {Frame: 11, Duration: 3},
{Frame: 12, Duration: 3}, {Frame: 12, Duration: 3},
{Frame: 13, Duration: 3},
}, },
}, },
"aw_run_left": { "aw_run_left": {
Frames: []engine.AnimFrame{ Frames: []engine.AnimFrame{
{Frame: 13, Duration: 3},
{Frame: 14, Duration: 3}, {Frame: 14, Duration: 3},
{Frame: 15, Duration: 3}, {Frame: 15, Duration: 3},
{Frame: 16, Duration: 3}, {Frame: 16, Duration: 3},
{Frame: 17, Duration: 3},
}, },
}, },
} }