Skip to content

Commit 8f45806

Browse files
committed
update API + refactoring
1 parent e430c33 commit 8f45806

4 files changed

Lines changed: 82 additions & 79 deletions

File tree

anim.go

Lines changed: 42 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,28 @@ package anim
22

33
import (
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.
1329
type 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.
3743
type 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
132137
func (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-
}

examples/atlases/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ func (g *Game) Update() error {
3838
DIO.GeoM.Reset()
3939

4040
if inpututil.IsKeyJustPressed(ebiten.KeyF) {
41-
switch animPlayer.CurrentAtlas() {
41+
switch animPlayer.Atlas() {
4242
case "ShiftHue":
43-
animPlayer.Data.CurrentAtlas = "Default"
43+
animPlayer.SetAtlas("Default")
4444
case "Default":
45-
animPlayer.Data.CurrentAtlas = "ShiftHue"
45+
animPlayer.SetAtlas("ShiftHue")
4646
}
4747
}
4848

examples/demo/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func main() {
7777
animPlayer.NewAnim("idle", 0, 0, 32, 32, 5, false, false, 5)
7878
animPlayer.NewAnim("run", 0, 32, 32, 32, 8, false, false, 12)
7979
animPlayer.NewAnim("jump", 0, 32*2, 32, 32, 4, false, false, 15)
80-
animPlayer.Data.CurrentAnim = "idle"
80+
animPlayer.SetAnim("idle")
8181

8282
ebiten.SetWindowSize(400, 300)
8383
if err := ebiten.RunGame(&Game{}); err != nil {

helper.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package anim
2+
3+
import (
4+
"image"
5+
6+
"github.com/hajimehoshi/ebiten/v2"
7+
)
8+
9+
// SubImages returns sub-images from spriteSheet image
10+
func SubImages(spriteSheet *ebiten.Image, x, y, w, h, subImageCount int, vertical bool) []*ebiten.Image {
11+
12+
subImages := []*ebiten.Image{}
13+
frameRect := image.Rect(x, y, x+w, y+h)
14+
15+
for range subImageCount {
16+
subImages = append(subImages, spriteSheet.SubImage(frameRect).(*ebiten.Image))
17+
if vertical {
18+
frameRect.Min.Y += h
19+
frameRect.Max.Y += h
20+
} else {
21+
frameRect.Min.X += w
22+
frameRect.Max.X += w
23+
}
24+
}
25+
return subImages
26+
27+
}
28+
29+
// MakePingPong arranges the animation indexes to play back and forth.
30+
// [0 1 2 3] -> [0 1 2 3 2 1]
31+
func MakePingPong(frames []*ebiten.Image) []*ebiten.Image {
32+
for i := len(frames) - 2; i > 0; i-- {
33+
frames = append(frames, frames[i])
34+
}
35+
return frames
36+
}

0 commit comments

Comments
 (0)