/
animation.go
57 lines (47 loc) · 978 Bytes
/
animation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package tank
import "github.com/hajimehoshi/ebiten/v2"
const (
defaultInterval = 20
)
type Animation struct {
images []*ebiten.Image
interval int
paused bool
crtImgIdx int
}
func NewAnimation(interval int) *Animation {
if interval <= 0 {
interval = defaultInterval
}
return &Animation{
images: []*ebiten.Image{},
paused: false,
interval: interval,
crtImgIdx: 0,
}
}
func (a *Animation) Pause() {
a.paused = true
a.crtImgIdx = 0
}
func (a *Animation) Resume() {
a.paused = false
}
func (a *Animation) Update(count int) {
if !a.paused {
if count%a.interval == 0 {
a.crtImgIdx = (a.crtImgIdx + 1) % len(a.images)
}
}
}
func (a *Animation) GetImage() *ebiten.Image {
return a.images[a.crtImgIdx]
}
func (a *Animation) appendImage(img *ebiten.Image) *Animation {
a.images = append(a.images, img)
return a
}
func (a *Animation) appendImages(imgs []*ebiten.Image) *Animation {
a.images = append(a.images, imgs...)
return a
}