sprite framesize/frameoffset

This commit is contained in:
Josh Deprez 2021-08-07 21:31:39 +10:00 committed by Josh Deprez
parent 89461950e2
commit 702774be37
2 changed files with 12 additions and 7 deletions

View file

@ -14,6 +14,8 @@ func init() {
// Sprite combines an Actor with the ability to Draw from a single spritesheet. // Sprite combines an Actor with the ability to Draw from a single spritesheet.
type Sprite struct { type Sprite struct {
Actor Actor
FrameSize image.Point
FrameOffset image.Point
Hidden bool Hidden bool
ID ID
Src ImageRef Src ImageRef
@ -27,15 +29,16 @@ func (s *Sprite) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
return return
} }
var op ebiten.DrawImageOptions var op ebiten.DrawImageOptions
op.GeoM.Translate(float64(s.Pos.X), float64(s.Pos.Y)) dp := s.Pos.Add(s.FrameOffset)
op.GeoM.Translate(float64(dp.X), float64(dp.Y))
op.GeoM.Concat(geom) op.GeoM.Concat(geom)
frame := s.anim.CurrentFrame() frame := s.anim.CurrentFrame()
src := s.Src.Image() src := s.Src.Image()
w, _ := src.Size() w, _ := src.Size()
sp := image.Pt((frame*s.Size.X)%w, ((frame*s.Size.X)/w)*s.Size.Y) sp := image.Pt((frame*s.FrameSize.X)%w, ((frame*s.FrameSize.X)/w)*s.FrameSize.Y)
screen.DrawImage(src.SubImage(image.Rectangle{sp, sp.Add(s.Size)}).(*ebiten.Image), &op) screen.DrawImage(src.SubImage(image.Rectangle{sp, sp.Add(s.FrameSize)}).(*ebiten.Image), &op)
} }
func (s *Sprite) Scan() []interface{} { return []interface{}{&s.Actor} } func (s *Sprite) Scan() []interface{} { return []interface{}{&s.Actor} }

View file

@ -73,8 +73,10 @@ func main() {
Actor: engine.Actor{ Actor: engine.Actor{
CollisionDomain: "level_1", CollisionDomain: "level_1",
Pos: image.Pt(100, 100), Pos: image.Pt(100, 100),
Size: image.Pt(10, 16), Size: image.Pt(8, 16),
}, },
FrameOffset: image.Pt(-1, 0),
FrameSize: image.Pt(10, 16),
Src: engine.ImageRef{Path: "assets/aw.png"}, Src: engine.ImageRef{Path: "assets/aw.png"},
ZPos: 2, ZPos: 2,
}, },