/
aseprite.go
173 lines (155 loc) · 4.07 KB
/
aseprite.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
package aseprite
import (
"bytes"
"embed"
"encoding/json"
"image"
"log"
"sort"
"github.com/hajimehoshi/ebiten/v2"
)
type Frame struct {
Image *ebiten.Image
Duration float64
}
type Tag struct {
From int
To int
Name string
}
type Animation struct {
Tags map[string]*Tag
Frames []*Frame
CurrentTag *Tag
CurrentFrame *Frame
CurrentFrameIdx int
FrameCounter float64
Options *Options
Image *ebiten.Image
OnLoopCallback OnLoopType
Instance interface{}
}
type OnLoopType func(interface{}, *Animation)
type Options struct {
// a pointer to the folder containing assets
EmbedFolder *embed.FS
// the root path to the aseprite assets (json and png)
Root string
}
func Load(path string, opt *Options, tag string, instance interface{}) *Animation {
data, err := opt.EmbedFolder.ReadFile(opt.Root + "/" + path)
if err != nil {
log.Fatal(err)
}
var anim Animation
anim.Options = opt
anim.Instance = instance
var aseData map[string]interface{}
err = json.Unmarshal(data, &aseData)
if err != nil {
log.Fatal(err)
} else {
anim.Init(aseData, tag)
}
return &anim
}
func (anim *Animation) Init(aseData map[string]interface{}, tag string) {
frames := aseData["frames"].(map[string]interface{})
meta := aseData["meta"].(map[string]interface{})
anim.GetImage(meta)
anim.GetTags(meta)
anim.GetFrames(frames)
anim.CurrentTag = anim.Tags[tag]
anim.CurrentFrameIdx = anim.CurrentTag.From
}
func (anim *Animation) OnLoop(callback OnLoopType) {
anim.OnLoopCallback = callback
}
func (anim *Animation) GetImage(meta map[string]interface{}) {
imagePath := meta["image"].(string)
anim.Image = newImageFromEmbed(anim.Options.Root+"/"+imagePath, anim.Options.EmbedFolder)
}
func newImageFromEmbed(path string, folder *embed.FS) *ebiten.Image {
imgData, err := folder.ReadFile(path)
if err != nil {
log.Fatal(err)
}
imgFile, _, err := image.Decode(bytes.NewReader(imgData))
if err != nil {
log.Fatal(err)
}
return ebiten.NewImageFromImage(imgFile)
}
func (anim *Animation) GetTags(meta map[string]interface{}) {
if anim.Tags == nil {
anim.Tags = make(map[string]*Tag)
}
for _, val := range meta["frameTags"].([]interface{}) {
tag := val.(map[string]interface{})
anim.Tags[tag["name"].(string)] = &Tag{
From: int(tag["from"].(float64)),
To: int(tag["to"].(float64)),
Name: tag["name"].(string),
}
}
}
func (anim *Animation) GetFrames(frames map[string]interface{}) {
keys := make([]string, 0, len(frames))
for k := range frames {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
val := frames[k]
frameItem := val.(map[string]interface{})
frame := frameItem["frame"].(map[string]interface{})
x := int(frame["x"].(float64))
y := int(frame["y"].(float64))
w := int(frame["w"].(float64))
h := int(frame["h"].(float64))
duration := frameItem["duration"].(float64)
anim.Frames = append(
anim.Frames,
&Frame{
Image: anim.Image.SubImage(image.Rect(x, y, x+w, y+h)).(*ebiten.Image),
Duration: duration,
})
}
}
func (anim *Animation) Update() {
if anim.CurrentFrame == nil {
anim.CurrentFrame = anim.Frames[anim.CurrentFrameIdx]
return
}
if anim.FrameCounter >= anim.CurrentFrame.Duration {
newFrameIdx := anim.CurrentFrameIdx + 1
if newFrameIdx > anim.CurrentTag.To {
if anim.OnLoopCallback != nil {
anim.OnLoopCallback(anim.Instance, anim)
}
newFrameIdx = anim.CurrentTag.From
}
anim.CurrentFrameIdx = newFrameIdx
anim.FrameCounter = 0
anim.CurrentFrame = anim.Frames[anim.CurrentFrameIdx]
} else {
anim.FrameCounter += (1.0 / 60.0) * 1000
}
}
func (anim *Animation) Draw(x float64, y float64, screen *ebiten.Image) {
opt := &ebiten.DrawImageOptions{}
opt.GeoM.Translate(x, y)
screen.DrawImage(anim.CurrentFrame.Image, opt)
}
func (anim *Animation) SetTag(tag string) {
if anim.Tags[tag] == nil {
log.Println("Could not find tag " + tag)
return
}
if anim.CurrentTag.Name == tag {
return
}
anim.CurrentTag = anim.Tags[tag]
anim.CurrentFrameIdx = anim.CurrentTag.From
anim.CurrentFrame = anim.Frames[anim.CurrentFrameIdx]
}