Skip to content

Commit

Permalink
Added animatiom over flag and pause switch in example
Browse files Browse the repository at this point in the history
  • Loading branch information
svera committed Sep 15, 2020
1 parent 1df2355 commit 0cc513a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
9 changes: 9 additions & 0 deletions animation/animation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -126,6 +127,7 @@ func (a *Animation) SetCurrentAnim(ID string) error {
a.currentFrameNumber = a.lastFrame()
}
a.elapsed = 0
a.over = false
}
return nil
}
Expand Down Expand Up @@ -170,6 +172,7 @@ func (a *Animation) nextFrameIndex() int {
if !a.isLastFrame(a.currentFrameNumber) {
return a.currentFrameNumber + 1
}
a.over = true
return a.currentFrameNumber
}

Expand All @@ -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
}
9 changes: 8 additions & 1 deletion examples/animated-sprite/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"]...)

Expand All @@ -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
}

Expand All @@ -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
}
}

0 comments on commit 0cc513a

Please sign in to comment.