ichigo/engine/anim.go

43 lines
994 B
Go
Raw Normal View History

2021-07-31 17:15:32 +10:00
package engine
// Anim is an "instance" of an AnimDef: an animation being displayed,
// together with the current state.
type Anim struct {
2021-07-31 17:17:26 +10:00
Def *AnimDef
Index int
Ticks int
2021-07-31 17:15:32 +10:00
}
2021-07-31 17:17:26 +10:00
func (a *Anim) CurrentFrame() int { return a.Def.Frames[a.Index].Frame }
2021-07-31 17:15:32 +10:00
2021-08-05 15:24:16 +10:00
func (a *Anim) Reset() { a.Index, a.Ticks = 0, 0 }
2021-07-31 17:15:32 +10:00
// Update increments the tick count and advances the frame if necessary.
func (a *Anim) Update() error {
2021-07-31 17:17:26 +10:00
a.Ticks++
2021-07-31 19:20:36 +10:00
if a.Def.OneShot && a.Index == len(a.Def.Frames)-1 {
2021-07-31 17:15:32 +10:00
// on the last frame of a one shot so remain on final frame
return nil
}
2021-07-31 17:17:26 +10:00
if a.Ticks >= a.Def.Frames[a.Index].Duration {
a.Ticks = 0
a.Index++
2021-07-31 17:15:32 +10:00
}
2021-07-31 19:20:36 +10:00
if !a.Def.OneShot && a.Index >= len(a.Def.Frames) {
2021-07-31 17:17:26 +10:00
a.Index = 0
2021-07-31 17:15:32 +10:00
}
return nil
}
// AnimDef describes an animation (sequence of frames and timings).
type AnimDef struct {
2021-07-31 19:25:15 +10:00
Frames []AnimFrame
OneShot bool
2021-07-31 17:15:32 +10:00
}
// AnimFrame describes a frame in an animation.
type AnimFrame struct {
2021-07-31 19:25:15 +10:00
Frame int // show this frame
Duration int // for this long, in ticks
2021-07-31 17:15:32 +10:00
}