Files
jailsadventure2/data/actors.lua

219 lines
7.6 KiB
Lua

actors={
list={},
updating=false,
main={},
under_cursor="",
surface=0,
should_free_surf=false,
init=function()
for i,actor in ipairs(actors.list) do
if actor.should_free_surf then
surf.free(actor.surface)
end
end
actors.list={}
actors.updating=false
end,
add=function(actor)
actor.dx,actor.dy=0,0
if actor.jailmoji then
actor.surface=jailmoji.get(actor.jailmoji)
actor.gfx={x=0,y=0}
else
actor.surface=surf.load("sprites.gif")
end
table.insert(actors.list, actor)
end,
remove=function(name)
for i,v in ipairs(actors.list) do
if v.name and v.name==name then
table.remove(actors.list,i)
return
end
end
end,
search=function(name)
for i,v in ipairs(actors.list) do
if v.name and v.name==name then
return v
end
end
end,
face=function(name, focus)
local o = actors.search(focus).o
if o=='l' then
o='r'
elseif o=='r' then
o='l'
elseif o=='u' then
o='d'
elseif o=='d' then
o='u'
end
actors.search(name).o = o
end,
display=function()
actors.under_cursor=""
for i,v in ipairs(actors.list) do
if v.o=='' then goto continue end
local frame=((v.dx+v.dy)%2)*16
local x,y = v.x, v.y
if v.ox then x = x + v.ox end
if v.oy then y = y + v.oy end
local mousex,mousey=mouse.pos()
local mx = math.floor((mousex+game.cam.x)/8)
local my = math.floor((mousey+game.cam.y)/8)
if (mx==x or mx==x+1) and (my==y or my==y-1) then
actors.under_cursor=v.name
pal.subpal(1,16,8)
end
surf.source(v.surface)
if v.o=='u' then
draw.surf(v.gfx.x+frame,v.gfx.y+16,16,16,x*8+v.dx*2,y*8-12+v.dy*2,16,16,v.dy>1)
elseif v.o=='d' then
draw.surf(v.gfx.x+frame,v.gfx.y,16,16,x*8+v.dx*2,y*8-12+v.dy*2,16,16,-v.dy>2)
elseif v.o=='l' then
draw.surf(v.gfx.x+frame,v.gfx.y+32,16,16,x*8+v.dx*2,y*8-12+v.dy*2,16,16)
elseif v.o=='r' then
draw.surf(v.gfx.x+frame,v.gfx.y+32,16,16,x*8+v.dx*2,y*8-12+v.dy*2,16,16,true)
end
if not key.down(key.LSHIFT) then pal.subpal() end
::continue::
end
end,
check_collision=function(x,y,o)
for i,actor in ipairs(actors.list) do
if o=='u' then
if actor.y==y-1 and (actor.x==x-1 or actor.x==x or actor.x==x+1) then return true end
elseif o=='d' then
if actor.y==y+1 and (actor.x==x-1 or actor.x==x or actor.x==x+1) then return true end
elseif o=='l' then
if actor.y==y and actor.x==x-2 then return true end
elseif o=='r' then
if actor.y==y and actor.x==x+2 then return true end
end
end
return false
end,
interact=function(x,y,o)
for i,actor in ipairs(actors.list) do
if o=='d' then
if actor.x==x and actor.y==y+1 then if actor.action then actor.action() end end
elseif o=='u' then
if actor.x==x and actor.y==y-1 then if actor.action then actor.action() end end
elseif o=='l' then
if actor.y==y and actor.x==x-2 then if actor.action then actor.action() end end
elseif o=='r' then
if actor.y==y and actor.x==x+2 then if actor.action then actor.action() end end
end
end
end,
moveto=function(name,dx,dy)
local actor = actors.search(name)
if actor.x==dx and actor.y==dy then
actor.path=nil
return
end
actor.path={pos=0,route=""}
if (actor.x<dx) then for i=1,dx-actor.x do actor.path.route=actor.path.route.."r" end end
if (actor.x>dx) then for i=1,actor.x-dx do actor.path.route=actor.path.route.."l" end end
if (actor.y<dy) then for i=1,dy-actor.y do actor.path.route=actor.path.route.."d" end end
if (actor.y>dy) then for i=1,actor.y-dy do actor.path.route=actor.path.route.."u" end end
end,
update=function()
if actors.updating then return end
actors.updating=true
local needs_sorting=false
for i,v in ipairs(actors.list) do
if v.dx==0 and v.dy==0 then
if v.path then
if v.path.pos == #v.path.route then
local in_scene = not v.path.keys
v.path=nil
game.update()
if in_scene then scene.cont() end
if v.name == actors.main.name then
local switch = switches.search(v.x,v.y)
if switch then
if switch.action() then v.path=nil end
end
end
end
if v.path and v.path.pos ~= #v.path.route then
v.path.pos=v.path.pos+1
local step=string.sub(v.path.route,v.path.pos,v.path.pos)
if step=='u' then
v.o='u'
if not actors.check_collision(v.x,v.y,v.o) and not mapa.check_collision(v.x,v.y-1) then
v.y=v.y-1
v.dy=4
needs_sorting=true
end
elseif step=='d' then
v.o='d'
if not actors.check_collision(v.x,v.y,v.o) and not mapa.check_collision(v.x,v.y+1) then
v.y=v.y+1
v.dy=-4
needs_sorting=true
end
elseif step=='l' then
v.o='l'
if not actors.check_collision(v.x,v.y,v.o) and not mapa.check_collision(v.x-1,v.y) then
v.x=v.x-1
v.dx=4
end
elseif step=='r' then
v.o='r'
if not actors.check_collision(v.x,v.y,v.o) and not mapa.check_collision(v.x+1,v.y) then
v.x=v.x+1
v.dx=-4
end
elseif step=='q' then
v.o='u'
elseif step=='a' then
v.o='d'
elseif step=='o' then
v.o='l'
elseif step=='p' then
v.o='r'
end
end
end
end
if v.dx > 0 then
v.dx=v.dx-1
elseif v.dx < 0 then
v.dx=v.dx+1
elseif v.dy > 0 then
v.dy=v.dy-1
elseif v.dy < 0 then
v.dy=v.dy+1
end
end
if needs_sorting then actors.sort() end
actors.updating=false
end,
sort=function()
table.sort(actors.list, compare_actors)
end
}
function compare_actors(a,b)
return a.y < b.y
end