- [NEW] Módul propi per al MAPA

- [WIP] Batman ja camina, bota, cau, puja i baixa escales
This commit is contained in:
2025-11-07 14:16:05 +01:00
parent cc2f1803a4
commit 14dc8a0948
5 changed files with 309 additions and 141 deletions

View File

@@ -1,11 +1,6 @@
require "mapa"
editor = {
num = 1,
map_surf = {nil,nil},
current_layer = 1,
current_tile = 1,
ox = 0,
oy = 0,
mx = 0,
my = 0,
@@ -18,65 +13,19 @@ editor = {
end,
new = function()
editor.num = 1
editor.map_surf[1] = surf.new(256,256)
editor.map_surf[2] = surf.new(256,256)
mapa.new()
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)
view.origin(mapa.x,mapa.y)
surf.cls(66)
pal.trans(0)
surf.source(tiles)
map.surf(editor.map_surf[1])
map.draw()
mapa.draw(1)
--batman.draw()
mapa.draw(2)
surf.source(tiles)
map.surf(editor.map_surf[2])
map.draw()
map.surf(editor.map_surf[editor.current_layer])
map.surf(mapa.surf[editor.current_layer])
local mx,my = mouse.pos()
@@ -87,25 +36,23 @@ editor = {
if key.press(key.TAB) then
game_update = editor.tile_picker_update
elseif key.press(key.RIGHT) then
editor.ox = editor.ox - 8
mapa.x = mapa.x - 8
elseif key.press(key.LEFT) then
editor.ox = editor.ox + 8
mapa.x = mapa.x + 8
elseif key.press(key.DOWN) then
editor.oy = editor.oy - 8
mapa.y = mapa.y - 8
elseif key.press(key.UP) then
editor.oy = editor.oy + 8
mapa.y = mapa.y + 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))
mapa.fill(tx//8, ty//8, map.tile(mx/8, my/8), editor.current_tile)
elseif key.down(key.LCTRL) and key.press(key.S) then
editor.save()
mapa.save()
elseif key.down(key.LCTRL) and key.press(key.L) then
editor.load(1)
mapa.load(1)
end
if mouse.down(mouse.LEFT) then
@@ -122,8 +69,8 @@ editor = {
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)
mapa.x = mapa.x + 8*(mx-editor.mx)
mapa.y = mapa.y + 8*(my-editor.my)
editor.mx,editor.my = mx,my
else
editor.mx = -1
@@ -132,25 +79,6 @@ editor = {
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 px<mw-1 and map.tile(px+1, py) == tile then table.insert(stack, {x = px + 1, y = py}) end
if px>0 and map.tile(px-1, py) == tile then table.insert(stack, {x = px - 1, y = py}) end
if py<mh-1 and map.tile(px, py+1) == tile then table.insert(stack, {x = px, y = py + 1}) end
if py>0 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)