ichigo/engine/sprite.go

54 lines
1 KiB
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-05 12:33:23 +10:00
func init() {
gob.Register(Sprite{})
}
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
FrameSize image.Point
FrameOffset image.Point
Hidden bool
2021-08-04 15:08:26 +10:00
ID
Src ImageRef
ZPos
2021-08-05 12:26:41 +10:00
anim *Anim
2021-08-04 15:08:26 +10:00
}
func (s *Sprite) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
if s.Hidden {
return
}
var op ebiten.DrawImageOptions
2021-08-07 21:31:39 +10:00
dp := s.Pos.Add(s.FrameOffset)
op.GeoM.Translate(float64(dp.X), float64(dp.Y))
2021-08-04 15:08:26 +10:00
op.GeoM.Concat(geom)
2021-08-05 12:26:41 +10:00
frame := s.anim.CurrentFrame()
2021-08-04 15:08:26 +10:00
src := s.Src.Image()
w, _ := src.Size()
2021-08-07 21:31:39 +10:00
sp := image.Pt((frame*s.FrameSize.X)%w, ((frame*s.FrameSize.X)/w)*s.FrameSize.Y)
2021-08-04 15:08:26 +10:00
2021-08-07 21:31:39 +10:00
screen.DrawImage(src.SubImage(image.Rectangle{sp, sp.Add(s.FrameSize)}).(*ebiten.Image), &op)
2021-08-04 15:08:26 +10:00
}
2021-08-05 12:26:41 +10:00
func (s *Sprite) Scan() []interface{} { return []interface{}{&s.Actor} }
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() }