62 lines
1.2 KiB
Go
62 lines
1.2 KiB
Go
package ox
|
|
|
|
//import "time"
|
|
import "surdeus.su/core/gg"
|
|
import "surdeus.su/core/gg/ax"
|
|
|
|
var _ = gg.Drawer(&AnimatedSprite{})
|
|
// 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(
|
|
animations ax.AnimationSet,
|
|
timeBetweenFrames gg.Duration,
|
|
) AnimatedSprite {
|
|
ret := AnimatedSprite{}
|
|
ret.animations = animations
|
|
ret.timeBetweenFrames = timeBetweenFrames
|
|
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 gg.Context) *gg.Drawing {
|
|
as.duration += c.Engine().DrawDT()
|
|
|
|
frames := as.animations[as.animationId]
|
|
fullTime := gg.Duration(len(frames)) * as.timeBetweenFrames
|
|
if as.duration > fullTime {
|
|
as.duration -= fullTime
|
|
}
|
|
|
|
as.Images[0] = frames[
|
|
(as.duration/as.timeBetweenFrames) %
|
|
gg.Duration(len(frames)),
|
|
]
|
|
|
|
return as.Sprite.Draw(c)
|
|
}
|
|
|