2025-08-19 20:26:57 +03:00

52 lines
1.3 KiB
Lua

--- @class TileMap
--- @field tileSize integer
--- @field atlas love.Image
local tileMap = {}
tileMap.__index = tileMap
--- @param path table
local function load(path)
--- @type love.Image
local atlas = path.atlas
--- @type table
local manifest = path.manifest
--- @type TileMap
local tiles = {
tileSize = manifest.tileSize,
atlas = atlas
}
manifest.tileSize = nil
local layout = {}
for group, ids in pairs(manifest) do
for _, id in ipairs(ids) do
if layout[id] then
table.insert(layout[id], group)
else
layout[id] = { group }
end
end
end
local cnt = 0
for y = 0, atlas:getHeight() - 1, tiles.tileSize do
for x = 0, atlas:getWidth() - 1, tiles.tileSize do
if layout[cnt] then
for _, group in ipairs(layout[cnt]) do
if tiles[group] then
table.insert(tiles[group], love.graphics.newQuad(x, y, tiles.tileSize, tiles.tileSize, atlas))
else
tiles[group] = { love.graphics.newQuad(x, y, tiles.tileSize, tiles.tileSize, atlas) }
end
end
end
cnt = cnt + 1
end
end
return setmetatable(tiles, tileMap)
end
return { load = load }