Files
jailsadventure2/data/game.lua

211 lines
5.9 KiB
Lua

require "actors"
require "menu"
require "objects"
require "scene"
require "wait"
require "switches"
require "brymode"
levels={}
require "m_arq"
require "m_jail1"
require "m_prac1"
require "m_prac2"
require "m_prac3"
require "m_repro"
game={
cam={x=0,y=0},
restart=function()
flags={}
objects.list={}
actors.main={name="jailer",x=9,y=14,o="u",gfx={x=32,y=0},level="repro"}
game.init("repro")
end,
init=function(levelname,f,objs)
if f then flags=f end
if objs then objects.list=objs end
game.level=levelname
actors.init()
switches.reset()
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
levels[game.level].load()
if game.paused then
game.resume()
else
update=game.update
end
actors.sort()
fade.fadein()
end,
show_menu=function()
game.pause()
--brymode.show(game.resume)
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,
draw=function()
cls(6)
camera(game.cam.x, game.cam.y)
setsource(tiles)
setmap(mapa.surface)
map(0,0,0,0,mapa.w, mapa.h)
switches.draw();
setsource(sprites)
if btn(KEY_LSHIFT) then subpal(1,16,8) end
actors.draw()
subpal()
local mx,my=mouse()
if actors.under_cursor~="" then
if (mx<=80) then
text(actors.under_cursor,mx+game.cam.x+4,my+game.cam.y+4,9)
else
local size = #actors.under_cursor
text(actors.under_cursor,mx+game.cam.x-size*4,my+game.cam.y+4,9)
end
end
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)
for i=0,15 do rectfill(20+i*4,0,20+i*4+4,4,i) end
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+160 > mapa.w*8 then game.cam.x = mapa.w*8-160 end
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+144 > mapa.h*8 then game.cam.y = mapa.h*8-144 end
if game.cam.y < 0 then game.cam.y=0 end
end
game.draw()
local mx,my=mouse()
text(math.floor((mx+game.cam.x)/8)..","..math.floor((my+game.cam.y)/8),1,19,8)
text(game.cam.x..","..game.cam.y,1,1,8)
if hero then
text(hero.x..","..hero.y,1,7,8)
end
text(game.level,1,13,8)
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}
elseif btnp(KEY_SPACE) then
actors.interact(hero.x,hero.y,hero.o)
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]
blit(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
}