editor = { num = 1, map_surf = {nil,nil}, current_layer = 1, current_tile = 1, ox = 0, oy = 0, mx = 0, my = 0, init = function(level_name) game_update = editor.update editor.new() --if level_name then -- editor.current_layer = 2 --end end, new = function() editor.num = 1 editor.map_surf[1] = surf.new(256,256) editor.map_surf[2] = surf.new(256,256) editor.current_layer = 1 editor.current_tile = 1 editor.ox = 0 editor.oy = 0 end, load = function(level) local base_name = "level" .. tostring(level) local file = io.open("data/" .. base_name .. ".lev", "r") if file then for line in file:lines() do local key,value = line:match("^(.+)=(.+)$") if key and value then if key == "start-x" then editor.ox = tonumber(value) elseif key == "start-y" then editor.oy = tonumber(value) end end end editor.num = level surf.free(editor.map_surf[1]) surf.free(editor.map_surf[2]) editor.map_surf[1] = surf.load(base_name .. "_1.gif") editor.map_surf[2] = surf.load(base_name .. "_2.gif") else print("No s'ha trobat l'arxiu!") end end, save = function() local base_name = "level" .. tostring(editor.num) local file = io.open("data/" .. base_name .. ".lev", "w") if file then file:write("start-x=" .. tostring(editor.ox) .. "\n") file:write("start-y=" .. tostring(editor.oy) .. "\n") end surf.save(editor.map_surf[1], "data/" .. base_name .. "_1.gif") surf.save(editor.map_surf[2], "data/" .. base_name .. "_2.gif") end, update = function() view.origin(editor.ox,editor.oy) surf.cls(66) pal.trans(0) surf.source(tiles) map.surf(editor.map_surf[1]) map.draw() --batman.draw() surf.source(tiles) map.surf(editor.map_surf[2]) map.draw() map.surf(editor.map_surf[editor.current_layer]) local mx,my = mouse.pos() local tx = (mx//8)*8 local ty = (my//8)*8 draw.rect(tx-1,ty-1,10, 10, 9) if key.press(key.TAB) then game_update = editor.tile_picker_update elseif key.press(key.RIGHT) then editor.ox = editor.ox - 8 elseif key.press(key.LEFT) then editor.ox = editor.ox + 8 elseif key.press(key.DOWN) then editor.oy = editor.oy - 8 elseif key.press(key.UP) then editor.oy = editor.oy + 8 elseif key.press(key.N1) then editor.current_layer=1 elseif key.press(key.N2) then editor.current_layer=2 elseif key.press(key.N2) then editor.current_layer=2 elseif key.press(key.F) then editor.fill(tx//8, ty//8, map.tile(mx/8, my/8)) elseif key.down(key.LCTRL) and key.press(key.S) then editor.save() elseif key.down(key.LCTRL) and key.press(key.L) then editor.load(1) end if mouse.down(mouse.LEFT) then map.tile(mx/8, my/8, editor.current_tile) elseif mouse.press(mouse.RIGHT) then editor.current_tile = map.tile(mx/8, my/8) end view.origin(0,0) draw.text(tostring(mx//8)..","..tostring(my//8),50,0,22) mx,my = mouse.pos() if key.down(key.SPACE) then if editor.mx == -1 then editor.mx,editor.my = mx,my end editor.ox = editor.ox + 8*(mx-editor.mx) editor.oy = editor.oy + 8*(my-editor.my) editor.mx,editor.my = mx,my else editor.mx = -1 end text(tostring(editor.current_layer),0,0,22,42) end, fill = function(x, y, tile) local stack = {} table.insert(stack, {x = x, y = y}) while #stack > 0 do local mw, mh = surf.size(editor.map_surf[1]) local pos = table.remove(stack) local px, py = pos.x, pos.y map.tile(px, py, editor.current_tile) if px0 and map.tile(px-1, py) == tile then table.insert(stack, {x = px - 1, y = py}) end if py0 and map.tile(px, py-1) == tile then table.insert(stack, {x = px, y = py - 1}) end end end, tile_picker_update = function() view.origin(0,0) surf.cls(66) pal.trans(255) surf.source(tiles) local sw,sh = surf.size(tiles) draw.surf(0,0,sw,sh,0,0) if key.press(key.ESCAPE) then game_update = editor.update end local mx,my = mouse.pos() local tx = (mx//8)*8 local ty = (my//8)*8 draw.rect(tx-1,ty-1,10, 10, 9) if mouse.press(mouse.LEFT) then editor.current_tile = (tx//8) + (ty//8) * (sw//8) game_update = editor.update end end }