neckrat 2fc1a92ad1 quad map refactor №1
Co-authored-by: Ivan Yuriev <ivanyr44@gmail.com>
2025-10-26 01:21:45 +03:00

56 lines
1.5 KiB
Lua

--- @class TileMap
--- @field tileSize integer
--- @field atlas love.Image
--- @field map table<string, love.Quad>
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
local _tileMap = {
tileSize = manifest.tileSize,
atlas = atlas,
map = {}
}
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, _tileMap.tileSize do
for x = 0, atlas:getWidth() - 1, _tileMap.tileSize do
if layout[cnt] then
for _, group in ipairs(layout[cnt]) do
print(x, y, _tileMap.tileSize, atlas, false)
local quad = love.graphics.newQuad(x, y, _tileMap.tileSize, _tileMap.tileSize, atlas)
if _tileMap.map[group] then
table.insert(_tileMap.map[group], quad)
else
_tileMap.map[group] = { quad }
end
end
end
cnt = cnt + 1
end
end
return setmetatable(_tileMap, tileMap)
end
return { load = load }