Tilemap = class("Tilemap", Sheet) function Tilemap:initialize(img, columns, rows, tileWidth, tileHeight, solid) Sheet.initialize(self, img, tileWidth, tileHeight) self.cols = columns self.rows = rows self.batch = love.graphics.newSpriteBatch(img) self.solid = solid if solid then self.map = {} end end function Tilemap:draw() love.graphics.draw(self.batch, self.x, self.y) end function Tilemap:set(x, y, index) if index > 0 then self.batch:addq(self.quads[index], x * self.tw, y * self.th) end if self.solid then self.map[y * self.cols + x] = index > 0 end end function Tilemap:load(t) local i = 1 for y = 0, self.rows - 1 do for x = 0, self.cols - 1 do if t[i] > 0 then self:set(x, y, t[i]) end i = i + 1 end end end function Tilemap:collideRect(x, y, width, height) x = math.floor((x - self.x) / self.tw) y = math.floor((y - self.y) / self.th) width = math.floor(width / self.tw) height = math.floor(height / self.th) for ix = x, width - 1 do for iy = y, height - 1 do if self.map[iy * self.cols + ix] then return true end end end return false end