require "abad"
require "gps"
require "gorro"
require "peu"
require "premiere"
require "elalien"
require "batman"
require "bol"
require "gota"
require "invisibl"
require "zombie"
require "caco"
require "trigger"
require "score"
require "imp"
require "bambolla"
require "cacau"
require "fireball"
require "switches"
require "logo"

o2aX = arcade_config.org2arc_escala

modes={playing=0,editing=1}
--mode=modes.editing
mode=modes.playing

seltile = 0

actors={}
cameras={}
camera_names={[0]="GPS","Gorro","Peu de Paco","Premiere","EL_ALIEN","BatMan"}
current_camera=0

function remove_actor(actor)
    for index, value in pairs(actors) do
        if value == actor then
            table.remove(actors,index)
        end
    end
end

function game_init(menu)
    actors={}
    cameras={}
    camera_names={[0]="GPS","Gorro","Peu de Paco","Premiere","EL_ALIEN","BatMan"}
    current_camera=0

    -- ACTORS PRINCIPALS
    abad_init()
    table.insert(actors,abad)

    gps.init()
    table.insert(actors,gps)

    gorro.init()
    table.insert(actors,gorro)

    peu.init()
    table.insert(actors,peu)

    premiere.init()
    table.insert(actors,premiere)

    elalien.init()
    table.insert(actors,elalien)

    batman.init()
    table.insert(actors,batman)

    bol.init()
    table.insert(actors,bol)

    -- MALOTES PERSISTENTS
    table.insert(actors,gota.new(70,40,8,20))
    table.insert(actors,gota.new(70,72,8,30))
    table.insert(actors,gota.new(63,40,4,25))
    table.insert(actors,gota.new(63,72,4,20))
    table.insert(actors,gota.new(64,24,4,10))
    table.insert(actors,gota.new(64,48,4,25))
    table.insert(actors,gota.new(65,56,4,10))
    table.insert(actors,gota.new(65,63,4,25))
    table.insert(actors,gota.new(66,24,4,10))
    table.insert(actors,gota.new(66,32,4,15))
    table.insert(actors,gota.new(77,16,4,30))
    table.insert(actors,gota.new(77,72,4,25))

    table.insert(actors,invisible.new(77,40,32))
    table.insert(actors,invisible.new(77,48,32))
    table.insert(actors,invisible.new(58,16,32))
    table.insert(actors,invisible.new(58,16,32))

    table.insert(actors,zombie.new(2,24,24,false))
    table.insert(actors,caco.new(13,24,16,true))
    table.insert(actors,caco.new(20,24,16,false))
    table.insert(actors,zombie.new(32,24,24,false))
    table.insert(actors,caco.new(41,24,16,false))
    table.insert(actors,zombie.new(44,24,24,false))

    table.insert(actors,caco.new(50,24,16,false))
    table.insert(actors,caco.new(61,24,16,false))

    table.insert(actors,zombie.new(73,24,24,false))
    table.insert(actors,zombie.new(54,24,24,false))
    table.insert(actors,zombie.new(68,24,24,false))
    table.insert(actors,caco.new(57,24,16,false))
    table.insert(actors,zombie.new(46,24,24,false))
    table.insert(actors,caco.new(37,24,16,false))
    table.insert(actors,caco.new(26,24,16,false))
    table.insert(actors,zombie.new(28,24,24,false))
    table.insert(actors,caco.new(17,24,16,false))

    -- TRIGGERS
    if not menu then table.insert(actors,trigger.new(10,57,32,triggers.escena_abad_inici)) end
    table.insert(actors,trigger.new(11,16,32,triggers.escena_abad_corfes))

    table.insert(actors,trigger.new(31,12,32,triggers.escena_abad_portes))
    table.insert(actors,trigger.new(31,48,32,triggers.escena_abad_portes))
    table.insert(actors,trigger.new(71,56,24,triggers.escena_abad_portes))

    table.insert(actors,trigger.new(53,40,32,triggers.escena_abad_porta_casa))
    table.insert(actors,trigger.new(56,32,32,triggers.escena_abad_porta_batman))

    table.insert(actors,trigger.new(19,32,32,triggers.escena_habitacio_batman))

    table.insert(actors,trigger.new(14,40,32,triggers.teleport_a))
    table.insert(actors,trigger.new(67,8,32,triggers.teleport_b))
    table.insert(actors,trigger.new(29,40,32,triggers.teleport_c))
    table.insert(actors,trigger.new(61,8,32,triggers.teleport_d))
    score.create()
    
    cameras[0]=gps
    table.insert(cameras,gorro)
    table.insert(cameras,peu)
    table.insert(cameras,premiere)
    table.insert(cameras,elalien)
    table.insert(cameras,batman)

    mapa_do_backup()

    if menu then
        menu_sel=0
        menu_state=update_main_menu;
        abad.update = abad_nop
        game_update=update_menu
        menu_count=0
        menu_change_room=300
        menu_room=math.random(0,59)
        music.play(audio_main_song)
    else
       game_update=update_game
        music.stop()
    end
    --fade.fadein()
