stages = {}

stages.boss_loaded   = false
stages.boss_ready    = false
stages.boss_finished = false
boss = nil

function stages.stage1_init()
    mapa = stage1_mapa
    tiletype={void=0,nonpc=1,stair=2,switch=3,half=4,block=5}
    mapa.wait=0
    mapa.step=0
    load_tilemap( sf_mapa )
    
    llibre.init()
    table.insert(actors,llibre)
    
    gorro.init()
    table.insert(actors,gorro)
    
    peu.init()
    table.insert(actors,peu)
    
    premiere.init()
    table.insert(actors,premiere)
    
    elalien.init()
    table.insert(actors,elalien)
    
    batvio.init()
    table.insert(actors,batvio)
    
    boss = imp.new(55,4,3)
    table.insert( actors, boss )
    boss_meter = bar_meter.new(20,12,128,10,1,1,boss,5)
    
    --batman
    
    bol.init()
    table.insert(actors,bol)
    
    table.insert( actors, gota.new(70,5,1,20) )
    table.insert( actors, gota.new(70,9,1,30) )
    table.insert( actors, gota.new(63,5,1,25,2,-7) )
    table.insert( actors, gota.new(63,9,1,20,0,-8) )
    table.insert( actors, gota.new(64,3,1,10,0,-6) )
    table.insert( actors, gota.new(64,6,1,25,-1,-5) )
    table.insert( actors, gota.new(65,7,1,10,0,-7) )
    table.insert( actors, gota.new(65,8,1,25,0,-7) )
    table.insert( actors, gota.new(66,3,1,10,0,-7) )
    table.insert( actors, gota.new(66,4,1,15,0,-7) )
    table.insert( actors, gota.new(77,2,1,30,-5,-7) )
    table.insert( actors, gota.new(77,9,1,25,-5,-7) )
    
    -- FOC?
    table.insert(actors,invisible.new(77,5,4))
    table.insert(actors,invisible.new(77,6,4))
    table.insert(actors,invisible.new(58,2,4))
    table.insert(actors,invisible.new(58,3,4))
    
    table.insert( actors, caco.new(13,3,2,true ) )
    table.insert( actors, caco.new(17,3,2,false) )
    table.insert( actors, caco.new(20,3,2,false) )
    table.insert( actors, caco.new(26,3,2,false) )
    table.insert( actors, caco.new(30,4,2,false) )
    table.insert( actors, caco.new(37,3,2,false) )
    table.insert( actors, caco.new(41,3,2,false) )
    table.insert( actors, caco.new(40,3,2,false) )
    table.insert( actors, caco.new(45,3,2,false) )
    table.insert( actors, caco.new(50,3,2,false) )
    table.insert( actors, caco.new(57,3,2,false) )
    table.insert( actors, caco.new(61,3,2,false) )
    table.insert( actors, caco.new(74,3,2,false) )
    table.insert( actors, caco.new(75,3,2,false) )
    
    local zombie1 = zombie.new(2,  3, 3,false)
    table.insert( actors, zombie1 )
    table.insert( actors, zombie.new(2,  4, 3,true) )
    table.insert( actors, zombie.new(2,  5, 3,false) )
    table.insert( actors, zombie.new(3,  3, 3,true) )
    table.insert( actors, zombie.new(23, 0, 3,false) )
    table.insert( actors, zombie.new(23, 9, 3,true) )
    table.insert( actors, zombie.new(24, 6, 3,true) )
    table.insert( actors, zombie.new(28, 3, 3,false) )
    table.insert( actors, zombie.new(32, 3, 3,false) )
    table.insert( actors, zombie.new(42, 7, 3,true) )
    table.insert( actors, zombie.new(43, 3, 3,true) )
    table.insert( actors, zombie.new(44, 3, 3,false) )
    table.insert( actors, zombie.new(46, 3, 3,false,2) )
    table.insert( actors, zombie.new(46, 1, 3,false) )
    table.insert( actors, zombie.new(47, 1, 3,true) )
    table.insert( actors, zombie.new(47, 9, 3,false) )
    table.insert( actors, zombie.new(54, 3, 3,false) )
    table.insert( actors, zombie.new(59, 1, 3,false) )
    table.insert( actors, zombie.new(67, 8, 3,false,2) )
    table.insert( actors, zombie.new(68, 3, 3,false) )
    local zombie2 = zombie.new(73, 3, 3,false)
    table.insert( actors, zombie2 )
    table.insert( actors, zombie.new(73, 5, 3,false) )
    table.insert( actors, zombie.new(73, 7, 3,false) )
    table.insert( actors, zombie.new(75, 3, 3,true) )
    
    local hp_x, hp_y = coords.room_to_world(14, 10, 4)
    table.insert( actors, health_potion.new(14, hp_x, hp_y, 0, 0, 2) )
    local hp_x, hp_y = coords.room_to_world(77, 8, 4)
    table.insert( actors, health_potion.new(77, hp_x, hp_y, 0, 0, 3) )
    
    
    -- TRIGGERS
    -- if not menu then table.insert(actors,trigger.new(10,57,32,triggers.escena_abad_inici)) end
    table.insert( actors, trigger.new(10,7,3,triggers.escena_abad_inici,"inici","TR01") )
    table.insert( actors, trigger.new(11,2,3,triggers.escena_abad_corfes,"corfes","TR02") )

    table.insert( actors, trigger.new(31,1,3,triggers.escena_abad_portes,"portes","TR03") )
    table.insert( actors, trigger.new(31,6,3,triggers.escena_abad_portes,"portes","TR04") )
    table.insert( actors, trigger.new(71,7,3,triggers.escena_abad_portes,"portes","TR05") )

    table.insert( actors, trigger.new(53,5,3,triggers.escena_abad_porta_casa,"porta casa","TR06"))
    table.insert( actors, trigger.new(56,4,3,triggers.escena_abad_porta_batman,"porta batman","TR07"))

    table.insert( actors, trigger.new(19,4,3,triggers.escena_habitacio_batman,"habitacio batman","TR08"))
    -- 
    table.insert( actors, trigger.new(55,1,3,triggers.escena_lluita_imp,"lluita imp"))
    
    table.insert( actors, trigger.new(10,8,1,triggers.open_door,"","primera porta","switch",{11, 57,45}) )
    table.insert( actors, trigger.new(34,1,1,triggers.open_door,"","dos portes casa abad","switch",{32, 50,38,26,53,41,29},{"TR03","TR04"}) )
    table.insert( actors, trigger.new(52,1,1,triggers.open_door,"","porta casa abad","switch",{72, 42,30},{"TR05"}) )
    table.insert( actors, trigger.new(60,1,1,triggers.open_door,"","altillo","switch",{54, 54,42},{"TR06"}) )
    table.insert( actors, trigger.new(75,6,1,triggers.open_door,"","pasillo inf","switch",{77, 50,38,26}) )
    table.insert( actors, trigger.new(75,9,1,triggers.open_door,"","acces inf casa batman","switch",{57, 53,41},{"TR07"}) )
    table.insert( actors, trigger.new(79,5,1,triggers.open_door,"","sotan casa batman","switch",{36, 56,44}) )
    table.insert( actors, trigger.new(38,2,2,triggers.open_door,"","","switch",{39, 62,63}) )
    -- table.insert( actors, trigger.new(1, 1,1,triggers.open_door,"","acces cacaus","switch",{20, 53,41},{"TR08"}))
    
    -- 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))   

    trigger_event.new("Premiere dialeg 1",
                       trigger_ev.dialeg_trigger, 
                       trigger_ev.dialeg_init, 
                       nil,
                       premiere, abad, 150, "Ma que cosa fare", 150, 0, true)

    trigger_event.new("Zombie 1 dialeg",
                       trigger_ev.dialeg_trigger, 
                       trigger_ev.dialeg_init, 
                       nil,
                       zombie1, abad, 150, "Sereeeeebrooooosssss", 250, nil, true)

    trigger_event.new("Zombie 2 dialeg",
                       trigger_ev.dialeg_trigger, 
                       trigger_ev.dialeg_init, 
                       nil,
                       zombie2, abad, 150, "Aaaaaargh... per ell!", 250, nil, true)

    trigger_event.new("Elalien dialeg",
                       trigger_ev.dialeg_trigger, 
                       trigger_ev.dialeg_init, 
                       nil,
                       elalien, abad, 150, "I les tisores de podar?", 250, nil, true)
                       
    trigger_event.new("Abad dialeg",
                       trigger_ev.dialeg_trigger, 
                       trigger_ev.dialeg_init, 
                       nil,
                       abad, bol, 150, "Mmmm fa olor a cacaus...", 80, 49, false)

    tiles_layer2.new(13,11,3,144,144,16,16)
    tiles_layer2.new(13,11,4,144,144,16,16)
    tiles_layer2.new(12,5,2,128,208,16,16, tiles_layer2.update_aranya, tiles_layer2.draw_aranya)
    tiles_layer2.new(76,3,2,128,208,16,16, tiles_layer2.update_aranya, tiles_layer2.draw_aranya)
    tiles_layer2.new(78,6,2,128,208,16,16, tiles_layer2.update_aranya, tiles_layer2.draw_aranya)
    
    local abad_x, abad_y = coords.room_to_world ( 10, 4, 3 )
    -- local abad_x, abad_y = coords.room_to_world ( 54, 8, 3 )
    -- table.insert( actors, trigger.new(8,4,3,triggers.escena_stage1_ending,"stage1 ending","TR09") )
    -- local abad_x, abad_y = coords.room_to_world ( 8, 9, 3 )
    
    abad:move(abad_x, abad_y)
    abad_make_safe( true )
    
    local scr_ax, scr_ay = viewp:screen_coords(abad_x, abad_y)
    
    remote_view_init()
    
    viewp:free_move()
    
    stages.boss_ready = false
    stages.boss_loaded = false
end

function stages.stage1_update()
    -- print("stage1_update")
    -- fireball.update()
    -- if (abad.energia<=abad.max_energia/2) then
    --     -- Requeriment: tindre mitja clau
    --     premiere.healer_init()
    -- end
end

function stages.stage1_draw_back()
    -- print("stage1_draw_back")
end

function stages.stage1_draw_middle()
    -- print("stage1_draw_middle")
end

function stages.stage1_draw_front()
    -- print("stage1_draw_front")
    -- fireball.draw()
    -- boss_meter:draw()
end

function stages.stage2_init()
  -- print("stage 2 init")
end

-- require "stage1_maps"
require "stage1_world_map"
require "stage1_boss_map"
require "stage1_boss"
require "stage1_ending"
