gg/ox/asprite.go
2024-05-28 13:24:12 +05:00

56 lines
1 KiB
Go

package ox
import "surdeus.su/core/gg"
import "surdeus.su/core/gg/ax"
// The type implements animated sprites.
type AnimatedSprite struct {
Sprite
animations ax.AnimationSet
animationId ax.AnimationId
currentFrame int
// This is time between animation frames, not
// engine ones.
timeBetweenFrames gg.Duration
duration gg.Duration
}
func NewAnimatedSprite(
) *AnimatedSprite {
ret := &AnimatedSprite{}
return ret
}
// Change current animation to the specified one.
func (as *AnimatedSprite) Animate(id ax.AnimationId) bool {
if as.AnimationId == id {
return true
}
_, ok := as.Animations[id]
if ok {
as.duration = 0
as.AnimationId = id
}
return ok
}
func (as *AnimatedSprite) Draw(c Context) []EVertex {
as.duration += c.DrawDT()
frames := as.Animations[as.AnimationId]
fullTime := Duration(len(frames)) * as.TimeBetweenFrames
if as.duration > fullTime {
as.duration -= fullTime
}
as.Images[0] = frames[
(as.duration/as.TimeBetweenFrames) %
Duration(len(frames)),
]
return as.Sprite.Draw(c)
}