end

function draw_hab(hab,x,y,editing)
    view.origin(x*o2aX,y*o2aX)
    mapa_draw(hab)

    if not editing then
        for key,actor in pairs(actors) do
            if actor.hab==hab then
                actor:draw()
            end
        end
        if imp.hab==hab then imp.draw() end
        if bambolla.hab==hab then bambolla.draw() end
        if cacau.hab==hab then cacau:draw() end
        if fireball.hab==hab then fireball:draw() end
    end
    view.origin(0,0)
end

function update_dialog()
    draw.rectf(16*o2aX,16*o2aX,97*o2aX,35*o2aX,12)
    draw.rect(15*o2aX,15*o2aX,99*o2aX,37*o2aX,16)
    text("Save the map?",20*o2aX,20*o2aX,2)
    text("This cannot be undone",20*o2aX,27*o2aX,2)
    text("(Y)es         (N)o",25*o2aX,40*o2aX,2)

    if key.press(key.N) then
        game_update=update_game
    elseif key.press(key.Y) then
        mapa_save()
        game_update=update_game
    end
end

scroll=0
function update_game()
    surf.cls(16)

    if mode==modes.editing then
        draw_hab(abad.hab,0,0,true)
        text(abad.hab,120*o2aX,1*o2aX,2)

        draw.surf(0,(64+scroll*8)*o2aX,128*o2aX,48*o2aX,0,48*o2aX)
        local xx=(seltile&15)*8
        local yy=48+(seltile>>4)*8
        draw.rect(xx*o2aX,yy*o2aX,9*o2aX,9*o2aX, 3)
        text("EDIT",100*o2aX,1*o2aX,3)
        local hx = abad.hab%10
        local hy = math.floor(abad.hab/10)
        scroll=0
        if key.press(key.RIGHT) and hx<9 then
            abad.hab=abad.hab+1
        elseif key.press(key.LEFT) and hx>0 then
            abad.hab=abad.hab-1
        elseif key.press(key.DOWN) and hy<7 then
            abad.hab=abad.hab+10
        elseif key.press(key.UP) and hy>0 then
            abad.hab=abad.hab-10
        elseif key.press(key.RETURN) then
            mode=modes.playing
        elseif key.press(key.S) and (key.down(key.LCTRL) or key.down(key.LGUI)) then
            game_update=update_dialog
        elseif key.press(key.C) then
            mapa_cycle_colors(abad.hab)
        elseif key.press(key.M) then
            mapa_create_minimap()
            return
        end
        if key.down(key.TAB) then
            scroll=2
        end

        local mx,my=mouse.pos()
        if mx>>3 < 12 and my>>3 < 6 then
            text(mx>>3,100*o2aX,10*o2aX,2)
            text(my>>3,111*o2aX,10*o2aX,2)
            text(((mx>>3)+(my>>3)*12)*o2aX,118*o2aX,10*o2aX,2)
        end
        if mouse.down(mouse.LEFT) then
            if my>=48 then
                seltile=(mx>>3)+((my-48+(scroll*8))>>3)*16
            elseif mx<96 then
                mapa_set_tile(abad.hab,mx>>3,my>>3,seltile)
            end
        elseif mouse.down(mouse.RIGHT) then
            if my<48 and mx<96 then
                mapa_set_tile(abad.hab,mx>>3,my>>3,256)
            end
        end
        if mx>>3 < 12 and my>>3 < 6 then
            local tx,ty=(mx>>3)<<3,(my>>3)<<3
            draw.rect(tx*o2aX,ty*o2aX,9*o2aX,9*o2aX,3)
        end
    else
        if abad.hurting == 0 then
            draw_hab(abad.hab,0,0)
            --text(abad.hab,1,1,2)
            draw_hab(cameras[current_camera].hab,0,48)
            -- text(camera_names[current_camera],2*o2aX,49*o2aX,8)
            arc_textB(camera_names[current_camera],2*o2aX,49*o2aX,8)

            score.draw()
            surf.source(tiles)
            -- draw.text("x"..abad.vides,114*o2aX,13*o2aX,2)
            arc_text("x"..abad.vides,114*o2aX,13*o2aX,2)
            draw.rectf((102+(abad.energia>>1))*o2aX,30*o2aX,(21-(abad.energia>>1))*o2aX,8*o2aX,16)
        else
            draw.surf(0,0,16*o2aX,9*o2aX,50*o2aX,40*o2aX,16*o2aX,9*o2aX,true)
            if abad.hurting > 40 then
                -- draw.text("x"..abad.vides+1,63*o2aX,43*o2aX,3)
                arc_text("x"..abad.vides+1,63*o2aX,43*o2aX,3)
            elseif abad.hurting < 20 then
                -- draw.text("x"..abad.vides,63*o2aX,43*o2aX,2)
                arc_text("x"..abad.vides,63*o2aX,43*o2aX,2)
            end
        end

        mapa_update(abad.hab,cameras[current_camera].hab)

        for key,actor in pairs(actors) do
            actor:update()
            if actor.hab==cacau.hab and actor~=abad then
                if aabb(actor,cacau) then
                    if actor.hit~=nil then
                        actor:hit()
                        cacau.hab=-1
                    end
                end
            end
        end
        imp.update()
        bambolla.update()
        cacau.update()
        fireball.update()
        switches.update()
        
        if key.press(key.ESCAPE) or pad.press(btnPause) then
            pause()
        end
        if abad.objects.gps~=nil then
            if key.press(key.N1) then
                if abad.objects.gorro==nil and abad.objects.clau_premiere==nil then current_camera=1 end
            elseif key.press(key.N2) then
                if abad.objects.peu==nil and abad.objects.clau_elalien==nil then current_camera=2 end
            elseif key.press(key.N3) then
                current_camera=3
            elseif key.press(key.N4) then
                current_camera=4
            elseif key.press(key.N5) then
                current_camera=5
            end
            if pad.press(btnCycle1) then
                current_camera=current_camera+1
                if current_camera==6 then current_camera=1 end
                if current_camera==1 and (abad.objects.gorro or abad.objects.clau_premiere) then current_camera=2 end
                if current_camera==2 and (abad.objects.peu or abad.objects.clau_elalien) then current_camera=3 end
            elseif pad.press(btnCycle2) then
                current_camera=current_camera-1
                if current_camera==0 then current_camera=5 end
                if current_camera==2 and (abad.objects.peu or abad.objects.clau_elalien) then current_camera=1 end
                if current_camera==1 and (abad.objects.gorro or abad.objects.clau_premiere) then current_camera=5 end
            end
        end
    end
