diff --git a/animation/animation.go b/animation/animation.go index 8dbf5f2..c9e1d0f 100644 --- a/animation/animation.go +++ b/animation/animation.go @@ -59,6 +59,7 @@ type Animation struct { elapsed float64 Position pixel.Vec Dir float64 + over bool } // NewAnimation returns a new Sprite instance to be drawn at position x, y @@ -126,6 +127,7 @@ func (a *Animation) SetCurrentAnim(ID string) error { a.currentFrameNumber = a.lastFrame() } a.elapsed = 0 + a.over = false } return nil } @@ -170,6 +172,7 @@ func (a *Animation) nextFrameIndex() int { if !a.isLastFrame(a.currentFrameNumber) { return a.currentFrameNumber + 1 } + a.over = true return a.currentFrameNumber } @@ -181,6 +184,12 @@ func (a *Animation) isLastFrame(number int) bool { return len(a.anims[a.currentAnimID].frames)-1 == number } +// CurrentFrameNumber returns the frame number that it is drawn func (a *Animation) CurrentFrameNumber() int { return a.currentFrameNumber } + +// IsOver returns wheter an animation is over or not +func (a *Animation) IsOver() bool { + return a.over +} diff --git a/examples/animated-sprite/game.go b/examples/animated-sprite/game.go index 491fed6..9c7b795 100644 --- a/examples/animated-sprite/game.go +++ b/examples/animated-sprite/game.go @@ -14,6 +14,7 @@ type Game struct { level *Level canvas *pixelgl.Canvas imd *imdraw.IMDraw + paused bool } func NewGame(canvas *pixelgl.Canvas, imd *imdraw.IMDraw) *Game { @@ -38,8 +39,11 @@ func NewGame(canvas *pixelgl.Canvas, imd *imdraw.IMDraw) *Game { } func (g *Game) Loop(win *pixelgl.Window, dt float64) (string, error) { - g.imd.Clear() g.readInput(win, dt) + if g.paused { + return "game", nil + } + g.imd.Clear() delta := g.hero.Displacement(dt) sol := g.hero.boundingShape().Resolve(delta, g.level.Bounds["level1-background"]...) @@ -50,6 +54,7 @@ func (g *Game) Loop(win *pixelgl.Window, dt float64) (string, error) { g.imd.Draw(g.canvas) g.canvas.Draw(win, pixel.IM.Moved(win.Bounds().Center()).Scaled(win.Bounds().Center(), zoom)) + return "game", nil } @@ -62,5 +67,7 @@ func (g *Game) readInput(win *pixelgl.Window, dt float64) { g.hero.Right(dt) } else if !win.Pressed(pixelgl.KeyLeft) && !win.Pressed(pixelgl.KeyRight) && g.hero.Velocity(physic.AxisX) != 0 { g.hero.Decelerate(physic.AxisX, dt) + } else if win.JustPressed(pixelgl.KeyP) { + g.paused = !g.paused } }