support rectangular tile sources

This commit is contained in:
Josh Deprez 2021-08-02 16:32:27 +10:00 committed by Josh Deprez
parent 8ddfe28427
commit 19eb6361f5
3 changed files with 6 additions and 3 deletions

BIN
.DS_Store vendored

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

View file

@ -19,7 +19,7 @@ type Tilemap struct {
Hidden bool Hidden bool
ID ID
Map [][]Tile Map [][]Tile
Src ImageRef // must be a horizontal tile set Src ImageRef
TileSize int TileSize int
Transform GeoMDef Transform GeoMDef
ZPos ZPos
@ -30,6 +30,8 @@ func (t *Tilemap) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
if t.Hidden { if t.Hidden {
return return
} }
src := t.Src.Image()
w, _ := src.Size()
geom.Concat(*t.Transform.GeoM()) geom.Concat(*t.Transform.GeoM())
for j, row := range t.Map { for j, row := range t.Map {
for i, tile := range row { for i, tile := range row {
@ -40,8 +42,9 @@ func (t *Tilemap) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
op.GeoM.Translate(float64(i*t.TileSize), float64(j*t.TileSize)) op.GeoM.Translate(float64(i*t.TileSize), float64(j*t.TileSize))
op.GeoM.Concat(geom) op.GeoM.Concat(geom)
sx := tile.TileIndex() * t.TileSize s := tile.TileIndex() * t.TileSize
src := t.Src.Image().SubImage(image.Rect(sx, 0, sx+t.TileSize, t.TileSize)).(*ebiten.Image) 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, &op) screen.DrawImage(src, &op)
} }
} }