final_rooms = {2,6,60,21,49,23,66,32,4,46,39,55,74}

function final_init()
    actors={}
    cameras={}
    current_actor=1

    abad.update = abad_nop
    game_update=update_final
    next_actor()
    music.play(audio_final)
    local r,g,b=pal.color(15)
    pal.color(17,r,g,b)
    --fade.fadein()
end

go_next_actor=false

function update_final()
    surf.cls(16)

    view.origin(16,41)
    mapa_draw(final_room)
    draw_actor()
    view.origin(0,0)

    draw.rectf(0,0,15,97,16)
    draw.rectf(114,0,78,97,16)
    draw.rect(15,40,99,50,2)

    text("C A C A U S", 42, 12, 17)
    text(actor.name, 64-(#actor.name*2), 28, 11)

    mapa_update(abad.hab,final_room)

    if go_next_actor then next_actor() end
end

function init_actor(name,flipped,w,h,anim)
    actor={name=name,flip=flipped,w=w,h=h,anim=anim,frame=0,wait=0,step=0}
    actor.y=40-h
    actor.center_count=20
    actor.frame=actor.anim[1]
    if #anim==1 then
        actor.x=48-w/2
        actor.center_count=40
    elseif flipped then
        actor.x=96
    else
        actor.x=-actor.w
    end
end

function draw_actor()
    actor.wait=actor.wait+1

    if actor.wait==6 or (actor.wait==3 and current_actor==7) then
        actor.wait=0
        if actor.x == 48-actor.w/2 and actor.center_count>0 then
            actor.center_count=actor.center_count-1
            actor.frame=actor.anim[1]
        else
            actor.step=actor.step+1
            if actor.step >= #actor.anim then actor.step=0 end
            actor.frame=actor.anim[actor.step+1]

            if #actor.anim>1 then
                if actor.flip then
                    actor.x=actor.x-2
                else
                    actor.x=actor.x+2
                end
            end
            if current_actor==7 then
                actor.y=28-math.abs(math.sin(actor.x*0.1))*24
            end

            if actor.x>96 or actor.x<-actor.w or (#actor.anim==1 and actor.center_count==0) then
                go_next_actor=true
                fade.fadeoutin()
                return true
            end
        end
    end
    local tpl=128/actor.w
    draw.surf((actor.frame%tpl)*actor.w,math.floor(actor.frame/tpl)*actor.h,actor.w,actor.h,actor.x,actor.y,actor.w,actor.h,actor.flip)
end

function next_actor()
    go_next_actor=false
    faded = true
    if current_actor==1 then
        init_actor("ZOMBIE",false,16,16,{16,17,16,18})
    elseif current_actor==2 then
        init_actor("CACODEMON",true,16,16,{19,19,19,19,19,19,19,19,19,19,19,19,20,21})
        actor.y=16
    elseif current_actor==3 then
        init_actor("GPS",false,16,16,{31})
        actor.y=16
    elseif current_actor==4 then
        init_actor("IMP",false,16,16,{28,29,28,30})
    elseif current_actor==5 then
        init_actor("GORRO DE PREMIERE",false,16,16,{22})
    elseif current_actor==6 then
        init_actor("BAMBOLLA DE CAFÉ",true,16,16,{15,15})
    elseif current_actor==7 then
        init_actor("PEU DE PACO",false,16,16,{23})
    elseif current_actor==8 then
        init_actor("PREMIERE",false,16,16,{12,13,12,14})
    elseif current_actor==9 then
        init_actor("CLAU",false,16,8,{15})
    elseif current_actor==10 then
        init_actor("EL_ALIEN",true,16,16,{8,9,8,10})
    elseif current_actor==11 then
        init_actor("BOL DE CACAUS",false,16,8,{95})
    elseif current_actor==12 then
        init_actor("BATMAN",false,16,16,{24,25,24,26})
    elseif current_actor==13 then
        init_actor("EL ABAD",true,16,16,{0,1,0,2})
    elseif current_actor==14 then
        final_count=0
        game_update=update_final2
        fade.fadeoutin()
    end
    final_room=final_rooms[current_actor]
    current_actor=current_actor+1
end

function update_final2()
    surf.cls(16)

    text("C A C A U S", 42, 12, 17)
    text("GRACIES PER JUGAR", 30, 48, 10)

    final_count=final_count+1
    if final_count==200 or (key.press(keyShoot) or pad.press(btnShoot)) or key.press(key.RETURN) then
        game_exit()
        game_init(true)
        fade.fadeoutin()
    end
end
