methods for working with tiles

This commit is contained in:
Josh Deprez 2021-08-15 16:40:01 +10:00
parent c1e9570403
commit a6336dadc9

View file

@ -92,6 +92,23 @@ func (t *Tilemap) Update() error {
return nil
}
// TileAt returns the tile present at the given world coordinate.
func (t *Tilemap) TileAt(wc image.Point) Tile {
return t.Map[wc.Sub(t.Offset).Div(t.TileSize)]
}
// SetTileAt sets the tile at the given world coordinate.
func (t *Tilemap) SetTileAt(wc image.Point, tile Tile) {
t.Map[wc.Sub(t.Offset).Div(t.TileSize)] = tile
}
// TileBounds returns a rectangle describing the tile boundary for the tile
// at the given world coordinate.
func (t *Tilemap) TileBounds(wc image.Point) image.Rectangle {
p := wc.Sub(t.Offset).Div(t.TileSize).Mul(t.TileSize).Add(t.Offset)
return image.Rectangle{p, p.Add(image.Pt(t.TileSize, t.TileSize))}
}
// Tile is the interface needed by Tilemap.
type Tile interface {
TileIndex() int