end

function redraw_menu_hab()
    draw_hab(menu_room,16,41)
    draw.rectf(0,0,15*o2aX,97*o2aX,16)
    draw.rectf(114*o2aX,0,78*o2aX,97*o2aX,16)
    draw.rect(15*o2aX,40*o2aX,99*o2aX,50*o2aX,2)
    mapa_update(abad.hab,menu_room)

    for key,actor in pairs(actors) do
        actor:update()
    end
    imp.update()
    bambolla.update()
    cacau.update()
    fireball.update()
    switches.update()
end

function update_menu()
    menu_change_room=menu_change_room-1
    if menu_change_room==0 then
        menu_room = math.random(0,59)
        menu_change_room=300
    end

    surf.cls(16)
    -- text("C A C A U S", 42*o2aX, 8*o2aX, 15)
    -- arc_textB("C A C A U S", 42*o2aX, 8*o2aX, 15)
	draw_logo(14*o2aX, 0)
    menu_state()
end

function update_options_menu()
    menu_count=menu_count+1
    local parpadeig=false
    if menu_count>=20 then
        parpadeig=true
        if menu_count>40 then menu_count=0 end
    end
    draw.rect(28*o2aX,(28+(10*menu_sel))*o2aX,73*o2aX,9*o2aX,14)
    if (not parpadeig) then draw.rect(28*o2aX,(28+(10*menu_sel))*o2aX,73*o2aX,9*o2aX,13) end

    -- draw.text("MÚSICA", 30*o2aX, 30*o2aX, 14); draw.text(music.enabled() and "SI" or "NO",91*o2aX,30*o2aX,15)
    -- draw.text("SÓ", 30*o2aX, 40*o2aX, 14);     draw.text(sound.enabled() and "SI" or "NO",91*o2aX,40*o2aX,15)
    -- draw.text("REDEFINIR TECLAT", 30*o2aX, 50*o2aX, 14)
    -- draw.text("REDEFINIR GAMEPAD", 30*o2aX, 60*o2aX, 14)
    -- draw.text("TORNAR", 30*o2aX, 70*o2aX, 14)
	
	arc_text("MÚSICA", 30*o2aX, 30*o2aX, 14); arc_text(music.enabled() and "SI" or "NO",91*o2aX,30*o2aX,15)
    arc_text("SÓ", 30*o2aX, 40*o2aX, 14);     arc_text(sound.enabled() and "SI" or "NO",91*o2aX,40*o2aX,15)
    arc_text("REDEFINIR TECLAT", 30*o2aX, 50*o2aX, 14)
    arc_text("REDEFINIR GAMEPAD", 30*o2aX, 60*o2aX, 14)
    arc_text("TORNAR", 30*o2aX, 70*o2aX, 14)

    if key.press(keyShoot) or key.press(key.SPACE) or pad.press(pad.A) or pad.press(btnShoot) then
        if menu_sel==0 then
            music.enabled(not music.enabled())
            if music.enabled() then music.play(audio_main_song) end
        elseif menu_sel==1 then
            sound.enabled(not sound.enabled())
        elseif menu_sel==2 then
            menu_count=0
            menu_state=update_redefine_keys_menu
        elseif menu_sel==3 then
            menu_count=0
            menu_state=update_redefine_pad_menu
        else
            menu_count=0
            menu_sel=0
            menu_state=update_main_menu
        end
    elseif key.press(keyDown) or key.press(key.DOWN) or pad.press(pad.DOWN) or pad.press(btnDown) then
        menu_sel=menu_sel+1
        if menu_sel==5 then menu_sel=0 end
    elseif key.press(keyUp) or key.press(key.UP) or pad.press(pad.UP) or pad.press(btnUp) then
        menu_sel=menu_sel-1
        if menu_sel==-1 then menu_sel=4 end
    elseif key.press(key.ESCAPE) or pad.press(btnPause) then
        menu_count=0
        menu_sel=0
        menu_state=update_main_menu
    end
