-- tilemap test 04 get object -- -- use sti library -- https://github.com/karai17/Simple-Tiled-Implementation local sti = require "sti" function love.load() love.graphics.setDefaultFilter("nearest", "nearest") scr_w, scr_h = 640, 480 canvas = love.graphics.newCanvas(scr_w, scr_h) -- load image mark_img = love.graphics.newImage("enemy_mark.png") mark_quads = {} for i, k in ipairs({"a", "b", "c", "d"}) do local x, y = (i - 1) * 32, 0 local w, h = 32, 32 mark_quads[k] = love.graphics.newQuad(x, y, w, h, mark_img:getDimensions()) end -- load tilemap map = sti("mecha_bg2_map_with_enemy.lua") map.getGidByPixel = function(self, x, y, layer_name) local tilex, tiley = self:convertPixelToTile(math.floor(x), math.floor(y)) tilex = math.floor(tilex) tiley = math.floor(tiley) local layer = map.layers[layer_name] local tilew, tileh = layer.width, layer.height if tilex < 0 or tilex >= tilew or tiley < 0 or tiley >= tileh then return -2 end local tile = layer.data[tiley + 1][tilex + 1] if tile == nil then return -1 end return tile.gid end -- get map objects enemy_objs = {} for k, obj in pairs(map.objects) do local x, y, w, h = obj.x, obj.y, obj.width, obj.height x = x + w / 2 y = y + h / 2 table.insert(enemy_objs, {kind=obj.name, x=x, y=y}) end -- sort objects by y table.sort(enemy_objs, function(a, b) return (a.y > b.y) end) map.layers["enemy_tbl"].visible = false tx_start = 32 ty_start = 480 * 7 tx = tx_start ty = ty_start gid = 0 -- framerate steady min_dt = 1 / 60 next_time = love.timer.getTime() end function love.update(dt) next_time = next_time + min_dt 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 map:update(dt) -- keyboard check local speed = 160 * dt local kd = love.keyboard.isDown if kd("left") or kd("a") then tx = tx - speed end if kd("right") or kd("d") then tx = tx + speed end if kd("up") or kd("w") then ty = ty - speed end if kd("down") or kd("s") then ty = ty + speed end map.layers["enemy_tbl"].x = -tx map.layers["enemy_tbl"].y = -ty map.layers["bg_a"].x = -tx map.layers["bg_a"].y = -ty map.layers["bg_b"].x = -tx / 2 map.layers["bg_b"].y = -ty / 2 map.layers["bg_c"].x = -tx / 4 map.layers["bg_c"].y = -ty / 4 -- 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() love.graphics.setCanvas(canvas) love.graphics.clear(0, 0, 0, 255) -- draw tilemap BG love.graphics.setColor(255, 255, 255, 255) map:draw() -- draw objects love.graphics.setColor(255, 255, 255, 255) for i, obj in ipairs(enemy_objs) do local x = obj.x - tx local y = obj.y - ty love.graphics.draw(mark_img, mark_quads[obj.kind], x, y, 0, 1, 1, 16, 16) end 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("R : reset", 10, 100) love.graphics.print("ESC : exit", 10, 120) love.graphics.print("objs = "..tostring(#enemy_objs), 10, 140) love.graphics.print("gid = "..tostring(gid), 10, 160) 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) if key == "escape" then -- ESC to exit love.event.quit() elseif key == "r" then -- reset scroll position tx = tx_start ty = ty_start end end