-- Minimal Tiled (TMX) map loader for simple orthogonal maps with CSV layers. local TMX = {} local function attr(tag, name) local v = tag:match(name .. '="([^"]*)"') return v and tonumber(v) or nil end function TMX.load(path) local text = love.filesystem.read(path) local header = text:match(']*>') if not header then error('TMX.load: no tag in ' .. path) end local map = { width = attr(header, 'width'), height = attr(header, 'height'), tileWidth = attr(header, 'tilewidth') or 16, tileHeight = attr(header, 'tileheight') or 16, } local imgSrc = text:match(']*source="([^"]+)"') if not imgSrc then error('TMX.load: no tag in ' .. path) end map.imagePath = imgSrc local csv = text:match('(.-)') if not csv then error('TMX.load: no CSV layer data in ' .. path) end local tiles = {} for gid in csv:gmatch('%d+') do tiles[#tiles + 1] = tonumber(gid) end map.tiles = tiles local expected = map.width * map.height if #tiles ~= expected then error(('TMX.load: %s has %d tiles, expected %d'):format(path, #tiles, expected)) end return map end function TMX.isSolid(map, tx, ty) if tx < 0 or ty < 0 or tx >= map.width or ty >= map.height then return true -- out of bounds counts as solid so the player can't leave end return (map.tiles[ty * map.width + tx + 1] or 0) > 0 -- tiles are 1-indexed end return TMX