end

function update_main_menu()
    menu_count=menu_count+1
    local parpadeig=false
    if menu_count>=20 then
        parpadeig=true
        if menu_count>40 then menu_count=0 end
    end
    draw.rect(28*o2aX,(18+(10*menu_sel))*o2aX,73*o2aX,9*o2aX,14)
    if (not parpadeig) then draw.rect(28*o2aX,(18+(10*menu_sel))*o2aX,73*o2aX,9*o2aX,13) end
    -- text("JUGAR", 54*o2aX, 20*o2aX, 14)
    arc_text("JUGAR", 54*o2aX, 20*o2aX, 14)
    -- text("OPCIONS", 50*o2aX, 30*o2aX, 14)
    arc_text("OPCIONS", 50*o2aX, 30*o2aX, 14)
    --if (not parpadeig or menu_sel~=2) then text("REDEFINIR GAMEPAD", 30, 32, 13) end

    redraw_menu_hab()

    if key.press(keyShoot) or key.press(key.SPACE) or pad.press(pad.A) or pad.press(btnShoot) then
        if menu_sel==0 then
            game_exit()
            game_init()
            fade.fadeoutin()
        elseif menu_sel==1 then
            menu_count=0
            menu_sel=0
            menu_state=update_options_menu--update_redefine_keys_menu;
        else
            menu_count=0
            menu_state=update_redefine_pad_menu
        end
    elseif key.press(keyDown) or key.press(key.DOWN) or pad.press(pad.DOWN) or pad.press(btnDown) then
        menu_sel=menu_sel+1
        if menu_sel==2 then menu_sel=0 end
    elseif key.press(keyUp) or key.press(key.UP) or pad.press(pad.UP) or pad.press(btnUp) then
        menu_sel=menu_sel-1
        if menu_sel==-1 then menu_sel=1 end
    elseif key.press(key.ESCAPE) or pad.press(btnPause) then
        sys.quit()
    end
