Files
jailsadventure2/data/game.lua
Raimon Zamora 5091fe267b - Implementat el segon layer
- Implementat resizar el mapa
2023-02-02 18:54:40 +01:00

186 lines
5.8 KiB
Lua

game={
cam={x=0,y=0},
restart=function()
flags={}
objects.list={}
actors.main={name="jailer",x=6,y=9,o="r",gfx={x=32,y=0},level="jail"}
game.init("jail")
end,
init=function(levelname,f,objs)
if f then flags=f end
if objs then objects.list=objs end
game.level=levelname
actors.init()
if actors.main.level==levelname then
actors.add({name=actors.main.name,x=actors.main.x,y=actors.main.y,o=actors.main.o,gfx=actors.main.gfx})
end
mapa.load(game.level..".map")
actors.add({name="estudiant",x=12,y=8,o="d",gfx={x=64,y=0}})
switches.add({x=8,y=8,w=1,h=3,action=
function()
if not flags.usu1 then
flags.usu1=1
scene.start({
function() actors.add({name="usufondo",x=16,y=8,o="d",gfx={x=0,y=0}}) scene.cont() end,
function() wait.start(1) end,
function() actors.search("usufondo").path={pos=0,route='dlllll'} end,
function() balloon.show("HOY NO HAY JAIL!\nTU MISMO!!",10,"usufondo",false,{x=7,w=10,h=2}) end,
function() objects.collect("diskito_usufondo") end,
function() actors.search("usufondo").path={pos=0,route='rrrrru'} end,
function() wait.start(0.5) end,
function() actors.remove("usufondo") scene.cont() end,
function() wait.start(1) end,
function() balloon.show("IMBÈSIL...",2,"jailer",true,{x=5,w=7,h=1}) end
})
return true
else
return false
end
end
})
if game.paused then
game.resume()
else
update=game.update
end
actors.sort()
fade.fadein()
end,
show_menu=function()
game.pause()
menu.show({ {"GUARDAR PARTIDA", game.save},
{"CARREGAR PARTIDA", game.load},
{"EIXIR", main_init},
}, game.resume)
end,
save=function()
local i=1
file = io.open(configfolder().."slot"..i..".txt", "w")
if file then
io.output(file)
io.write("level="..game.level.."\n")
local hero = actors.search(actors.main.name)
io.write("x="..hero.x.."\n")
io.write("y="..hero.y.."\n")
io.write("o="..hero.o.."\n")
io.write("[FLAGS]\n")
for k,v in pairs(flags) do
io.write(k.."="..v.."\n")
end
io.write("[OBJECTS]\n")
for i,v in ipairs(objects.list) do
io.write(v.."\n")
end
io.close(file)
end
game.resume()
end,
load=function()
local i=1
file = io.open(configfolder().."slot"..i..".txt", "r")
local level_name = ""
local new_flags={}
local new_objects={}
if file then
io.input(file)
local k,v=getkeyval(io.read())
level_name=v
local k,v=getkeyval(io.read()) actors.main.x=tonumber(v)
local k,v=getkeyval(io.read()) actors.main.y=tonumber(v)
local k,v=getkeyval(io.read()) actors.main.o=v
io.read() -- ignore "[FLAGS]" line
local line = io.read()
while line ~= "[OBJECTS]" do
local k,v=getkeyval(line)
new_flags[k]=v
line = io.read()
end
line = io.read()
while line do
table.insert(new_objects, line)
line = io.read()
end
io.close(file)
end
fade.fadeout()
--game.resume()
--actors.main.x=10
--actors.main.y=8
game.init(level_name,new_flags,new_objects)
end,
update=function()
if game.paused then return end
local hero = actors.search("jailer")
if hero then
game.cam.x = hero.x*8 + hero.dx*2 - 80
if game.cam.x < 0 then game.cam.x=0 end
game.cam.y = hero.y*8 + hero.dy*2 - 72
if game.cam.y < 0 then game.cam.y=0 end
end
camera(game.cam.x, game.cam.y)
setsource(tiles)
setmap(mapa.surface)
map(0,0,0,0,mapa.w, mapa.h)
setsource(sprites)
actors.draw()
if mapa.front_layer then
setsource(tiles)
setmap(mapa.front_layer)
map(0,0,0,0,mapa.w, mapa.h)
end
setmap(mapa.surface)
camera(0,0)
if not scene.script and hero and not hero.path and hero.dx+hero.dy==0 then
if btn(KEY_DOWN) then
hero.path={pos=0,route='d',keys=true}
elseif btn(KEY_UP) then
hero.path={pos=0,route='u',keys=true}
elseif btn(KEY_LEFT) then
hero.path={pos=0,route='l',keys=true}
elseif btn(KEY_RIGHT) then
hero.path={pos=0,route='r',keys=true}
end
end
if btnp(KEY_ESCAPE) then
game.show_menu()
end
if beat() then actors.update() end
end,
pause=function()
game.paused = true
rectfill(9,84, 150, 128,6)
rect(9,84, 150, 128,8)
rect(8,83, 151, 129,6)
text("OBJECTES:",14,82,8)
setsource(objectes)
local x,y=15,90
for i,name in ipairs(objects.list) do
local obj = object[name]
sspr(obj.x,obj.y,16,16,x,y)
x=x+19
if x==148 then x,y=15,y+19 end
end
end,
resume=function()
game.paused = nil
end
}