@@ -2,12 +2,28 @@ package anim
22
33import (
44 "fmt"
5- "image"
65 "math"
76
87 "github.com/hajimehoshi/ebiten/v2"
98)
109
10+ const str string = `
11+ Playback state;
12+ Current Atlas: %v
13+ Current Anim: %v
14+ Paused: %v
15+ Tick: %v
16+ Current Anim Index %v
17+ Current Anim FPS: %v
18+ `
19+
20+ // Animation for AnimationPlayer
21+ type Animation struct {
22+ Name string // Name of the aimation state
23+ Frames []* ebiten.Image // Animation frames
24+ FPS float64 // Animation playback speed (Frames Per Second).
25+ }
26+
1127// PlaybackData is AnimationPlayer's playback data.
1228// With this structure, the playback state can be saved to disk and reloaded.
1329type PlaybackData struct {
@@ -23,16 +39,6 @@ type PlaybackData struct {
2339 CurrentIndex int
2440}
2541
26- var str = `
27- Playback state;
28- Current Atlas: %v
29- Current Anim: %v
30- Paused: %v
31- Tick: %v
32- Current Anim Index %v
33- Current Anim FPS: %v
34- `
35-
3642// AnimationPlayer plays and manages animations.
3743type AnimationPlayer struct {
3844 Data * PlaybackData
@@ -45,15 +51,15 @@ type AnimationPlayer struct {
4551 Animations map [string ]map [string ]* Animation
4652}
4753
48- func (a * AnimationPlayer ) String () string {
54+ func (ap * AnimationPlayer ) String () string {
4955 return fmt .Sprintf (
5056 str ,
51- a .Data .CurrentAtlas ,
52- a .Data .CurrentAnim ,
53- a .Data .Paused ,
54- a .Data .Tick ,
55- a .Data .CurrentIndex ,
56- a .CurrentAnimFPS (),
57+ ap .Data .CurrentAtlas ,
58+ ap .Data .CurrentAnim ,
59+ ap .Data .Paused ,
60+ ap .Data .Tick ,
61+ ap .Data .CurrentIndex ,
62+ ap .CurrentAnimFPS (),
5763 )
5864}
5965
@@ -105,7 +111,12 @@ func (ap *AnimationPlayer) NewAnim(
105111 if pingPong {
106112 subImages = MakePingPong (subImages )
107113 }
108- animation := NewAnimation (stateName , subImages , FPS )
114+ // animation := NewAnimation(stateName, subImages, FPS)
115+ animation := & Animation {
116+ Name : stateName ,
117+ Frames : subImages ,
118+ FPS : FPS ,
119+ }
109120 ap.Animations [atlas.Name ][stateName ] = animation
110121 }
111122 ap .Data .CurrentAnim = stateName
@@ -122,12 +133,6 @@ func (ap *AnimationPlayer) SetAllFPS(FPS float64) {
122133 }
123134}
124135
125- // AddAnimation adds the given animation to this player.
126- // Adds the name of the animation as a map key.
127- func (ap * AnimationPlayer ) AddAnimation (a * Animation ) {
128- ap .Animations [ap .Data .CurrentAtlas ][a .Name ] = a
129- }
130-
131136// CurrentAnimFPS returns FPS of the current animation
132137func (ap * AnimationPlayer ) CurrentAnimFPS () float64 {
133138 return ap .Animations [ap .Data .CurrentAtlas ][ap .Data .CurrentAnim ].FPS
@@ -141,18 +146,25 @@ func (ap *AnimationPlayer) SetAnimFPS(animName string, FPS float64) {
141146// SetAnim sets the animation and resets to the first frame.
142147//
143148// If you assign ap.Data.CurrentAnim = "animName" directly, the animation will not be reset.
144- func (ap * AnimationPlayer ) SetAnim (state string ) {
145- if ap .Data .CurrentAnim != state {
146- ap .Data .CurrentAnim = state
149+ func (ap * AnimationPlayer ) SetAnim (animName string ) {
150+ if ap .Data .CurrentAnim != animName {
151+ ap .Data .CurrentAnim = animName
147152 ap .Data .Tick = 0
148153 ap .Data .CurrentIndex = 0
149154 }
150155}
151156
152- func (ap * AnimationPlayer ) CurrentAtlas () string {
157+ func (ap * AnimationPlayer ) SetAtlas (atlasName string ) {
158+ ap .Data .CurrentAtlas = atlasName
159+ }
160+
161+ // Atlas returns current Atlas
162+ func (ap * AnimationPlayer ) Atlas () string {
153163 return ap .Data .CurrentAtlas
154164}
155- func (ap * AnimationPlayer ) CurrentAnim () string {
165+
166+ // Anim returns current animation state name
167+ func (ap * AnimationPlayer ) Anim () string {
156168 return ap .Data .CurrentAnim
157169}
158170
@@ -182,48 +194,3 @@ func (ap *AnimationPlayer) Update() {
182194 // update current frame
183195 ap .CurrentFrame = ap .Animations [ap .Data .CurrentAtlas ][ap .Data .CurrentAnim ].Frames [ap .Data .CurrentIndex ]
184196}
185-
186- // Animation for AnimationPlayer
187- type Animation struct {
188- Name string // Name of the aimation state
189- Frames []* ebiten.Image // Animation frames
190- FPS float64 // Animation playback speed (Frames Per Second).
191- }
192-
193- // NewAnimation returns new Animation
194- func NewAnimation (name string , frames []* ebiten.Image , FPS float64 ) * Animation {
195- return & Animation {
196- Name : name ,
197- Frames : frames ,
198- FPS : FPS ,
199- }
200- }
201-
202- // SubImages returns sub-images from spriteSheet image
203- func SubImages (spriteSheet * ebiten.Image , x , y , w , h , subImageCount int , vertical bool ) []* ebiten.Image {
204-
205- subImages := []* ebiten.Image {}
206- frameRect := image .Rect (x , y , x + w , y + h )
207-
208- for range subImageCount {
209- subImages = append (subImages , spriteSheet .SubImage (frameRect ).(* ebiten.Image ))
210- if vertical {
211- frameRect .Min .Y += h
212- frameRect .Max .Y += h
213- } else {
214- frameRect .Min .X += w
215- frameRect .Max .X += w
216- }
217- }
218- return subImages
219-
220- }
221-
222- // MakePingPong arranges the animation indexes to play back and forth.
223- // [0 1 2 3] -> [0 1 2 3 2 1]
224- func MakePingPong (frames []* ebiten.Image ) []* ebiten.Image {
225- for i := len (frames ) - 2 ; i > 0 ; i -- {
226- frames = append (frames , frames [i ])
227- }
228- return frames
229- }
0 commit comments