-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite.go
72 lines (59 loc) · 1.42 KB
/
sprite.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
package main
import (
"image"
"image/draw"
"time"
"github.com/clktmr/n64/drivers/controller"
"github.com/clktmr/n64/rcp/texture"
)
type Sprite struct {
Node
sheet texture.Texture
frames []image.Rectangle
frame int
seek, duration time.Duration
}
func NewSprite(sheet texture.Texture, xCnt, yCnt int, duration time.Duration) *Sprite {
bounds := sheet.Bounds()
size := image.Point{
bounds.Size().X / xCnt,
bounds.Size().Y / yCnt,
}
frames := make([]image.Rectangle, 0)
frame := image.Rectangle{Max: size}.Add(bounds.Min)
for range yCnt {
for range xCnt {
frames = append(frames, frame)
frame = frame.Add(image.Point{size.X, 0})
}
frame = frame.Add(image.Point{-frame.Min.X, size.Y})
}
return &Sprite{
sheet: sheet,
frames: frames,
duration: duration,
}
}
func (p *Sprite) Size() image.Point {
return p.frames[p.frame].Size()
}
func (p *Sprite) Bounds() image.Rectangle {
return image.Rectangle{Max: p.Size()}.Add(p.globalPos)
}
func (p *Sprite) Update(delta time.Duration, input [4]controller.Controller) {
p.Node.Update(delta, input)
p.seek += delta
if p.seek > p.duration {
p.frame = (p.frame + 1) % len(p.frames)
p.seek -= p.duration
}
}
func (p *Sprite) Render() {
frame := p.frames[p.frame]
r := p.Bounds()
renderer.Draw(r, p.sheet, frame.Min, draw.Over)
}
func (p *Sprite) Z() int {
frame := p.frames[p.frame]
return p.globalPos.Y + frame.Dy()
}