ichigo/engine/anim.go
Josh Deprez 183b373867 no json
2021-08-08 11:46:03 +10:00

40 lines
942 B
Go

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