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,7 +14,9 @@ func init() {
// Sprite combines an Actor with the ability to Draw from a single spritesheet.
type Sprite struct {
Actor
Hidden bool
FrameSize image.Point
FrameOffset image.Point
Hidden bool
ID
Src ImageRef
ZPos
@ -27,15 +29,16 @@ func (s *Sprite) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
return
}
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)
frame := s.anim.CurrentFrame()
src := s.Src.Image()
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} }

View file

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