end

function update_redefine_keys_menu()
    if key.press(key.ESCAPE) then
        menu_count = 0
        menu_state = update_options_menu;
    end
    if menu_count==0 then
        -- text("PULSA TECLA PER A AMUNT", 20, 24, 13)
        arc_textB("PULSA TECLA PER A AMUNT", 20*o2aX, 24*o2aX, 13)
        local k = key.press();
        if k ~= 0 and k~=key.ESCAPE then
            menu_count = 1
            keyUp=k
            config.key("keyup", keyUp)
        end
    elseif menu_count==1 then
        -- text("PULSA TECLA PER A AVALL", 20, 24, 13)
        arc_textB("PULSA TECLA PER A AVALL", 20*o2aX, 24*o2aX, 13)
        local k = key.press();
        if k ~= 0 and k~=key.ESCAPE then
            menu_count = 2
            keyDown=k
            config.key("keydown", keyDown)
        end
    elseif menu_count==2 then
        -- text("PULSA TECLA PER A ESQUERRA", 14, 24, 13)
        arc_textB("PULSA TECLA PER A ESQUERRA", 14*o2aX, 24*o2aX, 13)
        local k = key.press();
        if k ~= 0 and k~=key.ESCAPE then
            menu_count = 3
            keyLeft=k
            config.key("keleft", keyLeft)
        end
    elseif menu_count==3 then
        -- text("PULSA TECLA PER A DRETA", 20, 24, 13)
        arc_textB("PULSA TECLA PER A DRETA", 20*o2aX, 24*o2aX, 13)
        local k = key.press();
        if k ~= 0 and k~=key.ESCAPE then
            menu_count = 4
            keyRight=k
            config.key("keyright", keyRight)
        end
    elseif menu_count==4 then
        -- text("PULSA TECLA PER A BOTAR", 20, 24, 13)
        arc_textB("PULSA TECLA PER A BOTAR", 20*o2aX, 24*o2aX, 13)
        local k = key.press();
        if k ~= 0 and k~=key.ESCAPE then
            menu_count = 5
            keyJump=k
            config.key("keyjump", keyJump)
        end
    elseif menu_count==5 then
        -- text("PULSA TECLA PER A DISPAR", 18, 24, 13)
        arc_textB("PULSA TECLA PER A DISPAR", 18*o2aX, 24*o2aX, 13)
        local k = key.press();
        if k ~= 0 and k~=key.ESCAPE then
            menu_count = 0
            menu_state = update_options_menu;
            keyShoot=k
            config.key("keyshoot", keyShoot)
        end
    end
end

function update_redefine_pad_menu()
    if key.press(key.ESCAPE) then
        menu_count = 0
        menu_state = update_options_menu;
    end
    if menu_count==0 then
        -- text("PULSA BOTÓ PER A AMUNT", 22, 24, 13)
        arc_textB("PULSA BOTÓ PER A AMUNT", 22*o2aX, 24*o2aX, 13)
        local btn = pad.press();
        if btn ~= -1 then
            menu_count = 1
            btnUp=btn
            config.key("btnup", btnUp)
        end
    elseif menu_count==1 then
        -- text("PULSA BOTÓ PER A AVALL", 22, 24, 13)
        arc_textB("PULSA BOTÓ PER A AVALL", 22*o2aX, 24*o2aX, 13)
        local btn = pad.press();
        if btn ~= -1 then
            menu_count = 2
            btnDown=btn
            config.key("btndown", btnDown)
        end
    elseif menu_count==2 then
        -- text("PULSA BOTÓ PER A ESQUERRA", 16, 24, 13)
        arc_textB("PULSA BOTÓ PER A ESQUERRA", 16*o2aX, 24*o2aX, 13)
        local btn = pad.press();
        if btn ~= -1 then
            menu_count = 3
            btnLeft=btn
            config.key("keleft", btnLeft)
        end
    elseif menu_count==3 then
        -- text("PULSA BOTÓ PER A DRETA", 22, 24, 13)
        arc_textB("PULSA BOTÓ PER A DRETA", 22*o2aX, 24*o2aX, 13)
        local btn = pad.press();
        if btn ~= -1 then
            menu_count = 4
            btnRight=btn
            config.key("btnright", btnRight)
        end
    elseif menu_count==4 then
        -- text("PULSA BOTÓ PER A BOTAR", 22, 24, 13)
        arc_textB("PULSA BOTÓ PER A BOTAR", 22*o2aX, 24*o2aX, 13)
        local btn = pad.press();
        if btn ~= -1 then
            menu_count = 5
            btnJump=btn
            config.key("btnjump", btnJump)
        end
    elseif menu_count==5 then
        -- text("PULSA BOTÓ PER A DISPAR", 20, 24, 13)
        arc_textB("PULSA BOTÓ PER A DISPAR", 20*o2aX, 24*o2aX, 13)
        local btn = pad.press();
        if btn ~= -1 then
            menu_count = 6
            btnShoot=btn
            config.key("btnshoot", btnShoot)
        end
    elseif menu_count==6 then
        -- text("PULSA BOTÓ PER A GPS ARRERE", 14, 24, 13)
        arc_textB("PULSA BOTÓ PER A GPS ARRERE", 14*o2aX, 24*o2aX, 13)
        local btn = pad.press();
        if btn ~= -1 then
            menu_count = 7
            btnCycle1=btn
            config.key("btncycle1", btnCycle1)
        end
    elseif menu_count==7 then
        -- text("PULSA BOTÓ PER A GPS AVANT", 14, 24, 13)
        arc_textB("PULSA BOTÓ PER A GPS AVANT", 14*o2aX, 24*o2aX, 13)
        local btn = pad.press();
        if btn ~= -1 then
            menu_count = 8
            btnCycle2=btn
            config.key("btncycle2", btnCycle2)
        end
    elseif menu_count==8 then
        -- text("PULSA BOTÓ PER A PAUSA", 22, 24, 13)
        arc_textB("PULSA BOTÓ PER A PAUSA", 22*o2aX, 24*o2aX, 13)
        local btn = pad.press();
        if btn ~= -1 then
            menu_count = 0
            menu_state = update_options_menu;
            btnPause=btn
            config.key("btnpause", btnPause)
        end
    end
