summaryrefslogtreecommitdiff
path: root/tilemap.lua
diff options
context:
space:
mode:
authorcursed22bc <admin@pixeldawn.org>2026-03-01 12:58:57 +0200
committercursed22bc <admin@pixeldawn.org>2026-03-01 12:58:57 +0200
commit937de6cf0402535def99bdd8056ac706afee2c82 (patch)
tree9e7694bdd3a90cc1c9862abd609f9dad612704e5 /tilemap.lua
parentde94226e1b302cee2a006f78f0153aa5fa081f47 (diff)
flexible multisized tiles in tilemap loading
Diffstat (limited to 'tilemap.lua')
-rw-r--r--tilemap.lua72
1 files changed, 46 insertions, 26 deletions
diff --git a/tilemap.lua b/tilemap.lua
index 4780cfe..ab2a52c 100644
--- a/tilemap.lua
+++ b/tilemap.lua
@@ -39,7 +39,7 @@ local function objectToEntity(obj)
return entity
end
-function Tilemap:new(mapPath, tilesetPath)
+function Tilemap:new(mapPath, tilesets)
local self = setmetatable({}, Tilemap)
self.entitiesTiles = {}
@@ -48,34 +48,40 @@ function Tilemap:new(mapPath, tilesetPath)
self.layerBackground = nil
self.layerGround = nil
self.layerForeground = nil
- self.tileWidth = 16
- self.tileHeight = 16
+ self.tileWidth = 8
+ self.tileHeight = 8
self.mapWidth = 0
self.mapHeight = 0
- self.tilesetImage = nil
- self.tileQuads = {}
+ self.tileGidInfo = {}
+ self.layerTileDimensions = {}
local mapData = loadMapData(mapPath)
self.mapData = mapData
- self.tileWidth = mapData and (mapData.tilewidth or 16) or 16
- self.tileHeight = mapData and (mapData.tileheight or 16) or 16
+ self.tileWidth = mapData and (mapData.tilewidth or 8) or 8
+ self.tileHeight = mapData and (mapData.tileheight or 8) or 8
self.mapWidth = (mapData and mapData.width or 0) * self.tileWidth
self.mapHeight = (mapData and mapData.height or 0) * self.tileHeight
- if type(tilesetPath) == "string" and tilesetPath ~= "" then
- local ok, img = pcall(love.graphics.newImage, tilesetPath)
- if ok and img then
- self.tilesetImage = img
- local firstGid = (mapData.tilesets and mapData.tilesets[1] and mapData.tilesets[1].firstgid) or 1
- local imgW, imgH = img:getDimensions()
- local tw, th = self.tileWidth, self.tileHeight
- local cols = math.floor(imgW / tw)
- local rows = math.floor(imgH / th)
- for index = 0, (cols * rows) - 1 do
- local qx = (index % cols) * tw
- local qy = math.floor(index / cols) * th
- self.tileQuads[firstGid + index] = love.graphics.newQuad(qx, qy, tw, th, imgW, imgH)
+ if mapData and mapData.tilesets then
+ for _, ts in ipairs(mapData.tilesets) do
+ local info = tilesets[ts.name]
+ if info then
+ local ok, img = pcall(love.graphics.newImage, info.path)
+ if ok and img then
+ local tw = info.tilewidth or self.tileWidth
+ local th = info.tileheight or self.tileHeight
+ local imgW, imgH = img:getDimensions()
+ local cols = math.floor(imgW / tw)
+ local rows = math.floor(imgH / th)
+ for index = 0, (cols * rows) - 1 do
+ local qx = (index % cols) * tw
+ local qy = math.floor(index / cols) * th
+ local gid = ts.firstgid + index
+ local quad = love.graphics.newQuad(qx, qy, tw, th, imgW, imgH)
+ self.tileGidInfo[gid] = { image = img, quad = quad, tilewidth = tw, tileheight = th }
+ end
+ end
end
end
end
@@ -88,6 +94,19 @@ function Tilemap:new(mapPath, tilesetPath)
elseif n == "ground" then self.layerGround = layer
elseif n == "foreground" then self.layerForeground = layer
end
+ local dims = nil
+ if layer.data then
+ for _, gid in ipairs(layer.data) do
+ if gid and gid ~= 0 then
+ local gi = self.tileGidInfo[gid]
+ if gi then
+ dims = { tilewidth = gi.tilewidth, tileheight = gi.tileheight }
+ break
+ end
+ end
+ end
+ end
+ self.layerTileDimensions[n] = dims or { tilewidth = self.tileWidth, tileheight = self.tileHeight }
elseif layer.type == "objectgroup" and layer.objects then
local name = (layer.name or ""):gsub("%s+", "_"):lower()
for _, obj in ipairs(layer.objects) do
@@ -169,6 +188,11 @@ function Tilemap:getTileHeight()
return self.tileHeight
end
+function Tilemap:getLayerTileDimensions(layerName)
+ return self.layerTileDimensions[layerName:lower()]
+ or { tilewidth = self.tileWidth, tileheight = self.tileHeight }
+end
+
function Tilemap:getMapWidth()
return self.mapWidth
end
@@ -177,12 +201,8 @@ function Tilemap:getMapHeight()
return self.mapHeight
end
-function Tilemap:getTilesetImage()
- return self.tilesetImage
-end
-
-function Tilemap:getTileQuads()
- return self.tileQuads
+function Tilemap:getTileGidInfo()
+ return self.tileGidInfo
end
return Tilemap