forked from SolarLune/goaseprite
/
player.go
executable file
·283 lines (233 loc) · 7.89 KB
/
player.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Package sprite is an Aseprite JSON loader written in Golang.
package sprite
import (
"errors"
"image"
"github.com/hajimehoshi/ebiten/v2"
)
var (
ErrNoTagByName = errors.New("no tags by name")
)
// Player is an animation player for Aseprite files.
type Player struct {
File *File
PlaySpeed float32 // The playback speed; altering this can be used to globally slow down or speed up animation playback.
CurrentTag *Tag // The currently playing animation.
FrameIndex int // The current frame of the File's animation / tag playback.
PrevFrameIndex int // The previous frame in the playback.
frameCounter float32
prevUVX float64
prevUVY float64
// OnLoop gets called when the playing animation / tag does a complete loop. For a ping-pong
// animation, this is a full forward + back cycle.
OnLoop func(p *Player)
// OnFrameChange gets called when the playing animation / tag changes frames.
OnFrameChange func(p *Player, frame int)
// OnTagEnter gets called when entering a tag from "outside" of it (i.e. if not playing a
//tag and then it gets played, this gets called, or if you're playing a tag and you pass
// through another tag).
OnTagEnter func(p *Player, t *Tag)
OnTagExit func(p *Player, t *Tag)
// OnDraw callbacl called just before drawing the sprite, if return false the draw is aborted.
OnDraw func(p *Player, screen, img *ebiten.Image, opts *ebiten.DrawImageOptions) bool
playDirection int
img *ebiten.Image
}
// CreatePlayer returns a new animation player that plays animations from a given Aseprite file.
func (f *File) CreatePlayer() *Player {
return &Player{
File: f,
PlaySpeed: 1,
}
}
func (f *File) CreatePlayerWithImage(img *ebiten.Image) *Player {
return &Player{
File: f,
PlaySpeed: 1,
img: img,
}
}
// Clone clones the Player.
func (p *Player) Clone() *Player {
newPlayer := p.File.CreatePlayer()
newPlayer.PlaySpeed = p.PlaySpeed
newPlayer.CurrentTag = p.CurrentTag
newPlayer.FrameIndex = p.FrameIndex
newPlayer.frameCounter = p.frameCounter
newPlayer.OnLoop = p.OnLoop
newPlayer.OnFrameChange = p.OnFrameChange
newPlayer.OnTagEnter = p.OnTagEnter
newPlayer.OnTagExit = p.OnTagExit
return newPlayer
}
func (p *Player) Draw(screen *ebiten.Image) error {
opts := &ebiten.DrawImageOptions{}
sub := p.img.SubImage(image.Rect(p.CurrentFrameCoords())).(*ebiten.Image)
if p.OnDraw != nil {
if stop := p.OnDraw(p, screen, sub, opts); stop {
return nil
}
}
screen.DrawImage(sub, opts)
return nil
}
// Play sets the specified tag name up to be played back. A tagName of "" will play back the entire file.
func (p *Player) Play(tagName string) error {
t, ok := p.File.Tags[tagName]
if !ok {
return ErrNoTagByName
}
if p.CurrentTag == t {
return nil
}
if p.CurrentTag == nil {
p.PrevFrameIndex = -1
} else {
p.PrevFrameIndex = p.FrameIndex
}
p.CurrentTag = t
p.frameCounter = 0
if t.Direction == PlayBackward {
p.playDirection = -1
p.FrameIndex = p.CurrentTag.End
} else {
p.playDirection = 1
p.FrameIndex = p.CurrentTag.Start
}
p.pollTagChanges()
return nil
}
// Update updates the currently playing animation. dt is the delta value between the previous frame and the current frame.
func (p *Player) Update(dt float32) {
if p.CurrentTag == nil {
return
}
t := p.CurrentTag
p.frameCounter += dt * p.PlaySpeed
frameDur := p.File.Frames[p.FrameIndex].Duration
p.prevUVX, p.prevUVY = p.CurrentUVCoords()
for p.frameCounter >= frameDur {
p.frameCounter -= frameDur
p.PrevFrameIndex = p.FrameIndex
p.FrameIndex += p.playDirection
if t.Direction == PlayPingPong {
if p.FrameIndex > t.End {
p.FrameIndex = t.End - 1
p.playDirection *= -1
} else if p.FrameIndex < t.Start {
p.FrameIndex = t.Start + 1
p.playDirection *= -1
if p.OnLoop != nil {
p.OnLoop(p)
}
}
} else if p.playDirection > 0 && p.FrameIndex > t.End {
p.FrameIndex -= t.End - t.Start + 1
if p.OnLoop != nil {
p.OnLoop(p)
}
} else if p.playDirection < 0 && p.FrameIndex < t.Start {
p.FrameIndex += t.End - t.Start + 1
if p.OnLoop != nil {
p.OnLoop(p)
}
}
if p.FrameIndex != p.PrevFrameIndex && p.OnFrameChange != nil {
p.OnFrameChange(p, p.FrameIndex)
}
p.pollTagChanges()
}
}
// TouchingTags returns the tags currently being touched by the Player (tag).
func (p *Player) TouchingTags() []*Tag {
var tags []*Tag
for _, t := range p.File.Tags {
if p.FrameIndex >= t.Start && p.FrameIndex <= t.End {
tags = append(tags, t)
}
}
return tags
}
// TouchingTagByName returns if a tag by the given name is being touched by the Player (tag).
func (p *Player) TouchingTagByName(tagName string) bool {
for _, t := range p.File.Tags {
if t.Name == tagName && p.FrameIndex >= t.Start && p.FrameIndex <= t.End {
return true
}
}
return false
}
// pollTagChanges polls the File for tag changes (entering or exiting Tags).
func (p *Player) pollTagChanges() {
if p.OnTagExit != nil {
for _, tag := range p.File.Tags {
if (p.PrevFrameIndex >= tag.Start && p.PrevFrameIndex <= tag.End) && (p.FrameIndex < tag.Start || p.FrameIndex > tag.End) {
p.OnTagExit(p, tag)
}
}
}
if p.OnTagEnter != nil {
for _, tag := range p.File.Tags {
if (p.PrevFrameIndex < tag.Start || p.PrevFrameIndex > tag.End) && (p.FrameIndex >= tag.Start && p.FrameIndex <= tag.End) {
p.OnTagEnter(p, tag)
}
}
}
}
// CurrentFrame returns the current frame for the currently playing Tag in the File and a boolean indicating if the Player is playing a Tag or not.
func (p *Player) CurrentFrame() (Frame, bool) {
if p.CurrentTag == nil {
return Frame{}, false
}
return p.File.Frames[p.FrameIndex], true
}
// CurrentFrameCoords returns the four corners of the current frame, of format (x1, y1, x2, y2). If File.CurrentFrame() is nil, it will instead
// return all -1's.
func (p *Player) CurrentFrameCoords() (int, int, int, int) {
frame, ok := p.CurrentFrame()
if !ok {
return -1, -1, -1, -1
}
return frame.X, frame.Y, frame.X + int(p.File.FrameWidth), frame.Y + int(p.File.FrameHeight)
}
// CurrentUVCoords returns the top-left corner of the current frame, of format (x, y). If File.CurrentFrame() is nil, it will instead
// return (-1, -1).
func (p *Player) CurrentUVCoords() (float64, float64) {
frame, ok := p.CurrentFrame()
if !ok {
return -1, -1
}
return float64(frame.X) / float64(p.File.Width), float64(frame.Y) / float64(p.File.Height)
}
// CurrentUVCoordsDelta returns the current UV Coords as a coordinate movement delta.
// For example, if an animation were to return the X-axis UV coordinates of :
// [ 0, 0, 0, 0, 0.5, 0.5, 0.5, 0.5 ],
// CurrentUVCoordsDelta would return [ 0, 0, 0, 0, 0.5, 0, 0, 0 ], as the UV coordinate
// only changes on that one frame in the middle, from 0 to 0.5. Once it goes to the end,
// it would return -0.5 to return back to the starting frame.
func (p *Player) CurrentUVCoordsDelta() (float64, float64) {
currentX, currentY := p.CurrentUVCoords()
return currentX - p.prevUVX, currentY - p.prevUVY
}
// SetFrameIndex sets the currently visible frame to frameIndex, using the playing animation as the range.
// This means calling SetFrameIndex with a frameIndex of 2 would set it to the third frame of the animation that is currently playing.
func (p *Player) SetFrameIndex(frameIndex int) {
if p.CurrentTag == nil {
return
}
p.FrameIndex = p.CurrentTag.Start + frameIndex
if p.FrameIndex > p.CurrentTag.End {
p.FrameIndex = p.CurrentTag.End
}
p.frameCounter = 0
}
// FrameIndexInAnimation returns the currently visible frame index, using the playing animation as the range.
// This means that a FrameIndexInAnimation of 0 would be the first frame in the currently playing animation,
// regardless of what frame in the sprite strip that is).
// If no animation is being played, this function will return -1.
func (p *Player) FrameIndexInAnimation() int {
if p.CurrentTag == nil {
return -1
}
return p.FrameIndex - p.CurrentTag.Start
}