From 6994dff47b8f5f1d3ed2a9d4068b7668910017bd Mon Sep 17 00:00:00 2001 From: surdeus Date: Sun, 14 Jan 2024 00:03:49 +0300 Subject: [PATCH] feat: better test for animation and the implementation. --- animation.go | 8 ++++---- cmd/test/main.go | 3 ++- cmd/test/player.go | 21 ++++++++++++++++++--- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/animation.go b/animation.go index c6d2d36..5ba39cc 100644 --- a/animation.go +++ b/animation.go @@ -2,7 +2,7 @@ package gg import ( "github.com/hajimehoshi/ebiten/v2" - "fmt" + //"fmt" ) type Animation []*Image @@ -45,7 +45,6 @@ func AnimationSetFromImage( fw, fh := r.Dx()/w, r.Dy()/h for _, define := range defines { - fmt.Println("def-idx-len:", len(define.Indexes)) animation := make(Animation, len(define.Indexes)) for i := range animation { idx := define.Indexes[i] @@ -56,7 +55,6 @@ func AnimationSetFromImage( }, )) } - fmt.Println("animation-len:", len(animation)) set[define.Id] = animation } @@ -76,6 +74,9 @@ type AnimatedSprite struct { } func (as *AnimatedSprite) Animate(id AnimationId) bool { + if as.AnimationId == id { + return true + } _, ok := as.Animations[id] if ok { as.duration = 0 @@ -87,7 +88,6 @@ func (as *AnimatedSprite) Animate(id AnimationId) bool { func (as *AnimatedSprite) Draw(c *Context) []EVertex { as.duration += c.DrawDt() frames := as.Animations[as.AnimationId] - fmt.Println("len:", len(frames)) fullTime := Duration(len(frames)) * as.TimeBetweenFrames if as.duration > fullTime { as.duration -= fullTime diff --git a/cmd/test/main.go b/cmd/test/main.go index 1439c4c..a7e4149 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -55,7 +55,8 @@ func main() { playerAnimations, _ = gg.AnimationSetFromImage( playerImg, 8, 3, - gg.AD(Walk).DefRow(0, 0, 1, 2, 3, 4), + gg.AD(Stand).DefRow(0, 0, 1, 2, 3, 4), + gg.AD(Walk).DefRow(1, 0, 1, 2, 3, 4, 5, 6, 7), ) player = NewPlayer() tri = NewTri() diff --git a/cmd/test/player.go b/cmd/test/player.go index 9e74153..64191e5 100644 --- a/cmd/test/player.go +++ b/cmd/test/player.go @@ -20,11 +20,11 @@ func NewPlayer() *Player { ret.Transform = gg.T() ret.Scale = gg.V2(1) ret.Around = gg.V2(.5) - ret.MoveSpeed = 90. + ret.MoveSpeed = 30. ret.ScaleSpeed = .2 ret.Animations = playerAnimations - ret.TimeBetweenFrames = time.Second/5 - fmt.Println("player-walk", ret.Animate(Walk)) + ret.TimeBetweenFrames = time.Second/10 + fmt.Println("player-walk", ret.Animate(Stand)) ret.Visible = true ret.Layer = PlayerL @@ -43,6 +43,7 @@ func (p *Player) Update(c *Context) { cam := c.Camera keys := c.Keys() + walking := false shift := c.IsPressed(gg.KeyShift) //p.Uniforms["Random"] = any(rand.Float32()) for _, v := range keys { @@ -59,12 +60,22 @@ func (p *Player) Update(c *Context) { cam.Position.X += p.MoveSpeed * dt case gg.KeyW: p.Position.Y += p.MoveSpeed * dt + walking = true + p.Animate(Walk) case gg.KeyA: p.Position.X -= p.MoveSpeed * dt + p.Scale.X = -1 + walking = true + p.Animate(Walk) case gg.KeyS: p.Position.Y -= p.MoveSpeed * dt + walking = true + p.Animate(Walk) case gg.KeyD: p.Position.X += p.MoveSpeed * dt + p.Scale.X = 1 + walking = true + p.Animate(Walk) case gg.KeyR: cam.Rotation += gg.Pi * p.ScaleSpeed * dt case gg.KeyT: @@ -104,6 +115,10 @@ func (p *Player) Update(c *Context) { } } + if !walking { + p.Animate(Stand) + } + for _, event := range c.Events { switch ec := event.(type) { case *gg.KeyDown: