-- require "final"
-- require "audio"

o2aX = arcade_config.org2arc_escala

avatar_abad_x = 6
avatar_abad2_x = 6
avatar_abad_y = 0
avatar_batman_x = 8
avatar_batman_y = 96
avatar_premiere_x = 138
avatar_premiere_y = 40
avatar_elalien_x = 6
avatar_elalien_y = 32
avatar_w = 22
avatar_h = 21
avatar_photo_w = avatar_w*2
avatar_photo_h = avatar_h*2
avatar_imp_x = 134
avatar_imp_y = 96

avatar_sf=surf.new(avatar_w, avatar_h)

scene={
    running = false,
    stop_music = true
}

scenes={
    lluita_imp= {
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"Eh!?","...","MANTE! Tu de qui eres?"},die=20},
        {x=avatar_imp_x, y=avatar_imp_y,flip=true,audio="imp",text={"","Aaaarrrrgghhh",""},die=20,musica=audio_song_imp},
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"Ah! Que eres foraster","Que fas açí? Has","vingut a per pelailles?"},die=20},
        {x=avatar_imp_x, y=avatar_imp_y,flip=true,audio="imp",text={"","Grwuuuunnnn",""},die=20},
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"Pos jo lo unic que","tinc es un grapat","de cacaus"},die=20},
        {x=avatar_imp_x, y=avatar_imp_y,flip=true,audio="imp",text={"","GROOOOAAAARRR",""},die=20},
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"Haver parat en Alcoi!","A mi que me contes!","   CACAUS A MI!!!!"},die=20},
    },
    llibre_trobat={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Este llibre...","Es el de les tècniques","especials dels abad..."},mood="interessant"},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"...","TÈCNICA 'CACAU AERI'","salt i dispar"}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Ja se","     'CACAU AERI'",""}},
    },
    intro_01= {
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"Ah!, què bo!","Nit de Netflix amb","un bon bol de cacaus!"},die=20},
    },
    intro_02= {
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Eh! On està el","bol de cacaus???",""},die=20, mood="sorpresa"},
    },
    intro_03= {
        {x=avatar_batman_x,y=avatar_batman_y,flip=true,audio="batman",musica=audio_song_batman,text={"ME L'HE ENDUT JO!!!", "S'HAN ACABAT ELS","CACAUS PA TU!"},die=20},
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"Què??","Però perquè???",""},die=20},
        {x=avatar_batman_x,y=avatar_batman_y,flip=true,audio="batman",text={"Estàs com una vaca!","Has de deixar de menjar", "eixes gorrinades"},die=20},
        {x=avatar_batman_x,y=avatar_batman_y,flip=true,audio="batman",text={"Mira'm a mi. Estic","tot buenorro i faig", "esport"},die=20},
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"Esport?????","I una merda!!","He de cuidar la panxa!"},die=20},
        {x=avatar_batman_x,y=avatar_batman_y,flip=true,audio="batman",text={"Pos t'aguantes.","Ja pots despedir-te", "dels cacaus..."},die=20},
    },
    intro_04= {
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"Serà mamó...","",""},die=20},
    },
    abad_inici={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Se va a enterar eixe","imbècil de Batman","quan el pille..."}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Ràpid! A casa de Batman!","Abans de que se menje","tots el cacaus"}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Vaig a parlar en Premiere","que està al pis de dalt", "a vore si me pot ajudar"}},
    },
    abad_corfes={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Osti, està ple de","monstruacos.",""}, mood="sorpresa"},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Els enviaré a altra","dimensió d'una cacauà!",""}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Si van altets","igual val la pena","ajupir-se..."}}
    },
    abad_portes={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Qui ha tancat totes","les portes?","",mood="interessant"}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"A saber on està","cada interruptor","per a obrir-les..."}},
    },
    abad_porta_casa={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Vaja, la porta de","casa també està","tancada."}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Però he de eixir!","Batman viu en la","casa de davant!"}},
    },
    abad_porta_batman={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Be, era de esperar","que Batman tinguera","la porta tancada."}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Crec que hi havia","un passadis secret","del meu sotan al seu."}},
    },
    gps_trobat={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Ale, ja tinc el GPS.","Ara puc trobar tot","el que necessite..."}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"polsant les tecles","de l'1 al 5",""}},
    },
    premiere_intro={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",musica=audio_song_premiere,text={"Premiere??","Qué fas per ma casa?",""}},
        {x=avatar_premiere_x,y=avatar_premiere_y,flip=true,audio="premiere",text={"Porca miseria!!","l'uomo pipistrello!!","m'ha llevatto il gorro!"}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Pipistrello??","Això ques??",""}},
        {x=avatar_premiere_x,y=avatar_premiere_y,flip=true,audio="premiere",text={"Il batmano!!","Incontrame il gorro","per l'amore d'un porco!"}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Tinc faena. A mi m'ha","furtat els cacaus i","he de trobarlos."}},
        {x=avatar_premiere_x,y=avatar_premiere_y,flip=true,audio="premiere",text={"Lei tenne in su quarto!","Io tengo micha clave!","Achudame i t'achudo!"}}
    },
    premiere_intro_gorro={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",musica=audio_song_premiere,text={"Premiere??","Qué fas per ma casa?",""}},
        {x=avatar_premiere_x,y=avatar_premiere_y,flip=true,audio="premiere",text={"Porca miseria!!","l'uomo pipistrello!!","m'ha llevatto il gorro!"}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Pipistrello??","Això... be, te igual","Ací el tens, atontat."}},
        {x=avatar_premiere_x,y=avatar_premiere_y,flip=true,audio="premiere",text={"Per fortuna!","Grazie mille!","Come podo achudarte?"}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Pos Batman m'ha furtat","els cacaus. Supose que","els te amagats."}},
        {x=avatar_premiere_x,y=avatar_premiere_y,flip=true,audio="premiere",text={"In lo suo quarto!","Io tengo micha clave!","Tenne, tenne!!"}}
    },
    premiere_gorro={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",musica=audio_song_premiere,text={"Tin, el ditxos gorro.","",""}},
        {x=avatar_premiere_x,y=avatar_premiere_y,flip=true,audio="premiere",text={"Bravissimo!","Che giornatta","fantastica! Figo!"}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Tu si que eres un","figo. On està eixa","mitja clau?"}},
        {x=avatar_premiere_x,y=avatar_premiere_y,flip=true,audio="premiere",text={"Giusto! Aquí tenne!","Buona fortuna per","la tua avventura!"}},
        {x=avatar_premiere_x,y=avatar_premiere_y,flip=true,audio="premiere",text={"E NON TOCARE il","Batmano! Sei molto","poderoso!"}}
    },
    gorro_trobat_nointro={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Mira, el gorro","de Premiere.","Millor l'agafe."}}
    },
    gorro_trobat={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Mira, el gorro","de Premiere.","A la saca."}}
    },
    elalien_intro={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",musica=audio_song_elalien,text={"Que passa alien?","A tu també t'ha","putejat el Batman?"}},
        {x=avatar_elalien_x,y=avatar_elalien_y,flip=true,audio="elalien",text={"CAGUEN L'OU!!","ESTAVA JO TAN TRANQUIL","PODANT AL MEU GOS..."}},
        {x=avatar_elalien_x,y=avatar_elalien_y,flip=true,audio="elalien",text={"...I QUAN NO MIRABA","M'HA FURTAT EL","MARANÀ!!!"}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Es...taves podant","a un gos...?",""}},
        {x=avatar_elalien_x,y=avatar_elalien_y,flip=true,audio="elalien",text={"EN JUPITER NO POTS","DEIXAR QUE LI CREIXEN","MES DE 12 PATES!!"}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Uh... clar...","A mi m'ha furtat","els cacaus."}},
        {x=avatar_elalien_x,y=avatar_elalien_y,flip=true,audio="elalien",text={"ELS TÉ EN LA SEUA","HABITACIÓ SEGUR.","TROBA EL BERENAR..."}},
        {x=avatar_elalien_x,y=avatar_elalien_y,flip=true,audio="elalien",text={"...I JO ET DONARÉ","LA MEITAT DE LA CLAU","PER A ENTRAR."}}
    },
    elalien_intro_peu={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",musica=audio_song_elalien,text={"Que passa alien?","A tu també t'ha","putejat el Batman?"}},
        {x=avatar_elalien_x,y=avatar_elalien_y,flip=true,audio="elalien",text={"CAGUEN L'OU!!","ESTAVA JO TAN TRANQUIL","PODANT AL MEU GOS..."}},
        {x=avatar_elalien_x,y=avatar_elalien_y,flip=true,audio="elalien",text={"...I QUAN NO MIRABA","M'HA FURTAT EL","MARANÀ!!!"}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Podant...? be, te","igual. Açò deu ser","el teu berenar."}},
        {x=avatar_elalien_x,y=avatar_elalien_y,flip=true,audio="elalien",text={"OSTI QUE BÓ!!!","GRACIES AUELO!","A TU QUE T'HA FET?"}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"M'ha furtat els cacaus.","Segur que els té","al seu quarto."}},
        {x=avatar_elalien_x,y=avatar_elalien_y,flip=true,audio="elalien",text={"SEGUR! PERO JO TINC","LA MEITAT DE LA CLAU.","TIN, I SORT!"}}
    },
    elalien_peu={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",musica=audio_song_elalien,text={"Ací tens...","Uff, que peste fa...",""}},
        {x=avatar_elalien_x,y=avatar_elalien_y,flip=true,audio="elalien",text={"GRACIES!!!","SÍ, EH? QUE","SUCOSET I BÓ."}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Clar...","Be, i eixa","mitja clau?"}},
        {x=avatar_elalien_x,y=avatar_elalien_y,flip=true,audio="elalien",text={"TIN! QUAN TINGUES","ELS CACAUS, PEGA-LI","CACAUÀ A BATMAN!"}},
    },
    peu_trobat_nointro={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Un peu podrit??","Qué asco!! Segur","que és d'el alien."}}
    },
    peu_trobat={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Joer, açò deu ser","el berenar d'el alien.","Ecs..."}}
    },
    habitacio_batman={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"L'habitació de Batman!","Segur que els cacaus","estàn ahí dins."}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Però necessite","una clau...",""}}
    },
    -- bol_trobat={
    --     {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"PER FI, JA TINC","ELS MEUS CACAUS!!!","OLE!! OLE!!"}},
    --     {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Se va a enterar","Batman ara quan","el pille..."}},
    --     {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Ara si que li puc","pegar una bona","cacauà..."}}
    -- },
    bol_trobat={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"EH! ON ESTAN","ELS MEUS CACAUS!!?",""}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Se va a enterar","Batman en quan","el pille..."}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"...","I eixe soroll?","Ve de dalt!"}}
    },
    stage1_ending={
        {x=avatar_batman_x,y=avatar_batman_y,flip=true,audio="batman",musica=audio_song_batman,text={"Te pensaves que anaves", "a recuperar-los tan fàcil","moniato!"},die=20},
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"Torna'm els meus cacaus","lladre!","Com t'agarre..."},die=20},
        {x=avatar_batman_x,y=avatar_batman_y,flip=true,audio="batman",text={"Si vols els cacaus","hauràs d'agarrar-me", ""},die=20},
        {x=avatar_batman_x,y=avatar_batman_y,flip=true,audio="batman",text={"I mes te val no tardar","perque estan bonissims", "Torradets en el seu punt"},die=20},
    },
    stage1_ending_2={
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"elalien!","Premiere!","Que feu ací!?"},die=20},
        {x=avatar_elalien_x,y=avatar_elalien_y,flip=true,audio="elalien",text={"HAVEM VIST COM","FUGIA BATMAN", "EN ELS CACAUS!!"},die=20},
        {x=avatar_premiere_x,y=avatar_premiere_y,flip=true,audio="premiere",text={"Porca miseria!!","Il batmano e molto", "cabroni"},die=20},
        {x=avatar_premiere_x,y=avatar_premiere_y,flip=true,audio="premiere",text={"Io tingo un artiluggio","per volare d'un gran", "amichi mio"},die=20},
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"OH! MOLTES GRACIES","PREMIERE!!!",""},die=20},
    },
    stage1_ending_3={
        {x=avatar_premiere_x,y=avatar_premiere_y,flip=true,audio="premiere",text={"TUTTO TUO!","IL BATMANO NO TENNE", "NI UNA POSSIBILITÁ"},die=20},
        {x=avatar_premiere_x,y=avatar_premiere_y,flip=true,audio="premiere",text={"CONTRA ESTE MANIFICO","ARTILUGGIO",""},die=20},
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"...","",""},die=20, mood="sorpresa"},
    },
    final= {
        {x=avatar_batman_x,y=avatar_batman_y,flip=true,audio="batman",musica=audio_song_batman,text={"AI!", "M'has fet mal!!!",""},die=20},
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"Aixó per furtar-me","el bol de cacaus,","imbèsil"},die=20},
        {x=avatar_batman_x,y=avatar_batman_y,flip=true,audio="batman",text={"Cert, esta me la","mereixia... però", "mira que be estàs!"},die=20},
        {x=avatar_batman_x,y=avatar_batman_y,flip=true,audio="batman",text={"Amb el que t'ha","costat pillar-me", "t'has ficat en forma"},die=20},
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"No me vingues en","tonteries,","mamarratxo."},die=20},
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"Creus que no he","notat que falta mig","bol de cacaus?"},die=20},
        {x=avatar_batman_x,y=avatar_batman_y,flip=true,audio="batman",text={"Jejejeje, es que","estàn molt bons!", ""},die=20},
        {x=avatar_batman_x,y=avatar_batman_y,flip=true,audio="batman",text={"Però si vols te","done del meu bol", "de tramussos."},die=20},
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"TRAMUSSOS...","",""},die=10},
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"M'atreviré a","passar l'última","frontera...?"},die=40},
    },
}

function start_scene(_scene, offset, stop_music)
    --bkg=newsurf(128,96)
    --setdest(bkg)
    --setsource(0)
    --draw.surf(0,0,128,96,0,0)
    --setsource(tiles)
    --setdest(0)
    scenes.current_scene=_scene
    scenes.dnum=1
    scenes.step=0
    scenes.char=1
    scenes.wait=0
    scenes.offset=28
    scenes.die=scenes.current_scene[scenes.dnum].die or 0
    if _scene[1].musica then
        --print(scene[1].musica)
        music.play(scenes.current_scene[scenes.dnum].musica)
    end
    if offset then scenes.offset=offset end
    -- old_update=game_update
    -- game_update=update_scene
    scene.running = true
    if not stop_music then scene.stop_music = false end
    -- print("SCENE START")
    states:executar("scene", true); -- guardar l'estat anterior i executar
end

function playtext(snd)
    if snd=="abad" then
        sound.play(audio_text_abad)
    elseif snd=="batman" then
        sound.play(audio_text_batman)
    elseif snd=="premiere" then
        sound.play(audio_text_premiere)
    elseif snd=="elalien" then
        sound.play(audio_text_elalien)
    elseif snd=="imp" then
        sound.play(audio_text_imp)
    end
end

function next_scene()
    scenes.dnum=scenes.dnum+1
    scenes.die=scenes.current_scene[scenes.dnum].die or 0
    scenes.step=2
    play_music()
    -- print("NEXT SCENE")
end

function play_music()
    if scenes.current_scene[scenes.dnum].musica then
        -- print("PLAY MUSIC "..scenes.dnum)
        music.stop()
        music.play(scenes.current_scene[scenes.dnum].musica)
    end
end

function draw_abad_with_mood(mood, dx, dy, dw, dh)
    local ox, oy =view.origin()
    view.origin(0,0)
    -- -- composicio
    local curr_sf = surf.target()
    surf.target(avatar_sf)
    surf.cls(16)
    draw.surf(scenes.current_scene[scenes.dnum].x,scenes.current_scene[scenes.dnum].y,avatar_w,avatar_h,0,0,avatar_w,avatar_h)
    abad_add_mood(0,scenes.current_scene[scenes.dnum].x,0,scenes.current_scene[scenes.dnum].y,false,mood)
    -- if mood=="sorpresa" then
    --     draw.surf(240,0,11,4,16-scenes.current_scene[scenes.dnum].x,5-scenes.current_scene[scenes.dnum].y,11,4)
    -- elseif mood=="enfadat" then
    --     draw.surf(240,4,11,4,16-scenes.current_scene[scenes.dnum].x,5-scenes.current_scene[scenes.dnum].y,11,4)
    -- elseif mood=="interessant" then
    --     draw.surf(240,8,11,4,16-scenes.current_scene[scenes.dnum].x,5-scenes.current_scene[scenes.dnum].y,11,4)
    -- else
    --     abad.mood="normal"
    -- end
    -- Dibuixar
    view.origin(ox, oy)
    surf.target(curr_sf)
    curr_sf = surf.source()
    surf.source(avatar_sf)
    draw.surf(0,0,avatar_w,avatar_h, dx, dy, dw, dh)
    surf.source(curr_sf)
    -- draw.surf(scenes.current_scene[scenes.dnum].x,scenes.current_scene[scenes.dnum].y,avatar_w,avatar_h, dx, dy, dw, dh)
end

function draw_scene()
    local txt_x = not scenes.current_scene[scenes.dnum].flip and 38 or 8

    view.origin(0,scenes.offset*o2aX)
           
    -- 0, 1, 2 -> Desplegar el cuadre de dialeg
    if scenes.step==0 then
        draw.rectf(0,16*o2aX,129*o2aX,2*o2aX,12)
        draw.line(0,15*o2aX,128*o2aX,15*o2aX,16)
        draw.line(0,18*o2aX,128*o2aX,18*o2aX,16)
    elseif scenes.step==1 then
        draw.rectf(0,8*o2aX,129*o2aX,19*o2aX,12)
        draw.line(0,7*o2aX,128*o2aX,7*o2aX,16)
        draw.line(0,27*o2aX,128*o2aX,27*o2aX,16)
    elseif scenes.step==2 then
        draw.rectf(0,0,129*o2aX,35*o2aX,12)
        draw.line(0,0,128*o2aX,0,16)
        draw.line(0,34*o2aX+1,128*o2aX,34*o2aX+1,16)
    -- 3, 4 -> Desplegar el avatar
    elseif scenes.step==3 then
        if not scenes.current_scene[scenes.dnum].flip then
            draw.rectf(8*o2aX,12*o2aX,avatar_photo_w+8, (avatar_photo_h+8)/2,16)
            -- Només està disponible per a l'abad
            if scenes.current_scene[scenes.dnum].mood then
                draw_abad_with_mood(scenes.current_scene[scenes.dnum].mood, 18, 26, avatar_photo_w, avatar_photo_h/2)
                -- draw.surf(scenes.current_scene[scenes.dnum].x,scenes.current_scene[scenes.dnum].y,avatar_w,avatar_h,9*o2aX,13*o2aX,20*o2aX,10*o2aX)
            else
                abad.mood="normal"
                draw.surf(scenes.current_scene[scenes.dnum].x,scenes.current_scene[scenes.dnum].y,avatar_w,avatar_h,9*o2aX,13*o2aX,avatar_photo_w, avatar_photo_h/2)
            end
        else
            draw.rectf(97*o2aX,12*o2aX,avatar_photo_w+8, (avatar_photo_h+8)/2,16)
            draw.surf(scenes.current_scene[scenes.dnum].x,scenes.current_scene[scenes.dnum].y,avatar_w,avatar_h,98*o2aX,13*o2aX,avatar_photo_w, avatar_photo_h/2,true)
        end
    elseif scenes.step==4 then
        if not scenes.current_scene[scenes.dnum].flip then
            draw.rectf(8*o2aX,7*o2aX,avatar_photo_w+8,avatar_photo_h+4,16)
            -- Només està disponible per a l'abad
            if scenes.current_scene[scenes.dnum].mood then
                draw_abad_with_mood(scenes.current_scene[scenes.dnum].mood, 18, 16, avatar_photo_w, avatar_photo_h)
                -- draw.surf(scenes.current_scene[scenes.dnum].x,scenes.current_scene[scenes.dnum].y,avatar_w,avatar_h,9*o2aX,8*o2aX,20*o2aX,20*o2aX)
            else
                abad.mood="normal"
                draw.surf(scenes.current_scene[scenes.dnum].x,scenes.current_scene[scenes.dnum].y,avatar_w,avatar_h,9*o2aX,8*o2aX,avatar_photo_w, avatar_photo_h)
            end
        else
            draw.rectf(97*o2aX,7*o2aX,avatar_photo_w+8,avatar_photo_h+4,16)
            draw.surf(scenes.current_scene[scenes.dnum].x,scenes.current_scene[scenes.dnum].y,avatar_w,avatar_h,98*o2aX,8*o2aX,avatar_photo_w, avatar_photo_h,true)
        end
    -- 5, 6, 7 -> Escriure les 3 frases de diàleg
    elseif scenes.step==5 then
        arc_textB(utf8.sub(scenes.current_scene[scenes.dnum].text[1],1,scenes.char),txt_x*o2aX,7*o2aX,2)
        if scenes.char<#scenes.current_scene[scenes.dnum].text[1] then
            scenes.step=scenes.step-1
            scenes.char=scenes.char+1
            playtext(scenes.current_scene[scenes.dnum].audio)
        else
            scenes.char=1
        end
    elseif scenes.step==6 then
        arc_textB(utf8.sub(scenes.current_scene[scenes.dnum].text[2],1,scenes.char),txt_x*o2aX,15*o2aX,2)
        if scenes.char<#scenes.current_scene[scenes.dnum].text[2] then
            scenes.step=scenes.step-1
            scenes.char=scenes.char+1
            playtext(scenes.current_scene[scenes.dnum].audio)
        else
            scenes.char=1
        end
    elseif scenes.step==7 then
        arc_textB(utf8.sub(scenes.current_scene[scenes.dnum].text[3],1,scenes.char),txt_x*o2aX,23*o2aX,2)
        if scenes.char<#scenes.current_scene[scenes.dnum].text[3] then
            scenes.step=scenes.step-1
            scenes.char=scenes.char+1
            playtext(scenes.current_scene[scenes.dnum].audio)
        else
            scenes.char=1
        end
    -- Render de tot lo anterior
    elseif scenes.step==8 then
        draw.rectf(0,0,129*o2aX,35*o2aX,12)
        draw.line(0,0,128*o2aX,0,16)
        draw.line(0,34*o2aX+1,128*o2aX,34*o2aX+1,16)
        if not scenes.current_scene[scenes.dnum].flip then
            draw.rectf(8*o2aX,7*o2aX,avatar_photo_w+8,avatar_photo_h+4,16)
            if scenes.current_scene[scenes.dnum].mood then
                draw_abad_with_mood(scenes.current_scene[scenes.dnum].mood, 18, 16, avatar_photo_w, avatar_photo_h)
                -- draw.surf(scenes.current_scene[scenes.dnum].x,scenes.current_scene[scenes.dnum].y,avatar_w,avatar_h,9*o2aX,13*o2aX,20*o2aX,10*o2aX)
            else
                abad.mood="normal"
                draw.surf(scenes.current_scene[scenes.dnum].x,scenes.current_scene[scenes.dnum].y,avatar_w,avatar_h,9*o2aX,8*o2aX,avatar_photo_w, avatar_photo_h)
            end
        else
            draw.rectf(97*o2aX,7*o2aX,avatar_photo_w+8,avatar_photo_h+4,16)
            draw.surf(scenes.current_scene[scenes.dnum].x,scenes.current_scene[scenes.dnum].y,avatar_w,avatar_h,98*o2aX,8*o2aX,avatar_photo_w, avatar_photo_h,true)
        end
		arc_textB(scenes.current_scene[scenes.dnum].text[1],txt_x*o2aX,7*o2aX,2)
        arc_textB(scenes.current_scene[scenes.dnum].text[2],txt_x*o2aX,15*o2aX,2)
        arc_textB(scenes.current_scene[scenes.dnum].text[3],txt_x*o2aX,23*o2aX,2)
    end
    if scenes.step<=8 then 
        scenes.step=scenes.step+1
    end
    view.origin(0,0)
end

function update_scene()
    scenes.wait=scenes.wait+1

    if scenes.wait>=6 then
        scenes.wait=0
        draw_scene()
        if scenes.step>8 then
            if scenes.die>0 then
                scenes.die=scenes.die-1
                if scenes.die==0 then
                    if scenes.dnum==#scenes.current_scene then
                        if scene.stop_music then music.stop() end
                        -- if batman.endgame then
                        --     final_init()
                        --     fade.fadeoutin()
                        -- else
                             -- game_update=old_update
                             -- flow:restore()
                             states:next()
                        -- end
                    else
                        next_scene()
                    end
                end
            else
                if math.floor(sys.time()*2)%2==0 then
                    draw.surf(104*o2aX,88*o2aX,7*o2aX,5*o2aX,118*o2aX,56*o2aX)
                else
                    draw.rectf(118*o2aX,56*o2aX,8*o2aX,6*o2aX,12)
                end
            end
        end
    end

    if (key.press(key.ESCAPE) or pad.press(btnPause)) and (scenes.current_scene~=scenes.final) then
        if scenes.current_scene[scenes.dnum].die then
            -- game_init(true)
            -- flow:next()
            -- game_update = menu_init
            states:executar("title")
        else
            pause()
        end
    end

    -- if scenes.step>8 then
    --     if controller:check("shoot") or controller:check("down") or 
    --        controller:check("up")    or controller:check("left") or 
    --        controller:check("right") or key.press(key.RETURN) then
    --         if scenes.dnum==#scenes.current_scene then
    --             if scenes.stop_music then music.stop() end
    --             -- if batman.endgame then
    --             --     final_init()
    --             --     fade.fadeoutin()
    --             -- else
    --                 -- game_update=old_update
    --                 states:next()
    --             -- end
    --         else
    --             -- scenes.dnum=scenes.dnum+1
    --             -- scenes.step=2
    --             next_scene()
    --         end
    --     end
    -- elseif key.press(keyShoot) or pad.press(btnShoot) then
    --     scenes.step=8
    --     scenes.die =20
    --     print("SKIP")
    -- end    
    if controller:check("shoot") then
        if scenes.step<8 then
            -- skip play text
            scenes.step=8
        elseif scenes.die==0 and scenes.step>=8 then
            -- continue scene
            if scenes.dnum==#scenes.current_scene then
                if scene.stop_music then music.stop() end
                states:next()
            else
                next_scene()
            end
        end 
    end
end

function end_scene()
    scene.running = false
    states:finish()
end

states:registrar("scene",{update_scene, end_scene})