2024-05-28 11:24:12 +03:00
|
|
|
package ox
|
|
|
|
|
2024-06-01 16:07:28 +03:00
|
|
|
//import "time"
|
2024-05-28 11:24:12 +03:00
|
|
|
import "surdeus.su/core/gg"
|
|
|
|
import "surdeus.su/core/gg/ax"
|
|
|
|
|
2024-06-01 16:07:28 +03:00
|
|
|
var _ = gg.Drawer(&AnimatedSprite{})
|
2024-05-28 11:24:12 +03:00
|
|
|
// The type implements animated sprites.
|
|
|
|
type AnimatedSprite struct {
|
|
|
|
Sprite
|
|
|
|
|
|
|
|
animations ax.AnimationSet
|
2024-06-01 16:07:28 +03:00
|
|
|
animationId ax.AnimationID
|
2024-05-28 11:24:12 +03:00
|
|
|
|
|
|
|
currentFrame int
|
|
|
|
|
|
|
|
// This is time between animation frames, not
|
|
|
|
// engine ones.
|
|
|
|
timeBetweenFrames gg.Duration
|
|
|
|
duration gg.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAnimatedSprite(
|
2024-06-01 16:07:28 +03:00
|
|
|
animations ax.AnimationSet,
|
|
|
|
timeBetweenFrames gg.Duration,
|
|
|
|
) AnimatedSprite {
|
|
|
|
ret := AnimatedSprite{}
|
|
|
|
ret.animations = animations
|
|
|
|
ret.timeBetweenFrames = timeBetweenFrames
|
2024-05-28 11:24:12 +03:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change current animation to the specified one.
|
2024-06-01 16:07:28 +03:00
|
|
|
func (as *AnimatedSprite) Animate(id ax.AnimationID) bool {
|
|
|
|
if as.animationId == id {
|
2024-05-28 11:24:12 +03:00
|
|
|
return true
|
|
|
|
}
|
2024-06-01 16:07:28 +03:00
|
|
|
_, ok := as.animations[id]
|
2024-05-28 11:24:12 +03:00
|
|
|
if ok {
|
|
|
|
as.duration = 0
|
2024-06-01 16:07:28 +03:00
|
|
|
as.animationId = id
|
2024-05-28 11:24:12 +03:00
|
|
|
}
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2024-06-01 16:07:28 +03:00
|
|
|
func (as *AnimatedSprite) Draw(c gg.Context) *gg.Drawing {
|
|
|
|
as.duration += c.Engine().DrawDT()
|
2024-05-28 11:24:12 +03:00
|
|
|
|
2024-06-01 16:07:28 +03:00
|
|
|
frames := as.animations[as.animationId]
|
|
|
|
fullTime := gg.Duration(len(frames)) * as.timeBetweenFrames
|
2024-05-28 11:24:12 +03:00
|
|
|
if as.duration > fullTime {
|
|
|
|
as.duration -= fullTime
|
|
|
|
}
|
|
|
|
|
|
|
|
as.Images[0] = frames[
|
2024-06-01 16:07:28 +03:00
|
|
|
(as.duration/as.timeBetweenFrames) %
|
|
|
|
gg.Duration(len(frames)),
|
2024-05-28 11:24:12 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
return as.Sprite.Draw(c)
|
|
|
|
}
|
|
|
|
|