end

function game_exit()
    mapa_restore_backup()
    actors={}
    cameras={}
end

function aabb(a, b)
    return (a.x+a.bb.x+a.bb.w >= b.x+b.bb.x) and (a.x+a.bb.x <= b.x+b.bb.x+b.bb.w) and (a.y+a.bb.y+a.bb.h >= b.y+b.bb.y) and (a.y+a.bb.y <= b.y+b.bb.y+b.bb.h)
end

function pause()
    surf.source(0)
    surf.target(back)
    draw.surf(0,0,128*o2aX,96*o2aX,0,0)
    surf.target(0)
    surf.source(tiles)
    pausa_option=1
    pause_old_update=game_update
    game_update=update_pause
end

function update_pause()
    draw.rectf(16*o2aX,16*o2aX,97*o2aX,65*o2aX,16)
    draw.rect(16*o2aX,16*o2aX,97*o2aX,65*o2aX,15)
    -- draw.text("PAUSA",54*o2aX,20*o2aX,15)
    arc_text("PAUSA",54*o2aX,20*o2aX,15)

    menu_count=menu_count+1
    local parpadeig=false
    if menu_count>=20 then
        parpadeig=true
        if menu_count>40 then menu_count=0 end
    end
    draw.rect(28*o2aX,(33+(10*(pausa_option-1)))*o2aX,73*o2aX,9*o2aX,14)
    if (not parpadeig) then draw.rect(28*o2aX,(33+(10*(pausa_option-1)))*o2aX,73*o2aX,9*o2aX,13) end

    --draw.rect(28,33+(10*(pausa_option-1)),73,9,13)

    -- draw.text("CONTINUAR",30*o2aX,35*o2aX,14)
    arc_text("CONTINUAR",30*o2aX,35*o2aX,14)
    -- draw.text("MUSICA:",30*o2aX,45*o2aX,14)
    arc_text("MUSICA:",30*o2aX,45*o2aX,14)
    if music.enabled() then
        -- draw.text("SI",91*o2aX,45*o2aX,15)
        arc_text("SI",91*o2aX,45*o2aX,15)
    else
        -- draw.text("NO",91*o2aX,45*o2aX,15)
        arc_text("NO",91*o2aX,45*o2aX,15)
    end
    -- draw.text("SÓ:",30*o2aX,55*o2aX,14)
    arc_text("SÓ:",30*o2aX,55*o2aX,14)
    if sound.enabled() then
        -- draw.text("SI",91*o2aX,55*o2aX,15)
        arc_text("SI",91*o2aX,55*o2aX,15)
    else
        -- draw.text("NO",91*o2aX,55*o2aX,15)
        arc_text("NO",91*o2aX,55*o2aX,15)
    end
    -- draw.text("EIXIR",30*o2aX,65*o2aX,14)
    arc_text("EIXIR",30*o2aX,65*o2aX,14)

    if key.press(key.ESCAPE) then
        surf.source(back)
        draw.surf(0,0,128*o2aX,96*o2aX,0,0)
        surf.source(tiles)    
        game_update = pause_old_update
    elseif key.press(keyDown) or pad.press(btnDown) then
        pausa_option = pausa_option + 1
        if pausa_option == 5 then pausa_option = 1 end
    elseif key.press(keyUp) or pad.press(btnUp) then
        pausa_option = pausa_option - 1
        if pausa_option == 0 then pausa_option = 4 end
    elseif key.press(keyShoot) or pad.press(btnShoot) then
        if pausa_option==1 then
            surf.source(back)
            draw.surf(0,0,128*o2aX,96*o2aX,0,0)
            surf.source(tiles)    
            game_update = pause_old_update
        elseif pausa_option==2 then
            music.enabled(not music.enabled())
        elseif pausa_option==3 then
            sound.enabled(not sound.enabled())
        else
            game_exit()
            game_init(true)
        end
    end
end
