require "fade"
require "game"
require "mapa"
require "scenes"

local arcade_config = require("arcade_config")

intro_wait=40
intro_step=0

o2aX = arcade_config.org2arc_escala

function intro_init()
    game_update = intro_intro
    intro_wait=400
    surf.cls(16)
	
    font.current(font_sf)
    surf.target(logo)
    surf.cls(16)
    draw.text("JAILGAMES",0,0,15)
    surf.target(0)
    surf.source(logo)
    draw.surf(0,0,72,10,48,70,arcade_config.logo_sf.width,arcade_config.logo_sf.height)
	
	-- surf.target(logo)
    -- surf.cls(16)
	-- text("presenta",0,0,14)
    -- surf.target(0)
    -- surf.source(logo)
    -- draw.surf(0,0,36,5,96,100,36*o2aX, 5*o2aX)
	
    arc_textB("presenta",96,102,14)
	-- draw.surf(0,0,36,5,28,35,72,10)
    -- draw.surf(0,0,36,5,56,70,arcade_config.logo_sf.width,arcade_config.logo_sf.height)
    surf.source(tiles)
    fade.fadein()
end

function intro_intro()
    -- text("presenta",48,50,14)

    intro_wait=intro_wait-1
    if intro_wait==0 or key.press(key.ESCAPE) or key.press(keyShoot) or pad.press(btnShoot) or pad.press(btnPause) then
        intro_wait=1
        game_update = intro_update
        fade.fadeoutin()
    end

end

function draw_item_intro( name, flip )
	local cw = arcade_config.character_width
	local ch = arcade_config.character_height
	if ( name == "abad" ) then
		draw.surf(0,0,cw,ch,44*o2aX,24*o2aX,cw,ch,flip)
	elseif (name == "batman" ) then
		draw.surf(0,48*o2aX,cw,ch,82*o2aX,24*o2aX,cw,ch,flip)
	elseif (name == "cacaus" ) then
		draw.surf(112*o2aX,88*o2aX,cw,ch/2,76*o2aX,32*o2aX,cw,ch/2,flip)
	elseif (name == "marc" ) then
		draw.rect(15*o2aX,3*o2aX,99*o2aX,51*o2aX,2)
	elseif (name == "pas porta" ) then
		draw.rectf(73*o2aX,24*o2aX,7*o2aX,16*o2aX,16)
	end
end

function draw_escenari ()
	surf.cls(16)
	draw_item_intro("marc",false)
	view.origin(16*o2aX,4*o2aX)
	mapa_draw(10)
	-- Borrar la porta del mapa
	draw_item_intro("pas porta")
end

function intro_update()
    if key.press(key.ESCAPE) or pad.press(btnPause) then
        game_init(true)
        fade.fadeoutin()
    elseif key.press(key.SPACE) then
        intro_wait=1
    end
	
    intro_wait=intro_wait-1
    if intro_wait==0 then
	    -- STEP 0
        if intro_step==0 then
            draw_escenari()
            draw_item_intro("abad", true)
            view.origin(0,0)
            intro_step=intro_step+1
	    -- STEP 1
        elseif intro_step==1 then
            start_scene(scenes.intro_01,58)
            intro_step=intro_step+1
	    -- STEP 2
        elseif intro_step==2 then
            draw_escenari()
            draw_item_intro("abad", false)
            view.origin(0,0)
            intro_step=intro_step+1
	    -- STEP 3
        elseif intro_step==3 then
            start_scene(scenes.intro_02,58)
            intro_step=intro_step+1
	    -- STEP 4
        elseif intro_step==4 then
            draw_escenari()
            draw_item_intro("abad", false)
			draw_item_intro("cacaus", true)
			draw_item_intro("batman", true)
            view.origin(0,0)
            intro_step=intro_step+1
	    -- STEP 5
        elseif intro_step==5 then
            start_scene(scenes.intro_03,58)
            intro_step=intro_step+1
	    -- STEP 6
        elseif intro_step==6 then
            draw_escenari()
            draw_item_intro("abad", false)
            view.origin(0,0)
            intro_step=intro_step+1
	    -- STEP 7
        elseif intro_step==7 then
            start_scene(scenes.intro_04,58)
            intro_step=intro_step+1
	    -- STEP 8
        elseif intro_step==8 then
            music.play(audio_main_song)
            game_init(true)
            fade.fadeoutin()
        end
        intro_wait=50
    end
end