package gameobjects import ( "github.com/faiface/pixel" "github.com/faiface/pixel/pixelgl" ) type Animate struct { //PlayerVec pixel.Vec Sprite *pixel.Sprite Up []pixel.Rect Down []pixel.Rect Left []pixel.Rect Right []pixel.Rect Idle bool Dir int Frame int Current []pixel.Rect SprSheet pixel.Picture Speed int64 } func NewAnimation(spriteSheet pixel.Picture, sprFrames []pixel.Rect) Animate { var animation Animate for i := 0; i < 3; i++ { animation.Up = append(animation.Up, sprFrames[i]) } for i := 3; i < 6; i++ { animation.Right = append(animation.Right, sprFrames[i]) } for i := 6; i < 9; i++ { animation.Left = append(animation.Left, sprFrames[i]) } for i := 9; i < 12; i++ { animation.Down = append(animation.Down, sprFrames[i]) } animation.Current = animation.Down animation.SprSheet = spriteSheet animation.Sprite = pixel.NewSprite(spriteSheet, animation.Current[0]) animation.Frame = 0 animation.Idle = false animation.Speed = 6 return animation } var elapsedTime int64 = 0 func (a *Animate) Update(dt int64) { var direction int = a.Dir switch direction { case 0: a.Current = a.Up case 1: a.Current = a.Down case 2: a.Current = a.Left case 3: a.Current = a.Right } elapsedTime += dt if elapsedTime >= 1000000000/ a.Speed{ a.Frame++ elapsedTime -= 1000000000 / a.Speed } if a.Frame >= 3 { a.Frame = 0 } if a.Idle { a.Frame = 0 } a.Sprite.Set(a.SprSheet, a.Current[a.Frame]) } func (a *Animate) Render(win *pixelgl.Window, pVec pixel.Vec) { a.Sprite.Draw(win, pixel.IM.Scaled(pixel.ZV, 4).Moved(pVec)) }