-
Notifications
You must be signed in to change notification settings - Fork 0
/
animation.go
212 lines (171 loc) · 4.39 KB
/
animation.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
package main
import (
"encoding/xml"
"fmt"
"os"
"github.com/veandco/go-sdl2/sdl"
)
type Animation struct {
repeat bool
isEnded bool
currFrame int
}
func NewAnimation(repeat bool) *Animation {
return &Animation{
repeat: repeat,
isEnded: false,
currFrame: 0,
}
}
func (a *Animation) IsEnded() bool {
return a.isEnded
}
func (a *Animation) Update(dt float64) {
}
type SpriteAnimation struct {
Animation
flip sdl.RendererFlip
frameCount int
speed int
spriteRow int
texId string
}
func NewSpriteAnimation(texId string, frameCount, speed int, flip sdl.RendererFlip) *SpriteAnimation {
return &SpriteAnimation{
texId: texId,
frameCount: frameCount,
speed: speed,
flip: flip,
}
}
func (sa *SpriteAnimation) SetFlip(flip sdl.RendererFlip) {
sa.flip = flip
}
func (sa *SpriteAnimation) GetSpriteRow() int {
return sa.spriteRow
}
func (sa *SpriteAnimation) SetSpriteRow(row int) {
sa.spriteRow = row
}
func (sa *SpriteAnimation) SetProps(texId string, spriteRow, frameCount, speed int) {
sa.texId = texId
sa.spriteRow = spriteRow
sa.frameCount = frameCount
sa.speed = speed
}
func (sa *SpriteAnimation) IncrementSpriteRow() {
sa.spriteRow++
}
func (sa *SpriteAnimation) DecrementSpriteRow() {
sa.spriteRow--
}
func (sa SpriteAnimation) Draw(x, y, width, height int, scaleX, scaleY float64) {
err := TextureManagerInstance.GetInstance().DrawFrame(sa.texId, x, y, width, height, sa.spriteRow, sa.currFrame, sa.flip)
if err != nil {
panic(err)
}
}
func (sa *SpriteAnimation) Update(dt uint64) {
time := sdl.GetTicks64()
sa.currFrame = int(int(time)/(sa.speed)) % sa.frameCount
}
func (sa SpriteAnimation) Destroy() {
}
type Sequence struct {
Speed int
FrameCount int
Width int
Height int
TextureIds []string
}
type SeqAnimation struct {
Animation
currSeq *Sequence
seqMap map[string]*Sequence
}
func NewSeqAnimation(repeat bool, path, seqId string) (*SeqAnimation, error) {
anim := SeqAnimation{
Animation: *NewAnimation(repeat),
currSeq: nil,
seqMap: make(map[string]*Sequence),
}
err := anim.parse(path)
if err != nil {
return nil, fmt.Errorf("failed to parse the enemy files, %v", err)
}
err = anim.SetCurrentSeq(seqId)
if err != nil {
return nil, fmt.Errorf("seq map is nil, %v", err)
}
return &anim, nil
}
func (sqa *SeqAnimation) SetCurrentSeq(seqId string) error {
_, ok := sqa.seqMap[seqId]
if !ok {
return fmt.Errorf("failed to find %v in seq map", seqId)
}
sqa.currSeq = sqa.seqMap[seqId]
return nil
}
func (sqa *SeqAnimation) SetRepeat(repeat bool) {
sqa.repeat = repeat
}
func (sqa *SeqAnimation) parse(path string) error {
data, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to load data from file, %v, %v", path, err)
}
var Anims XMLAnimations
err = xml.Unmarshal(data, &Anims)
if err != nil {
return fmt.Errorf("failed to convert to xml, %v", err)
}
for _, xmlSeq := range Anims.Seqs {
seq := &Sequence{
Speed: xmlSeq.Speed,
FrameCount: xmlSeq.FrameCount,
Width: xmlSeq.Width,
Height: xmlSeq.Height,
}
for _, xmlF := range xmlSeq.Frames {
seq.TextureIds = append(seq.TextureIds, xmlF.TexId)
}
sqa.seqMap[xmlSeq.Id] = seq
}
return nil
}
func (sqa *SeqAnimation) Draw(x, y int, scaleX, scaleY float64, flip sdl.RendererFlip) error {
texId := sqa.currSeq.TextureIds[sqa.currFrame]
err := TextureManagerInstance.GetInstance().Draw(texId, x, y, sqa.currSeq.Width, sqa.currSeq.Height, scaleX, scaleY, 1, flip, false)
if err != nil {
return fmt.Errorf("failed to draw the current frame %v, %v", texId, err)
}
return nil
}
func (sqa *SeqAnimation) Update(dt uint64) {
if sqa.repeat || !sqa.isEnded {
sqa.isEnded = false
sqa.currFrame = int(sdl.GetTicks64()/uint64(sqa.currSeq.Speed)) % sqa.currSeq.FrameCount
}
if !sqa.repeat && sqa.currFrame == (sqa.currSeq.FrameCount-1) {
sqa.isEnded = true
sqa.currFrame = sqa.currSeq.FrameCount - 1
}
}
func (sqa *SeqAnimation) Destroy() {
sqa.seqMap = make(map[string]*Sequence)
}
type XMLAnimations struct {
Seqs []XMLSequence `xml:"sequence"`
}
type XMLSequence struct {
Id string `xml:"id,attr"`
FrameCount int `xml:"frameCount,attr"`
Speed int `xml:"speed,attr"`
Width int `xml:"width,attr"`
Height int `xml:"height,attr"`
Frames []XMLFrame `xml:"frame"`
}
type XMLFrame struct {
TexId string `xml:"texId,attr"`
}