/
anim.go
92 lines (72 loc) · 1.76 KB
/
anim.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package draw
import (
"time"
"github.com/veandco/go-sdl2/sdl"
"github.com/go-pogo/sdlkit"
"github.com/go-pogo/sdlkit/debug"
"github.com/go-pogo/sdlkit/geom"
)
// https://docs.godotengine.org/en/stable/classes/class_spriteframes.html#class-spriteframes
type AnimSprite struct {
geom.Point // center of sprite
geom.Transform
sdlkit.TextureDisplay
Bounds geom.Bounds
// vervangen voor spriteframes
// spriteframes kunnen uit een spritesheet komen
sheet *SpriteSheet
currentFrame int
totalFrames int
rate time.Duration
time time.Duration
done bool
Repeat bool
}
func NewAnimSprite(sheet *SpriteSheet, duration time.Duration) *AnimSprite {
a := &AnimSprite{
Bounds: geom.RectBounds(sheet.indexes[0].W, sheet.indexes[0].H),
sheet: sheet,
rate: duration / time.Duration(sheet.Len()),
}
a.Alpha = 255
return a
}
func (a *AnimSprite) Finished() bool { return a.done }
func (a *AnimSprite) Frame() (*sdl.Texture, sdl.Rect) {
if a.done {
return nil, sdl.Rect{}
}
return a.sheet.tx, a.sheet.indexes[a.currentFrame]
}
func (a *AnimSprite) Rewind() {
a.currentFrame = 0
a.done = false
}
func (a *AnimSprite) Update(dt float32) {
a.Bounds.Transform(a.Point, a.Transform)
if a.done {
return
}
a.time += time.Duration(float32(time.Second) * dt)
if a.time > a.rate {
a.currentFrame++
a.time = 0
if a.currentFrame >= a.sheet.Len() {
if a.Repeat {
a.currentFrame = 0
} else {
a.done = true
}
}
}
}
func (a *AnimSprite) Draw(r *sdl.Renderer) (err error) {
tx, src := a.Frame()
if tx != nil {
// sdlkit.TextureDisplay{ niet standaard in anim? transform ook niet? }
err = DrawTexture(r, tx, &src, &a.Bounds.Rect, a.Transform, a.TextureDisplay)
}
debug.DrawPos(r, a.Point)
debug.DrawBounds(r, a.Bounds)
return err
}