From 8f9182baff36d33c07c5eb8df795e3cfcd5307a5 Mon Sep 17 00:00:00 2001 From: cursed22bc Date: Sun, 1 Mar 2026 11:17:23 +0200 Subject: improved tileset and load --- main.lua | 7 ++++--- tilemap.lua | 12 ++++-------- world.lua | 6 +++--- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/main.lua b/main.lua index e44d670..3f25812 100644 --- a/main.lua +++ b/main.lua @@ -16,7 +16,8 @@ local camera = nil local fonts = require("fonts") local currentState = "game" -local currentLevel = "assets/maps/tilemap1.lua" +local currentMapPath = "assets/maps/tilemap.lua" +local currentTilesetPath = "assets/maps/tileset.png" local world = nil local states = { @@ -36,9 +37,9 @@ end function states.game.load() local World = require("world") world = World:new() - world:load(currentLevel) + world:load(currentMapPath, currentTilesetPath) local target = world:getPlayer() or { x = 0, y = 0, width = 16, height = 16 } - camera = cameraModule:new(target, VIRTUAL_WIDTH/3, VIRTUAL_HEIGHT/3, true, WORLD_TO_CANVAS) + camera = cameraModule:new(target, VIRTUAL_WIDTH/2, VIRTUAL_HEIGHT/2, true, WORLD_TO_CANVAS) world:setCamera(camera) end diff --git a/tilemap.lua b/tilemap.lua index 64ca520..4780cfe 100644 --- a/tilemap.lua +++ b/tilemap.lua @@ -39,7 +39,7 @@ local function objectToEntity(obj) return entity end -function Tilemap:new(map) +function Tilemap:new(mapPath, tilesetPath) local self = setmetatable({}, Tilemap) self.entitiesTiles = {} @@ -55,7 +55,7 @@ function Tilemap:new(map) self.tilesetImage = nil self.tileQuads = {} - local mapData = loadMapData(map) + local mapData = loadMapData(mapPath) self.mapData = mapData self.tileWidth = mapData and (mapData.tilewidth or 16) or 16 @@ -63,10 +63,8 @@ function Tilemap:new(map) self.mapWidth = (mapData and mapData.width or 0) * self.tileWidth self.mapHeight = (mapData and mapData.height or 0) * self.tileHeight - if type(map) == "string" then - local basePath = map:gsub("%.lua$", ""):gsub("%.tmx$", "") - local imagePath = basePath .. ".png" - local ok, img = pcall(love.graphics.newImage, imagePath) + 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 @@ -121,8 +119,6 @@ function Tilemap:getEntitiesCameraBorders() return self.entitiesCameraBorders end --- Returns minX, maxX, minY, maxY from camera_border entities for use with Camera:setLimits. --- Returns nil, nil, nil, nil if there are no camera border entities. function Tilemap:getCameraLimits() local borders = self.entitiesCameraBorders if not borders or #borders == 0 then diff --git a/world.lua b/world.lua index 2098040..6890e60 100644 --- a/world.lua +++ b/world.lua @@ -21,17 +21,17 @@ function World:new() return self end -function World:load(levelPath) +function World:load(mapPath, tilesetPath) if self.physicsWorld then self.groundEntities = {} end self.physicsWorld = love.physics.newWorld(0, GRAVITY) self.physicsWorld:setCallbacks(nil, nil, nil, nil) - self.tilemap = Tilemap:new(levelPath) + self.tilemap = Tilemap:new(mapPath, tilesetPath) self.mapData = self.tilemap:getMapData() if not self.mapData then - error("World:load - no map data from " .. tostring(levelPath)) + error("World:load - no map data from " .. tostring(mapPath)) end local tileWidth = self.tilemap:getTileWidth() -- cgit v1.2.3