trigger={}

function trigger.new(_hab,_x,_y,_action,_escena,_id,_type,_doors,_linked_id)
    local world_x, world_y = coords.room_to_world(_hab,_x,_y)
    _bb={x=0,y=0,w=16,h=16} --default
    -- if _type=="switch" then _bb={x=0,y=0,w=16,h=17} end
    -- if _id then print("NEW => ".._id) end
    _type = _type or "invisible"
    return {
        name="trigger",
        type=_type,
        id=_id,
        escena=_escena,
        hab=_hab,
        x=world_x,
        y=world_y,
        w=16,
        h=16,
        fun=_action,
        doors=_doors,
        update=trigger.update,
        draw=trigger.draw,
        linked_id=_linked_id,
        enabled=true,
        disable_reason="",
        bb=_bb,
        run=trigger.run }
end

function trigger:run()
    if self.fun ~= nil then
        self:fun()
    end
end

function trigger:update()
    if not self.enabled then return end
    if self.hab==abad.hab then
        if collision(abad,self) then
            if self.type=="switch" and self.enabled then
                sound.play(audio_switch)
                self.enabled=false
                if self.linked_id then
                    disable_actor_by_id(self.linked_id)
                end
            end
            self:fun()
        end
    end   
end

function trigger:draw()
    if self.type~="switch" and not self.enabled then return end
    local scr_x, scr_y = viewp:screen_coords( self.x, self.y )
    if self.type=="switch" then
        if self.enabled then
            draw.surf(240,128,16,16,scr_x,scr_y,16,16)
        else
            -- "flipar" i desactivar en gris
            pal.subpal(2,1)
            draw.surf(240,128,16,16,scr_x,scr_y,16,16,true)
            pal.subpal(2)
        end    
    end
       
    -- debug rect
    -- draw.rect(scr_x,scr_y,self.bb.w,self.bb.h,3)
end

--function trigger:do_touched()
--    start_scene(self.scene)
--    remove_actor(self)
--end

triggers={}

function triggers:open_door()
    local hab= self.doors[1]-1
    if hab~=abad.hab then
        remote_view(hab)
    end
    for i=2,#self.doors do
        local tile_idx= self.doors[i]+1
        arc_set_tile_by_index(hab,tile_idx,mapa_empty_tile)
    end
end

function triggers:escena_abad_inici()
    start_scene(scenes.abad_inici)
    remove_actor(self)
end

function triggers:escena_abad_corfes()
    start_scene(scenes.abad_corfes)
    remove_actor(self)
end

function triggers:escena_abad_portes()
    local wx1,wy1 = coords.room_to_world(abad.hab,2,4)
    local wx2,wy2 = coords.room_to_world(abad.hab,6,3)
    if abad.escena_portes==nil 
       and ( (abad.hab==31 and arc_get_tile(wx1,wy1)~=256) 
              or (abad.hab==71 and arc_get_tile(wx2,wy2)~=256) ) then
        start_scene(scenes.abad_portes)
    end
    abad.escena_portes=true
    remove_actor(self)
end

function triggers:escena_abad_porta_casa()
    local wx1,wy1 = coords.room_to_world(abad.hab,6,4)
    if arc_get_tile(wx1,wy1)~=256 then
        start_scene(scenes.abad_porta_casa)
    end
    remove_actor(self)
end

function triggers:escena_abad_porta_batman()
    local wx1,wy1 = coords.room_to_world(abad.hab,5,4)
    if arc_get_tile(wx1,wy1)~=256 then
        start_scene(scenes.abad_porta_batman)
    end
    remove_actor(self)
end

function triggers:escena_habitacio_batman()
    if abad.objects.clau_premiere~=nil and 
       abad.objects.clau_elalien~=nil 
    then
        -- switches.start(0,0)
        local trigger_porta = trigger.new(abad.hab,1,1,triggers.open_door,"switch","porta acces cacaus","",{20, 53,41})
        trigger_porta:run()
        bol.furtat=true
        abad.objects.clau_premiere=nil
        abad.objects.clau_elalien=nil
        remove_actor(self)
    else
        if batman.habitacio_porta_intro==nil then
            start_scene(scenes.habitacio_batman)
            batman.habitacio_porta_intro=true
        end
    end
end

function triggers:escena_stage1_ending()
    -- print("STAGE 1 ENDING")
    -- abad.update = abad_nop
    remove_actor(self)
    states:executar("stage1_ending", true)
    -- live_scene.start("stage1_ending", "add")
    -- start_scene(scenes.stage1_ending, nil, false)
end

function triggers:escena_lluita_imp()
    -- set_actors_enabled_by_room(false, "boss", stage1_boss_mapa.r0.r, stage1_boss_mapa.r1.r)
    -- viewp:fixed({l=8,r=44,u=0,d=0}, viewp.x, viewp.y-48)
    -- start_scene(scenes.lluita_imp, nil, false)
    -- remove_actor(self)
    -- stages.stage1_boss_ready()
    remove_actor(self)
    states:executar("stage1_boss_intro", true)
end

-- function triggers:teleport_a()
--     abad.teleport=true
--     sound.play(audio_hit)
--     surf.cls(14)
--     imp.reset()
--     abad.hab=67
--     abad.x=24
-- end
-- 
-- function triggers:teleport_b()
--     if abad.teleport then
--         sound.play(audio_hit)
--         surf.cls(14)
--         imp.reset()
--         abad.hab=14
--         abad.x=16
--     end
-- end
-- 
-- function triggers:teleport_c()
--     abad.teleport2=true
--     sound.play(audio_hit)
--     surf.cls(14)
--     imp.reset()
--     abad.hab=61
--     abad.x=24
-- end
-- 
-- function triggers:teleport_d()
--     if abad.teleport2 then
--         sound.play(audio_hit)
--         surf.cls(14)
--         imp.reset()
--         abad.hab=29
--         abad.x=24
--     end
-- end
