Files
jailsadventure2/data/mapa.lua
Raimon Zamora 2dd36562da - [FIX] Reajustada la numeració de tiles
- [NEW] Comencem a treballar en la EUI
2024-12-31 10:44:39 +01:00

89 lines
2.4 KiB
Lua

mapa={
name=nil,
w=128,
h=128,
surface=nil,
new = function(w,h)
mapa.name=nil
mapa.w,mapa.h=w,h
if mapa.surface~=nil then freesurf(mapa.surface) end
mapa.surface=newsurf(w,h)
setmap(mapa.surface)
for y=0,127 do
for x=0,127 do
mset(x,y,0)
end
end
mapa.front_layer=nil
end,
load = function(filename)
file = io.open("data/"..filename, "r")
if file then
io.input(file)
mapa.w = tonumber(io.read())
mapa.h = tonumber(io.read())
local num_layers=tonumber(io.read())
mapa.new(mapa.w, mapa.h)
mapa.name=filename
for y=0,mapa.h-1 do
local line = io.read()
local x=0
for m in string.gmatch(line, "%d+") do
mset(x,y,m)
x=x+1
end
end
if num_layers==2 then
mapa.front_layer=newsurf(mapa.w,mapa.h)
setmap(mapa.front_layer)
for y=0,mapa.h-1 do
local line = io.read()
local x=0
for m in string.gmatch(line, "%d+") do
mset(x,y,m)
x=x+1
end
end
end
setmap(mapa.surface)
io.close(file)
end
end,
save = function(filename)
mapa.name=filename
file = io.open("data/"..filename, "w")
if file then
io.output(file)
io.write(mapa.w.."\n")
io.write(mapa.h.."\n")
if mapa.front_layer then io.write("2\n") else io.write("1\n") end
setmap(mapa.surface)
for y=0,mapa.h-1 do
for x=0,mapa.w-1 do
io.write(mget(x,y)..",")
end
io.write("\n")
end
if mapa.front_layer then
setmap(mapa.front_layer)
for y=0,mapa.h-1 do
for x=0,mapa.w-1 do
io.write(mget(x,y)..",")
end
io.write("\n")
end
end
io.close(file)
end
end,
check_collision=function(x,y)
return mget(x,y) >= 32 or mget(x+1,y) >= 32
end
}