sheet, tile/sheet, misc math

This commit is contained in:
Josh Deprez 2021-08-26 13:54:22 +10:00
parent 9a65ce6104
commit a7ff475b91
5 changed files with 80 additions and 28 deletions

View file

@ -49,3 +49,21 @@ type ZOrder float64
// DrawOrder returns z as a float64. // DrawOrder returns z as a float64.
func (z ZOrder) DrawOrder() float64 { return float64(z) } func (z ZOrder) DrawOrder() float64 { return float64(z) }
// Some math helpers
func mul2(p, q image.Point) image.Point {
p.X *= q.X
p.Y *= q.Y
return p
}
func div2(p, q image.Point) image.Point {
p.X /= q.X
p.Y /= q.Y
return p
}
func float2(p image.Point) (float64, float64) {
return float64(p.X), float64(p.Y)
}

36
engine/sheet.go Normal file
View file

@ -0,0 +1,36 @@
package engine
import (
"image"
"github.com/hajimehoshi/ebiten/v2"
)
var (
_ Prepper = &Sheet{}
_ Scanner = &Sheet{}
)
// 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} }
// SubImage returns an *ebiten.Image corresponding to the cell of the given index.
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)
}

View file

@ -24,16 +24,15 @@ func init() {
gob.Register(&Tilemap{}) gob.Register(&Tilemap{})
} }
// Tilemap renders a grid of square tiles. // Tilemap renders a grid of rectangular tiles at equal Z position.
type Tilemap struct { type Tilemap struct {
ID ID
Disabled Disabled
Hidden Hidden
Map map[image.Point]Tile // tilespace coordinate -> tile Map map[image.Point]Tile // tilespace coordinate -> tile
Ersatz bool // "fake wall" Ersatz bool // "fake wall"
Offset image.Point // world coordinates Offset image.Point // world coordinates
Src ImageRef Sheet Sheet
TileSize int
ZOrder ZOrder
} }
@ -45,8 +44,8 @@ func (t *Tilemap) CollidesWith(r image.Rectangle) bool {
// Probe the map at all tilespace coordinates overlapping the rect. // Probe the map at all tilespace coordinates overlapping the rect.
r = r.Sub(t.Offset) r = r.Sub(t.Offset)
min := r.Min.Div(t.TileSize) min := div2(r.Min, t.Sheet.CellSize)
max := r.Max.Sub(image.Pt(1, 1)).Div(t.TileSize) // NB: fencepost max := div2(r.Max.Sub(image.Pt(1, 1)), t.Sheet.CellSize) // NB: fencepost
for j := min.Y; j <= max.Y; j++ { for j := min.Y; j <= max.Y; j++ {
for i := min.X; i <= max.X; i++ { for i := min.X; i <= max.X; i++ {
@ -63,8 +62,6 @@ func (t *Tilemap) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
if t.Hidden { if t.Hidden {
return return
} }
src := t.Src.Image()
w, _ := src.Size()
og := opts.GeoM og := opts.GeoM
var geom ebiten.GeoM var geom ebiten.GeoM
for p, tile := range t.Map { for p, tile := range t.Map {
@ -72,13 +69,12 @@ func (t *Tilemap) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
continue continue
} }
geom.Reset() geom.Reset()
geom.Translate(float64(p.X*t.TileSize+t.Offset.X), float64(p.Y*t.TileSize+t.Offset.Y)) //geom.Translate(float64(p.X*t.Sheet.CellSize.X+t.Offset.X), float64(p.Y*t.Sheet.CellSize.Y+t.Offset.Y))
geom.Translate(float2(mul2(p, t.Sheet.CellSize).Add(t.Offset)))
geom.Concat(og) geom.Concat(og)
opts.GeoM = geom opts.GeoM = geom
s := tile.TileIndex() * t.TileSize src := t.Sheet.SubImage(tile.CellIndex())
sx, sy := s%w, (s/w)*t.TileSize
src := src.SubImage(image.Rect(sx, sy, sx+t.TileSize, sy+t.TileSize)).(*ebiten.Image)
screen.DrawImage(src, &opts) screen.DrawImage(src, &opts)
} }
} }
@ -86,7 +82,7 @@ func (t *Tilemap) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
// Scan returns a slice containing Src and all non-nil tiles. // Scan returns a slice containing Src and all non-nil tiles.
func (t *Tilemap) Scan() []interface{} { func (t *Tilemap) Scan() []interface{} {
c := make([]interface{}, 1, len(t.Map)+1) c := make([]interface{}, 1, len(t.Map)+1)
c[0] = &t.Src c[0] = &t.Sheet
for _, tile := range t.Map { for _, tile := range t.Map {
c = append(c, tile) c = append(c, tile)
} }
@ -110,24 +106,24 @@ func (t *Tilemap) Update() error {
// TileAt returns the tile present at the given world coordinate. // TileAt returns the tile present at the given world coordinate.
func (t *Tilemap) TileAt(wc image.Point) Tile { func (t *Tilemap) TileAt(wc image.Point) Tile {
return t.Map[wc.Sub(t.Offset).Div(t.TileSize)] return t.Map[div2(wc.Sub(t.Offset), t.Sheet.CellSize)]
} }
// SetTileAt sets the tile at the given world coordinate. // SetTileAt sets the tile at the given world coordinate.
func (t *Tilemap) SetTileAt(wc image.Point, tile Tile) { func (t *Tilemap) SetTileAt(wc image.Point, tile Tile) {
t.Map[wc.Sub(t.Offset).Div(t.TileSize)] = tile t.Map[div2(wc.Sub(t.Offset), t.Sheet.CellSize)] = tile
} }
// TileBounds returns a rectangle describing the tile boundary for the tile // TileBounds returns a rectangle describing the tile boundary for the tile
// at the given world coordinate. // at the given world coordinate.
func (t *Tilemap) TileBounds(wc image.Point) image.Rectangle { func (t *Tilemap) TileBounds(wc image.Point) image.Rectangle {
p := wc.Sub(t.Offset).Div(t.TileSize).Mul(t.TileSize).Add(t.Offset) p := mul2(div2(wc.Sub(t.Offset), t.Sheet.CellSize), t.Sheet.CellSize).Add(t.Offset)
return image.Rectangle{p, p.Add(image.Pt(t.TileSize, t.TileSize))} return image.Rectangle{p, p.Add(t.Sheet.CellSize)}
} }
// Tile is the interface needed by Tilemap. // Tile is the interface needed by Tilemap.
type Tile interface { type Tile interface {
TileIndex() int CellIndex() int
} }
// Ensure StaticTile and AnimatedTile satisfy Tile. // Ensure StaticTile and AnimatedTile satisfy Tile.
@ -140,14 +136,14 @@ var (
// StaticTile returns a fixed tile index. // StaticTile returns a fixed tile index.
type StaticTile int type StaticTile int
func (s StaticTile) TileIndex() int { return int(s) } func (s StaticTile) CellIndex() int { return int(s) }
// AnimatedTile uses an Anim to choose a tile index. // AnimatedTile uses an Anim to choose a tile index.
type AnimatedTile struct { type AnimatedTile struct {
Animer Animer
} }
func (a AnimatedTile) TileIndex() int { return a.CurrentFrame() } func (a AnimatedTile) CellIndex() int { return a.CurrentFrame() }
// Scan returns a.Animer. (It could be a Loader.) // Scan returns a.Animer. (It could be a Loader.)
func (a AnimatedTile) Scan() []interface{} { return []interface{}{a.Animer} } func (a AnimatedTile) Scan() []interface{} { return []interface{}{a.Animer} }

Binary file not shown.

14
main.go
View file

@ -18,7 +18,7 @@ func main() {
ebiten.SetWindowSize(640, 480) ebiten.SetWindowSize(640, 480)
ebiten.SetWindowTitle("TODO") ebiten.SetWindowTitle("TODO")
if false { if true {
writeLevel1() writeLevel1()
} }
@ -109,11 +109,13 @@ func writeLevel1() {
Src: engine.ImageRef{Path: "assets/space.png"}, Src: engine.ImageRef{Path: "assets/space.png"},
}, },
&engine.Tilemap{ &engine.Tilemap{
ID: "terrain", ID: "terrain",
ZOrder: 2, ZOrder: 2,
Map: tiles, Map: tiles,
Src: engine.ImageRef{Path: "assets/boxes.png"}, Sheet: engine.Sheet{
TileSize: 16, CellSize: image.Pt(16, 16),
Src: engine.ImageRef{Path: "assets/boxes.png"},
},
}, },
&engine.SolidRect{ &engine.SolidRect{
ID: "ceiling", ID: "ceiling",