local abad={x=0,y=0,w=32,h=32,dx=88,dy=48,dw=32,dh=32}
local batman={x=0,y=96,w=32,h=32,dx=164,dy=48,dw=32,dh=32}
local cacaus={x=224,y=176,w=32,h=16,dx=152,dy=64,dw=32,dh=16}
local cacaus_trans={x=192,y=176,w=16,h=8,dx=65,dy=56,dw=16,dh=8}
local marc={x=30,y=6,w=198,h=102,color=2}
local pas_porta={x=146,y=48,w=14,h=32,color=16}
local scene_x_offset = 32
local scene_y_offset = 8

intro_wait=40
intro_step=0

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

function draw_item_intro( name, flip, mood )
	if ( name == "abad" ) then
        -- abad.mood = mood or "normal"
		draw.surf(abad.x, abad.y, abad.w, abad.h, abad.dx, abad.dy, abad.dw, abad.dh, flip)
        abad_add_mood(abad.dx,0,abad.dy,0,flip,mood)
	elseif (name == "batman" ) then
		draw.surf(batman.x, batman.y, batman.w, batman.h, batman.dx, batman.dy, batman.dw, batman.dh, flip)
	elseif (name == "cacaus" ) then
		draw.surf(cacaus.x, cacaus.y, cacaus.w, cacaus.h, cacaus.dx, cacaus.dy, cacaus.dw, cacaus.dh, flip)
    elseif (name == "cacaus trans" ) then
		draw.surf(cacaus_trans.x, cacaus_trans.y, cacaus_trans.w, cacaus_trans.h, cacaus_trans.dx, cacaus_trans.dy, cacaus_trans.dw, cacaus_trans.dh, flip)
	elseif (name == "marc" ) then
		draw.rect(marc.x, marc.y, marc.w, marc.h, marc.color)
	elseif (name == "pas porta" ) then
		draw.rectf(pas_porta.x, pas_porta.y, pas_porta.w, pas_porta.h, pas_porta.color)
	end
end

function intro_init()
print("INTRO_INIT")
    -- game_update = intro_intro
    intro_wait=400
    surf.source(tiles)
    surf.target(0)
    surf.cls(16)
    fade.fadein()
    font.current(font_sf)
    -- flow:add_path({intro_intro, intro_update})
    states:next()
end

function intro_intro()
    intro_wait=1
    -- game_update = intro_update
    states:next()
    fade.fadeoutin()
end

function intro_update()
    
    if key.press(key.ESCAPE) or pad.press(btnPause) then
        -- game_init(true)
        print("INTRO_UPDATE")
        -- game_update = menu_init
        -- game_init(true)
        intro_end()
    elseif key.press(key.SPACE) then
        intro_wait=1
    end
	
    intro_wait=intro_wait-1
    if intro_wait==0 then
	    -- == INTRO_01
        -- STEP 0
        if intro_step==0 then
            draw_escenari()
            draw_item_intro("abad", false)
            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
            
	    -- == INTRO_02
        -- STEP 2
        elseif intro_step==2 then
            draw_escenari()
            draw_item_intro("abad", true, "sorpresa")
            draw_item_intro("cacaus trans", 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
	    
        -- == INTRO_03
        -- 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
	    
        -- == INTRO_04
        -- 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
            -- game_init(true)
            -- game_update = menu_init
            intro_end()
        end
        intro_wait=50
    end
end

function intro_end()
    print("intro_end")
    fade.fadeoutin()
    states:executar("title")
end

states:registrar("intro", {intro_init, intro_intro, intro_update})