tilemap go brrrr

This commit is contained in:
Josh Deprez 2021-08-26 10:57:17 +10:00
parent 42aa119e7f
commit 2c94fc918c

View file

@ -24,14 +24,14 @@ func init() {
gob.Register(&Tilemap{}) gob.Register(&Tilemap{})
} }
// Tilemap renders a grid of tiles. // Tilemap renders a grid of square tiles.
type Tilemap struct { type Tilemap struct {
ID ID
Disabled Disabled
Hidden Hidden
Map map[image.Point]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 Src ImageRef
TileSize int TileSize int
ZOrder ZOrder
@ -43,19 +43,14 @@ func (t *Tilemap) CollidesWith(r image.Rectangle) bool {
return false return false
} }
// If we round down r.Min, and round up r.Max, to the nearest tile // Probe the map at all tilespace coordinates overlapping the rect.
// coordinates, that gives the full range of tiles to test.
sm1 := t.TileSize - 1
r = r.Sub(t.Offset) r = r.Sub(t.Offset)
min := r.Min.Div(t.TileSize) min := r.Min.Div(t.TileSize)
max := r.Max.Add(image.Pt(sm1, sm1)).Div(t.TileSize) max := r.Max.Sub(image.Pt(1, 1)).Div(t.TileSize) // 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++ {
if t.Map[image.Pt(i, j)] == nil { if t.Map[image.Pt(i, j)] != nil {
continue
}
if r.Overlaps(image.Rect(i*t.TileSize, j*t.TileSize, (i+1)*t.TileSize, (j+1)*t.TileSize)) {
return true return true
} }
} }