2021-08-26 13:54:22 +10:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
)
|
|
|
|
|
2021-08-27 11:49:11 +10:00
|
|
|
var _ interface {
|
|
|
|
Prepper
|
|
|
|
Scanner
|
|
|
|
} = &Sheet{}
|
2021-08-26 13:54:22 +10:00
|
|
|
|
|
|
|
// Sheet handles images that consist of a grid of equally sized regions
|
|
|
|
// (cells) and can produce subimages for the cell at an index. This is useful
|
|
|
|
// for various applications such as sprite animation and tile maps.
|
|
|
|
type Sheet struct {
|
|
|
|
CellSize image.Point
|
|
|
|
Src ImageRef
|
|
|
|
|
|
|
|
w int // width as measured in number of cells
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sheet) Prepare(*Game) {
|
|
|
|
s.w, _ = s.Src.Image().Size()
|
|
|
|
s.w /= s.CellSize.X
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sheet) Scan() []interface{} { return []interface{}{&s.Src} }
|
|
|
|
|
2021-08-26 14:55:31 +10:00
|
|
|
// SubImage returns an *ebiten.Image corresponding to the cell at the given
|
|
|
|
// index.
|
2021-08-26 13:54:22 +10:00
|
|
|
func (s *Sheet) SubImage(i int) *ebiten.Image {
|
|
|
|
p := mul2(image.Pt(i%s.w, i/s.w), s.CellSize)
|
|
|
|
r := image.Rectangle{p, p.Add(s.CellSize)}
|
|
|
|
return s.Src.Image().SubImage(r).(*ebiten.Image)
|
|
|
|
}
|