ichigo/engine/sprite.go

46 lines
911 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-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
Hidden bool
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-05 12:26:41 +10:00
op.GeoM.Translate(float64(s.Pos.X), float64(s.Pos.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-05 12:26:41 +10:00
sp := image.Pt((frame*s.Size.X)%w, ((frame*s.Size.X)/w)*s.Size.Y)
2021-08-04 15:08:26 +10:00
2021-08-05 12:26:41 +10:00
screen.DrawImage(src.SubImage(image.Rectangle{sp, sp.Add(s.Size)}).(*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 12:26:41 +10:00
func (s *Sprite) SetAnim(a *Anim) { 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() }