-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaseplayer.go
More file actions
265 lines (229 loc) · 6.27 KB
/
Copy pathaseplayer.go
File metadata and controls
265 lines (229 loc) · 6.27 KB
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
package aseplayer
import (
"errors"
"fmt"
"image"
"io/fs"
"slices"
"time"
"github.com/hajimehoshi/ebiten/v2"
"github.com/setanarut/aseprite"
"github.com/setanarut/v"
)
const Delta = time.Second / 60
type subImager interface {
SubImage(image.Rectangle) image.Image
}
// AnimPlayer plays and manages Aseprite tag animations.
type AnimPlayer struct {
// The frame of the animation currently being played.
//
// Example:
// dio.GeoM.Translate(animPlayer.CurrentFrame.Position.X, animPlayer.CurrentFrame.Position.Y)
// dio.GeoM.Translate(x, y)
// screen.DrawImage(g.animPlayer.CurrentFrame.Image, dio)
CurrentFrame *Frame
// The animation currently being played
CurrentAnimation *Animation
// Animations accessible by their Aseprite tag names
Animations map[string]*Animation
// If true, the animation is paused
Paused bool
frameElapsedTime time.Duration
frameIndex int
isEnded bool
repeatCount uint16
}
// Update advances the animation by the given delta time.
//
// It handles frame progression, looping, and repeat count logic.
// Does nothing if the animation is paused or has ended.
//
// Example:
//
// myAnimPlayer.Update(aseplayer.Delta)
func (a *AnimPlayer) Update(dt time.Duration) {
if a.Paused || a.isEnded {
return
}
activeAnim := a.CurrentAnimation
a.frameElapsedTime += dt
if a.frameElapsedTime >= a.CurrentFrame.Duration {
a.frameElapsedTime = 0
a.frameIndex++
if a.frameIndex >= len(activeAnim.Frames) {
if activeAnim.Repeat == 0 {
a.frameIndex = 0
} else {
a.repeatCount++
if a.repeatCount >= activeAnim.Repeat {
a.isEnded = true
a.frameIndex = len(activeAnim.Frames) - 1
a.CurrentFrame = &activeAnim.Frames[a.frameIndex]
return
}
a.frameIndex = 0
}
}
}
a.CurrentFrame = &activeAnim.Frames[a.frameIndex]
}
// If Animation.Repeat is not zero, it returns true when the animation ends. If it is zero, it is always false.
func (a *AnimPlayer) IsEnded() bool {
return a.isEnded
}
// Play rewinds and plays the animation.
func (a *AnimPlayer) Play(tag string) {
a.CurrentAnimation = a.Animations[tag]
a.CurrentFrame = &a.CurrentAnimation.Frames[0]
a.Rewind()
}
// PlayIfNotCurrent rewinds and plays the animation with the given tag if it's not already playing
func (a *AnimPlayer) PlayIfNotCurrent(tag string) {
if tag != a.CurrentAnimation.Name {
a.Play(tag)
}
}
// Rewinds animation
func (a *AnimPlayer) Rewind() {
a.frameIndex = 0
a.frameElapsedTime = 0
a.CurrentFrame = &a.CurrentAnimation.Frames[0]
a.isEnded = false
a.repeatCount = 0
}
const animPlayerFormatString string = `Repeat count: %v
Ended: %v
Index: %v
Paused: %v
--- Animation ---
%v
`
func (a *AnimPlayer) String() string {
return fmt.Sprintf(animPlayerFormatString,
a.repeatCount,
a.IsEnded(),
a.frameIndex,
a.Paused,
a.CurrentAnimation,
)
}
const animationFormatString string = `Tag: %v
Total Frames: %v
Repeat: %v
UserData:
%v
`
func (a *Animation) String() string {
return fmt.Sprintf(animationFormatString,
a.Name,
len(a.Frames),
a.Repeat,
a.UserData)
}
// Animation for AnimPlayer
type Animation struct {
// The animation tag name is identical to the Aseprite tags
Name string
// Animation frames
Frames []Frame
// Repeat specifies how many times the animation should loop.
// A value of 0 means infinite looping.
Repeat uint16
// Text field of Aseprite Tag's User Data.
//
// It is useful for data transfer. It can be automated with Aseprite Lua scripting.
//
// https://www.aseprite.org/api/tag#tagdata
UserData string
}
type Frame struct {
*ebiten.Image
// Position represents the Cel's top-left coordinates relative to the Aseprite canvas.
Position v.Vec
// Duration of the frame as defined in the Aseprite file.
Duration time.Duration
}
// NewAnimPlayerFromAsepriteFileSystem creates an AnimPlayer from an Aseprite file.
//
// The first Aseprite tag is automatically set as the current animation.
//
// The Aseprite file must contain at least one tag, otherwise an error will occur.
func NewAnimPlayerFromAsepriteFileSystem(fs fs.FS, asePath string) (ap *AnimPlayer, err error) {
ase, err := aseprite.ReadFs(fs, asePath, true)
if err != nil {
return nil, err
}
return animPlayerfromAseprite(&ase)
}
// NewAnimPlayerFromAsepriteFile creates an AnimPlayer from an Aseprite file.
//
// The first Aseprite tag is automatically set as the current animation.
//
// The Aseprite file must contain at least one tag, otherwise an error will occur.
func NewAnimPlayerFromAsepriteFile(asePath string) (ap *AnimPlayer, err error) {
ase, err := aseprite.Read(asePath, true)
if err != nil {
return nil, err
}
return animPlayerfromAseprite(&ase)
}
func animPlayerfromAseprite(ase *aseprite.Ase) (ap *AnimPlayer, err error) {
topVisibleLayer := len(ase.Layers) - 1
if len(ase.Tags) == 0 {
return nil, errors.New("the Aseprite file does not have a tag")
}
ap = &AnimPlayer{
Animations: make(map[string]*Animation),
}
imageCache := make(map[int]*ebiten.Image)
for _, tag := range ase.Tags {
ap.Animations[tag.Name] = &Animation{
Name: tag.Name,
Repeat: tag.Repeat,
UserData: tag.UserData.Text,
}
tagLen := tag.End - tag.Start + 1
frames := make([]Frame, 0, tagLen)
frameIdx := 0
for i := tag.Start; i <= tag.End; i++ {
frames = append(frames, Frame{})
pivot := ase.Layers[topVisibleLayer].Cel(i).Pos
frames[frameIdx].Position = v.Vec{
X: float64(pivot.X),
Y: float64(pivot.Y),
}
if cachedImage, exists := imageCache[i]; exists {
// shallow copy of sub tag image
frames[frameIdx].Image = cachedImage
} else {
img := ase.Layers[topVisibleLayer].Cel(i).Image
newImage := ebiten.NewImageFromImage(img)
imageCache[i] = newImage
frames[frameIdx].Image = newImage
}
frames[frameIdx].Duration = ase.Durations[i]
frameIdx++
}
switch tag.LoopDirection {
// pingpong
case 2:
for i := len(frames) - 2; i > 0; i-- {
originalFrame := frames[i]
frameCopy := Frame{
Image: originalFrame.Image,
Position: originalFrame.Position,
Duration: originalFrame.Duration,
}
frames = append(frames, frameCopy)
}
// reverse
case 1:
slices.Reverse(frames)
}
ap.Animations[tag.Name].Frames = frames
}
ap.CurrentAnimation = ap.Animations[ase.Tags[0].Name]
ap.CurrentFrame = &ap.CurrentAnimation.Frames[0]
return
}