-- tilemap test 03 local sti = require "sti" local scroll_type function love.load() -- init -- set filter love.graphics.setDefaultFilter("nearest", "nearest") -- set canvas size scr_w = 640 scr_h = 480 canvas = love.graphics.newCanvas(scr_w, scr_h) -- get window width and height wdw_w, wdw_h = love.graphics.getDimensions() scr_scale = math.min((wdw_w / scr_w), (wdw_h / scr_h)) scr_ofsx = (wdw_w - (scr_w * scr_scale)) / 2 scr_ofsy = (wdw_h - (scr_h * scr_scale)) / 2 -- load tilemap map = sti("mecha_bg_map.lua") map.getGidByPixel = function(self, x, y, layerindex) local tilex, tiley = self:convertPixelToTile(math.floor(x), math.floor(y)) tilex = math.floor(tilex) tiley = math.floor(tiley) local layer = map.layers[layerindex] local tilew = layer.width local tileh = layer.height local gid = -2 if tilex >= 0 and tiley >= 0 and tilex < tilew and tiley < tileh then local tile = layer.data[tiley + 1][tilex + 1] if tile == nil then gid = -1 else gid = tile.gid end end return gid end scroll_type = 2 layers = {} for i,layer in ipairs(map.layers) do -- print(i, layer.name) layers[i] = layer end -- init bg position tx_start = 0 ty_start = 0 tx = tx_start ty = ty_start gid = 0 -- framerate steady min_dt = 1 / 60 next_time = love.timer.getTime() end function love.update(dt) -- update next_time = next_time + min_dt map:update(dt) -- keyboard check local speed = 160 * dt local kd = love.keyboard.isDown if kd("left") or kd("a") then tx = tx - speed elseif kd("right") or kd("d") then tx = tx + speed end if kd("up") or kd("w") then ty = ty - speed elseif kd("down") or kd("s") then ty = ty + speed end if kd("r") then tx = tx_start ty = ty_start end if scroll_type == 0 then map.layers["bg_a"].x = 0 map.layers["bg_a"].y = 0 map.layers["bg_b"].x = 0 map.layers["bg_b"].y = 0 elseif scroll_type == 1 then layers[2].x = -tx layers[2].y = -ty layers[1].x = -tx / 2 layers[1].y = -ty / 2 elseif scroll_type == 2 then map.layers["bg_a"].x = -tx map.layers["bg_a"].y = -ty map.layers["bg_b"].x = -tx / 4 map.layers["bg_b"].y = -ty / 4 end -- get mouse position local mx, my = love.mouse.getPosition() mx = (mx - scr_ofsx) / scr_scale my = (my - scr_ofsy) / scr_scale -- get tile gid mx = mx + tx my = my + ty gid = map:getGidByPixel(mx, my, "bg_a") end function love.draw() -- set canvas love.graphics.setCanvas(canvas) -- draw BG color love.graphics.setColor(0, 0, 0) love.graphics.rectangle("fill", 0, 0, scr_w, scr_h) -- draw tilemap BG love.graphics.setColor(255, 255, 255) if scroll_type == 0 then map:draw(-tx, -ty) elseif scroll_type == 1 then for i, layer in ipairs(layers) do map:drawLayer(layer) end elseif scroll_type == 2 then map:draw() end -- unset canvas love.graphics.setCanvas() -- draw canvas to window love.graphics.setColor(255, 255, 255) love.graphics.draw(canvas, scr_ofsx, scr_ofsy, 0, scr_scale, scr_scale) love.graphics.print("FPS: "..tostring(love.timer.getFPS()), 10, 10) love.graphics.print("tx = "..tostring(tx), 10, 40) love.graphics.print("ty = "..tostring(ty), 10, 60) love.graphics.print("C : scroll type = "..tostring(scroll_type), 10, 80) love.graphics.print("R : reset", 10, 100) love.graphics.print("ESC : exit", 10, 120) love.graphics.print("gid = "..tostring(gid), 10, 160, 0, 2.0, 2.0) if love.system.getOS() == "Windows" then -- wait local cur_time = love.timer.getTime() if next_time <= cur_time then next_time = cur_time else love.timer.sleep(next_time - cur_time) end end end function love.keypressed(key, isrepeat) -- ESC to exit if key == "escape" then love.event.quit() end if key == "c" then scroll_type = (scroll_type + 1) % 3 end end