-
Notifications
You must be signed in to change notification settings - Fork 6
/
sprite.go
211 lines (177 loc) · 4.61 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package ganim8
import (
"image"
"github.com/hajimehoshi/ebiten/v2"
"golang.org/x/exp/rand"
)
type SpriteSize struct {
W, H int
}
type SpriteSizeF struct {
W, H float64
}
// Sprite is a sprite that can be drawn to the screen.
// It can be animated by changing the current frame.
type Sprite struct {
frames []*image.Rectangle
image *ebiten.Image
subImages []*ebiten.Image
size SpriteSize
sizeF SpriteSizeF
length int
flippedH, flippedV bool
op *ebiten.DrawImageOptions
shaderOp *ebiten.DrawRectShaderOptions
}
// NewSprite returns a new sprite.
func NewSprite(img *ebiten.Image, frames []*image.Rectangle) *Sprite {
subImages := make([]*ebiten.Image, len(frames))
for i, frame := range frames {
subImages[i] = img.SubImage(*frame).(*ebiten.Image)
}
size := SpriteSize{0, 0}
sizeF := SpriteSizeF{0, 0}
if len(frames) > 0 {
size = SpriteSize{frames[0].Dx(), frames[0].Dy()}
sizeF = SpriteSizeF{float64(frames[0].Dx()), float64(frames[0].Dy())}
}
return &Sprite{
frames: frames,
image: img,
subImages: subImages,
length: len(frames),
size: size,
sizeF: sizeF,
op: &ebiten.DrawImageOptions{},
shaderOp: &ebiten.DrawRectShaderOptions{},
}
}
// Size returns the size of the sprite.
func (spr *Sprite) Size() (int, int) {
return spr.size.W, spr.size.H
}
// Width returns the width of the sprite.
func (spr *Sprite) Width() int {
return spr.size.W
}
// W is a shortcut for Width().
func (spr *Sprite) W() int {
return spr.Width()
}
// Height returns the height of the sprite.
func (spr *Sprite) Height() int {
return spr.size.H
}
// H is a shortcut for Height().
func (spr *Sprite) H() int {
return spr.Height()
}
// Length returns the number of frames.
func (spr *Sprite) Length() int {
return spr.length
}
// RandomIndex returns random index of the sprite
func (spr *Sprite) RandomIndex() int {
return rand.Intn(spr.length)
}
// LoopIndex returns loop index of the sprite.
func (spr *Sprite) LoopIndex(index int) int {
if index >= spr.length {
return index % spr.length
}
return index
}
// IsEnd returns true if the current frame is the last frame.
func (spr *Sprite) IsEnd(index int) bool {
return index >= spr.length-1
}
// FlipH flips the sprite horizontally.
func (spr *Sprite) FlipH() {
spr.flippedH = !spr.flippedH
}
// FlipV flips the sprite vertically.
func (spr *Sprite) FlipV() {
spr.flippedV = !spr.flippedV
}
// SetFlipH flips the sprite horizontally.
func (spr *Sprite) SetFlipH(flipH bool) {
spr.flippedH = flipH
}
// SetFlipV flips the sprite vertically.
func (spr *Sprite) SetFlipV(flipV bool) {
spr.flippedV = flipV
}
// Draw draws the current frame with the specified options.
func (spr *Sprite) Draw(screen *ebiten.Image, index int, opts *DrawOptions) {
x, y := opts.X, opts.Y
w, h := spr.sizeF.W, spr.sizeF.H
r := opts.Rotate
ox, oy := opts.OriginX, opts.OriginY
sx, sy := opts.ScaleX, opts.ScaleY
op := spr.op
op.GeoM.Reset()
op.ColorM = opts.ColorM
op.CompositeMode = opts.CompositeMode
if spr.flippedH {
sx = sx * -1
ox = 1 - ox
}
if spr.flippedV {
sy = sy * -1
oy = 1 - oy
}
if sx != 1 || sy != 1 {
op.GeoM.Translate(-w*ox, -h*oy)
op.GeoM.Scale(sx, sy)
op.GeoM.Translate(w*ox, h*oy)
}
if r != 0 {
op.GeoM.Translate(-w*ox, -h*oy)
op.GeoM.Rotate(r)
op.GeoM.Translate(w*ox, h*oy)
}
op.GeoM.Translate((x - w*ox), (y - h*oy))
subImage := spr.subImages[index]
screen.DrawImage(subImage, op)
}
// DrawWithShader draws the current frame with the specified options.
func (spr *Sprite) DrawWithShader(screen *ebiten.Image, index int, opts *DrawOptions, shaderOpts *ShaderOptions) {
x, y := opts.X, opts.Y
w, h := spr.sizeF.W, spr.sizeF.H
r := opts.Rotate
ox, oy := opts.OriginX, opts.OriginY
sx, sy := opts.ScaleX, opts.ScaleY
op := spr.shaderOp
op.GeoM.Reset()
op.CompositeMode = opts.CompositeMode
op.Uniforms = shaderOpts.Uniforms
if r != 0 {
op.GeoM.Translate(-w*ox, -h*oy)
op.GeoM.Rotate(r)
op.GeoM.Translate(w*ox, h*oy)
}
if spr.flippedH {
sx = sx * -1
}
if spr.flippedV {
sy = sy * -1
}
if sx != 1 || sy != 1 {
op.GeoM.Translate(-w*ox, -h*oy)
op.GeoM.Scale(sx, sy)
op.GeoM.Translate(w*ox, h*oy)
}
op.GeoM.Translate((x - w*ox), (y - h*oy))
subImage := spr.subImages[index]
op.Images[0] = subImage
op.Images[1] = shaderOpts.Images[0]
op.Images[2] = shaderOpts.Images[1]
op.Images[3] = shaderOpts.Images[2]
screen.DrawRectShader(int(w), int(h), shaderOpts.Shader, op)
}
func (spr *Sprite) Clone() *Sprite {
s := *spr
s.op = &ebiten.DrawImageOptions{}
s.shaderOp = &ebiten.DrawRectShaderOptions{}
return &s
}