- Implementat el moviment dels actors amb rutes

- També el moviment amb teclat del "heroi"
This commit is contained in:
2023-01-30 17:51:16 +01:00
parent 42d5c1f308
commit 09bea06850
4 changed files with 91 additions and 35 deletions

View File

@@ -6,51 +6,87 @@ actors={
table.insert(actors.list, actor)
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,
draw=function()
for i,v in ipairs(actors.list) do
sspr(v.gfx.x,v.gfx.y,16,16,v.x*8+v.dx,v.y*8+v.dy,16,16,v.o)
local frame=((v.dx+v.dy)%2)*16
--print(v.o)
if v.o=='u' then
sspr(v.gfx.x+frame,v.gfx.y+16,16,16,v.x*8-4+v.dx*2,v.y*8-12+v.dy*2,16,16)
elseif v.o=='d' then
sspr(v.gfx.x+frame,v.gfx.y,16,16,v.x*8-4+v.dx*2,v.y*8-12+v.dy*2,16,16)
elseif v.o=='l' then
sspr(v.gfx.x+frame,v.gfx.y+32,16,16,v.x*8-4+v.dx*2,v.y*8-12+v.dy*2,16,16)
elseif v.o=='r' then
sspr(v.gfx.x+frame,v.gfx.y+32,16,16,v.x*8-4+v.dx*2,v.y*8-12+v.dy*2,16,16,true)
end
end
end,
update=function()
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
v.path.pos=v.path.pos+1
if v.path.pos > #v.path.route then
v.path=nil
else
local step=string.sub(v.path.route,v.path.pos,v.path.pos)
if step=='u' then
--check collision!
v.y=v.y-1
v.dy=-8
elseif step=='d' then
--check collision!
v.y=v.y+1
v.dy=8
elseif step=='l' then
--check collision!
v.x=v.x-1
v.dx=-8
elseif step=='r' then
--check collision!
v.x=v.x+1
v.dx=8
end
local step=string.sub(v.path.route,v.path.pos,v.path.pos)
if v.path.pos == #v.path.route then v.path=nil end
if step=='u' then
--check collision!
v.y=v.y-1
v.dy=4
v.o='u'
needs_sorting=true
elseif step=='d' then
--check collision!
v.y=v.y+1
v.dy=-4
v.o='d'
needs_sorting=true
elseif step=='l' then
--check collision!
v.x=v.x-1
v.dx=4
v.o='l'
elseif step=='r' then
--check collision!
v.x=v.x+1
v.dx=-4
v.o='r'
elseif step=='q' then
v.o='u'
elseif step=='a' then
v.o='d'
elseif step=='o' then
v.o='r'
elseif step=='p' then
v.o='l'
end
end
else
if v.dx > 0 then
v.dx=v.dx-2
elseif v.dx < 0 then
v.dx=v.dx+2
elseif v.dy > 0 then
v.dy=v.dy-2
elseif v.dy < 0 then
v.dy=v.dy+2
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
table.sort(actors.list, compare_actors)
end
end
}
}
function compare_actors(a,b)
return a.y < b.y
end