ichigo/engine/sprite.go

61 lines
971 B
Go
Raw Normal View History

2021-08-04 15:08:26 +10:00
package engine
import (
2021-08-05 12:33:23 +10:00
"encoding/gob"
2021-08-04 15:08:26 +10:00
"image"
"github.com/hajimehoshi/ebiten/v2"
)
2021-08-18 16:34:51 +10:00
// Ensure Sprite satisfies interfaces.
2021-08-27 11:49:11 +10:00
var _ interface {
Drawer
Scanner
Updater
} = &Sprite{}
2021-08-18 16:34:51 +10:00
2021-08-05 12:33:23 +10:00
func init() {
2021-08-25 15:04:38 +10:00
gob.Register(&Sprite{})
2021-08-05 12:33:23 +10:00
}
2021-08-05 12:26:41 +10:00
// Sprite combines an Actor with the ability to Draw from a single spritesheet.
2021-08-04 15:08:26 +10:00
type Sprite struct {
Actor
2021-08-07 21:31:39 +10:00
FrameOffset image.Point
2021-08-23 11:10:46 +10:00
Hidden
2021-08-26 13:57:19 +10:00
Sheet Sheet
2021-08-23 11:10:46 +10:00
ZOrder
2021-08-04 15:08:26 +10:00
2021-08-05 12:26:41 +10:00
anim *Anim
2021-08-04 15:08:26 +10:00
}
2021-08-12 14:06:01 +10:00
func (s *Sprite) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
2021-08-04 15:08:26 +10:00
if s.Hidden {
return
}
2021-08-07 21:31:39 +10:00
dp := s.Pos.Add(s.FrameOffset)
2021-08-12 14:06:01 +10:00
var geom ebiten.GeoM
geom.Translate(float64(dp.X), float64(dp.Y))
geom.Concat(opts.GeoM)
opts.GeoM = geom
2021-08-04 15:08:26 +10:00
2021-08-26 13:57:19 +10:00
src := s.Sheet.SubImage(s.anim.CurrentFrame())
screen.DrawImage(src, &opts)
2021-08-04 15:08:26 +10:00
}
2021-08-20 17:05:47 +10:00
func (s *Sprite) Scan() []interface{} {
return []interface{}{
&s.Actor,
2021-08-26 13:57:19 +10:00
&s.Sheet,
2021-08-20 17:05:47 +10:00
}
}
2021-08-04 15:08:26 +10:00
2021-08-05 15:24:16 +10:00
func (s *Sprite) SetAnim(a *Anim) {
if s.anim != a {
a.Reset()
}
s.anim = a
}
2021-08-04 17:19:17 +10:00
2021-08-05 12:26:41 +10:00
func (s *Sprite) Update() error { return s.anim.Update() }