2024-01-13 21:45:56 +03:00
|
|
|
package gg
|
|
|
|
|
|
|
|
import (
|
2024-01-13 23:46:12 +03:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
2024-01-14 00:03:49 +03:00
|
|
|
//"fmt"
|
2024-01-13 21:45:56 +03:00
|
|
|
)
|
|
|
|
|
2024-01-13 23:46:12 +03:00
|
|
|
type Animation []*Image
|
|
|
|
|
|
|
|
type AnimationDefine struct{
|
|
|
|
Id AnimationId
|
|
|
|
Indexes []ImageRectIndex
|
|
|
|
}
|
|
|
|
// Animation define
|
|
|
|
func AD(id AnimationId, indexes ...ImageRectIndex) AnimationDefine {
|
|
|
|
return AnimationDefine{
|
|
|
|
Id: id,
|
|
|
|
Indexes: indexes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (ad AnimationDefine) DefRow(y int, xs ...int) AnimationDefine {
|
|
|
|
for _, x := range xs {
|
|
|
|
ad.Indexes = append(ad.Indexes, ImageRectIndex{x, y})
|
|
|
|
}
|
|
|
|
return ad
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ad AnimationDefine) DefCol(x int, ys ...int) AnimationDefine {
|
|
|
|
for _, y := range ys {
|
|
|
|
ad.Indexes = append(ad.Indexes, ImageRectIndex{x, y})
|
|
|
|
}
|
|
|
|
return ad
|
2024-01-13 21:45:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type AnimationId int
|
2024-01-13 23:46:12 +03:00
|
|
|
type AnimationSet map[AnimationId] Animation
|
|
|
|
func AnimationSetFromImage(
|
|
|
|
img *Image,
|
|
|
|
w, h int,
|
|
|
|
defines ...AnimationDefine,
|
|
|
|
) (AnimationSet, error) {
|
|
|
|
set := AnimationSet{}
|
|
|
|
|
|
|
|
r := img.Bounds()
|
|
|
|
fw, fh := r.Dx()/w, r.Dy()/h
|
|
|
|
|
|
|
|
for _, define := range defines {
|
|
|
|
animation := make(Animation, len(define.Indexes))
|
|
|
|
for i := range animation {
|
|
|
|
idx := define.Indexes[i]
|
|
|
|
animation[i] = ebiten.NewImageFromImage(img.SubImage(
|
|
|
|
ImageRect{
|
|
|
|
Min: ImagePoint{idx.X*fw, idx.Y*fh},
|
|
|
|
Max: ImagePoint{(idx.X+1)*fw, (idx.Y+1)*fh},
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
set[define.Id] = animation
|
|
|
|
}
|
|
|
|
|
|
|
|
return set, nil
|
|
|
|
}
|
2024-01-13 21:45:56 +03:00
|
|
|
|
|
|
|
// The type implements animated sprites.
|
|
|
|
type AnimatedSprite struct {
|
|
|
|
Sprite
|
|
|
|
Animations AnimationSet
|
|
|
|
AnimationId AnimationId
|
|
|
|
CurrentFrame int
|
2024-01-13 23:46:12 +03:00
|
|
|
// This is time between animation frames, not
|
|
|
|
// engine ones.
|
|
|
|
TimeBetweenFrames Duration
|
|
|
|
duration Duration
|
2024-01-13 21:45:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (as *AnimatedSprite) Animate(id AnimationId) bool {
|
2024-01-14 00:03:49 +03:00
|
|
|
if as.AnimationId == id {
|
|
|
|
return true
|
|
|
|
}
|
2024-01-13 21:45:56 +03:00
|
|
|
_, ok := as.Animations[id]
|
|
|
|
if ok {
|
2024-01-13 23:46:12 +03:00
|
|
|
as.duration = 0
|
2024-01-13 21:45:56 +03:00
|
|
|
as.AnimationId = id
|
|
|
|
}
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *AnimatedSprite) Draw(c *Context) []EVertex {
|
2024-01-13 23:46:12 +03:00
|
|
|
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)
|
2024-01-13 21:45:56 +03:00
|
|
|
}
|
|
|
|
|