dofile('data/Scripts/scenes/02_legran_gate/anim/02_legran_gate_anim_bg.lua')
dofile('data/Scripts/scenes/02_legran_gate/comics/02_legran_gate_comics_villain.lua')

-------------------------------------------------------------------------------
class 'l_02_legran_gate' (LevelEventHandler)
-------------------------------------------------------------------------------

function l_02_legran_gate:__init(name)
	LevelEventHandler.__init(self, name)
	
	-- init ----------------------------------------
	--ObjectVisible('area_zoom_dog', false)
	ObjectVisible('area_link_mg',  false)
	ObjectVisible('area_link_03',  false)
	
	-- animations ----------------------------------
	self.anim_bg = AnimationBundle(Loader_02_BG_Animations(Object('bg')))
	self.anim_bg:PlayAnimation('back' )
	self.anim_bg:PlayAnimation('front')
	self.anim_bg:PlayAnimation('dust' )
	
	-- link scenes ---------------------------------
	LinkScene('area_link_01', 'CursorDoorDown',   'l_01_dock')
	LinkScene('area_link_03', 'CursorDoorLeftUp', 'l_03_yard')
	LinkScene('area_link_mg', 'CursorGlass',      'l_02_Lock')
	
	-- main logic ----------------------------------
	self:create_comics_villain()
	--[[
	if IsToolFound('MG_02_01_item_02') then
	    ObjectOpacity('layer_dog', 0)
	end
	--]]
	
	if IsMiniGameCompleted('l_02_Lock') then
	    if HasActionCompletedState('doorOpened') then
	        ObjectOpacity('layer_door_open', 1)
	        ObjectVisible('area_link_03', true)
	    else
	        local delay = 0.5
	        local time  = 0.5
	        
	        self.timer:Add( function()
	                            AddActionCompletedState('doorOpened')
	                            
	                            ObjectOpacity('layer_door_open', 1, time)
	                            MakeSoundEntity('02_open_gate'):PlaySound(false)
	                            ObjectVisible('area_link_03', true)
	                        end,
	                        delay)
	    end
	end
	
	RegisterMonologObject('area_hint_lighthouse', 'BBT_02_LIGHTHOUSE')
	RegisterMonologObject('area_hint_house',      'BBT_02_HOUSE')
	
	-- zoom zones ----------------------------------
	
	-- zoom dog
	do
	    CreateSmartZoomZone('dog')
	    ---------------------
	    
	    self.zoom_dog.on_open = function()
	        self:setDogIdleSoundsVolume(0)
            
            if self.sound_dog_idle2 then
                self.sound_dog_idle2:SetVolume(0)
            end
                                    
	        if not IsToolApplied('01_puppy_dog') then
	            if not self.video_dog_zoom_idle then
	                self.video_dog_zoom_idle = CreateVideoEntity('data/Video/Scenes/02_legran_gate/02_dog_zoom_idle.ogg', '', false)
                    self.video_dog_zoom_idle:AttachTo(Object('z_dog_bg'))
                    self.video_dog_zoom_idle:SetZ(1)
                    self.video_dog_zoom_idle:SetXY(0, 0)
                    self.video_dog_zoom_idle:SetLoop(true)
                    self.video_dog_zoom_idle:SetHideOnStop(true)
	            end
	            
	            if not self.sound_dog_zoom_idle then
	                self.sound_dog_zoom_idle = MakeSoundEntity('02_dog_zoom_idle')
	            end
	            
	            self.video_dog_zoom_idle:PlayVideo()
	            self.sound_dog_zoom_idle:PlaySound(true)
	        else
	            if not self.video_dog_zoom_idle2 then
	                self.video_dog_zoom_idle2 = CreateVideoEntity('data/Video/Scenes/02_legran_gate/02_dog_zoom_idle2.ogg', '', false)
                    self.video_dog_zoom_idle2:AttachTo(Object('z_dog_bg'))
                    self.video_dog_zoom_idle2:SetZ(1)
                    self.video_dog_zoom_idle2:SetXY(0, 0)
                    self.video_dog_zoom_idle2:SetLoop(true)
                    self.video_dog_zoom_idle2:SetHideOnStop(true)
	            end
	            
	            if not self.sound_dog_zoom_idle2 then
	                self.sound_dog_zoom_idle2 = MakeSoundEntity('02_dog_zoom_idle2')
	            end
	            
	            self.video_dog_zoom_idle2:PlayVideo()
	            self.sound_dog_zoom_idle2:PlaySound(true)
	        end
	    end
	    ---------------------
	    
	    self.zoom_dog.on_close = function()
            self.timer:Add( function()
                                if self.dogGrowlsAction then
                                    self.dogGrowlsAction:Kill()
                                    self.dogGrowlsAction = nil
                                    
                                    ObjectVisible('area_apply_zd_puppy', true)
                                    
                                    self.video_dog_zoom_growls:StopVideo()
                                    self.video_dog_zoom_growls:SetOpacity(0)
                                    
                                    self.sound_dog_zoom_growls:StopSound()
                                end
                                
                                if self.sound_dog_zoom_idle then
                                    self.sound_dog_zoom_idle:StopSound()
                                end
                                
                                if self.sound_dog_zoom_idle2 then
                                    self.sound_dog_zoom_idle2:StopSound()
                                end
                                
                                self:setDogIdleSoundsVolume(1)
                                
                                if self.sound_dog_idle2 then
                                    self.sound_dog_idle2:SetVolume(0.3)
                                end
            
                                if IsToolApplied('01_puppy_dog') then
                                    self.video_dog_zoom_idle2:StopVideo()
                                else
                                    self.video_dog_zoom_idle:StopVideo()
                                end
                            end,
                            ZOOM_ZONE_APPEARING_TIME)
	    end
	    ---------------------
	    
	    ObjectVisible('area_find_zd_mg_02_01_item_02', false)
	    ---------------------
	    
	    local function on_apply_puppy(e)
            ObjectVisible(e.target, false)
            
            if not IsToolFound('MG_02_01_item_02') then
                self.video_dog_idle2 = CreateVideoEntity('data/Video/Scenes/02_legran_gate/02_dog_idle2.ogg', '', true)
                self.video_dog_idle2:AttachTo(Object('bg'))
                self.video_dog_idle2:SetZ(2)
                self.video_dog_idle2:SetXY(5, 175)
                self.video_dog_idle2:SetLoop(true)
                self.video_dog_idle2:SetHideOnStop(true)
                
                self.sound_dog_idle2 = MakeSoundEntity('02_dog_idle2')
                self.sound_dog_idle2:SetVolume(0.2)
            end
            
            if e.save then
                ObjectVisible('area_link_mg',  true)
                ObjectVisible('area_find_zd_mg_02_01_item_02', true)
                
                if self.video_dog_idle2 then
                    self.video_dog_idle2:PlayVideo()
                    self.sound_dog_idle2:PlaySound(true)
                end
            else
                if self.dogGrowlsAction then
                    self.dogGrowlsAction:Kill()
                    self.dogGrowlsAction = nil
                    
                    self.video_dog_zoom_idle:PlayVideo()
                    self.video_dog_zoom_growls:StopVideo()
                    self.video_dog_zoom_growls:SetOpacity(0)
                    
                    self.sound_dog_zoom_growls:StopSound()
                    self.sound_dog_zoom_idle:PlaySound(true)
                end
                
                local change_time = 0.2
                
                self.video_dog_zoom_idle2 = CreateVideoEntity('data/Video/Scenes/02_legran_gate/02_dog_zoom_idle2.ogg', '', false)
                self.video_dog_zoom_idle2:AttachTo(Object('z_dog_bg'))
                self.video_dog_zoom_idle2:SetZ(1)
                self.video_dog_zoom_idle2:SetXY(0, 0)
                self.video_dog_zoom_idle2:SetLoop(true)
                self.video_dog_zoom_idle2:SetHideOnStop(true)
                self.video_dog_zoom_idle2:PlayVideo()
                self.video_dog_zoom_idle2:ChangeOpacity(0, 1, change_time)
                
                self.video_dog_idle2:PlayVideo()
                self.video_dog_idle2:ChangeOpacity(0, 1, 0.1)
                self.video_dog_idle:ChangeOpacity( 1, 0, 0.1)
                
                self.sound_dog_idle2:PlaySound(true)
                self.sound_dog_idle2:SetVolume(0)
                
                self.sound_dog_zoom_idle2 = MakeSoundEntity('02_dog_zoom_idle2')
	            self.sound_dog_zoom_idle2:PlaySound(true)
	            self.sound_dog_zoom_idle:StopSound()
	            self.sound_dog_zoom_idle = nil
                
                self.timer:Add( function()
                                    self.video_dog_idle:StopVideo()
                                    self:stopDogIdleSounds()
                                end,
                                0.1)
                
                self.mouseBlocker:Push()
                self.timer:Add( function()
                                    self.mouseBlocker:Pop()
                                    self.video_dog_zoom_idle:StopVideo()
                                    
                                    ObjectVisible('area_link_mg', true)
                                    ObjectVisible('area_find_zd_mg_02_01_item_02', true)
                                end,
                                change_time)
            end
        end
	    ---------------------
	    
	    local function on_click_apply_pappy_area(e)
	        if self.dogGrowlsAction then
	            return
	        end
	        
	        if not self.video_dog_zoom_growls then
                self.video_dog_zoom_growls = CreateVideoEntity('data/Video/Scenes/02_legran_gate/02_dog_zoom_growls.ogg', '', false)
                self.video_dog_zoom_growls:AttachTo(Object('z_dog_bg'))
                self.video_dog_zoom_growls:SetZ(2)
                self.video_dog_zoom_growls:SetXY(0, 0)
                self.video_dog_zoom_growls:SetLoop(false)
                self.video_dog_zoom_growls:SetHideOnStop(false)
            end
            
            if not self.sound_dog_zoom_growls then
                self.sound_dog_zoom_growls = MakeSoundEntity('02_dog_zoom_growls')
            end
            
            self.video_dog_zoom_growls:SetOpacity(1)
            self.video_dog_zoom_growls:PlayVideo()
            self.video_dog_zoom_idle:StopVideo()
            
            self.sound_dog_zoom_growls:PlaySound(false)
            self.sound_dog_zoom_idle:StopSound()
            
            --ObjectVisible('area_apply_zd_puppy', false)
            self.dogGrowlsAction = self.timer:Add(  function()
                                                        self.dogGrowlsAction = nil
                                                        --ObjectVisible('area_apply_zd_puppy', true)
                                                        
                                                        self.video_dog_zoom_idle:PlayVideo()
                                                        self.video_dog_zoom_growls:StopVideo()
                                                        self.video_dog_zoom_growls:SetOpacity(0)
                                                        
                                                        self.sound_dog_zoom_idle:PlaySound(true)
                                                    end,
                                                    self.video_dog_zoom_growls:GetDuration())
            
            return 'BBT_02_NEED_APPLY_PUPPY_DOG'
	    end
	    ---------------------
	    
	    local function on_find_mg_02_01_item_02(e)
	        if e.save then
	        else
	            self.video_dog_idle3 = CreateVideoEntity('data/Video/Scenes/02_legran_gate/02_dog_idle3.ogg', '', true)
                self.video_dog_idle3:AttachTo(Object('bg'))
                self.video_dog_idle3:SetZ(29)
                self.video_dog_idle3:SetXY(5, 175)
                self.video_dog_idle3:SetLoop(true)
                self.video_dog_idle3:SetHideOnStop(true)
                
                self.video_dog_idle3:PlayVideo()
                self.video_dog_idle2:StopVideo()
	        end
	        
	        ObjectVisible('area_find_zd_mg_02_01_item_02', false)
	        self.zoom_dog:CloseAndDisable()
	    end
	    ---------------------
	    
	    RegisterToolApplyInfo('01_puppy_dog', 'area_apply_zd_puppy', on_apply_puppy, on_click_apply_pappy_area)
	    RegisterToolFindInfo('MG_02_01_item_02', 'area_find_zd_mg_02_01_item_02', on_find_mg_02_01_item_02)
	end
end

function l_02_legran_gate:startDogIdleSounds(interval)
    if not self.dog_idle_sounds then
        self.dog_idle_sounds = {}
        
        table.insert(self.dog_idle_sounds, MakeSoundEntity('02_dog_idle_v1'))
        table.insert(self.dog_idle_sounds, MakeSoundEntity('02_dog_idle_v2'))
        table.insert(self.dog_idle_sounds, MakeSoundEntity('02_dog_idle_v3'))
    end
    
    local index = math.random(#self.dog_idle_sounds)
    local sound = self.dog_idle_sounds[index]
    
    if self.sog_idle_sound then
        self.sog_idle_sound:StopSound()
        self.sog_idle_sound = nil
    end
    
    sound:PlaySound(false)
    
    self.sog_idle_sound = sound
    self.sog_idle_action = self.timer:Add(  function()
                                                self:startDogIdleSounds(interval)
                                            end,
                                            interval)
end

function l_02_legran_gate:stopDogIdleSounds()
    if self.sog_idle_sound then
        self.sog_idle_sound:StopSound()
        self.sog_idle_sound = nil
    end
    
    if self.sog_idle_action then
        self.sog_idle_action:Kill()
        self.sog_idle_action = nil
    end
end

function l_02_legran_gate:setDogIdleSoundsVolume(volume)
    if self.dog_idle_sounds then
        for i, sound in ipairs(self.dog_idle_sounds) do
            sound:SetVolume(volume)
        end
    end
end

function l_02_legran_gate:create_comics_villain()
    local comics_name = '02_comics_villain'
    
    local comics_finish = function(save)
        if not IsToolApplied('01_puppy_dog') then
            self.video_dog_idle = CreateVideoEntity('data/Video/Scenes/02_legran_gate/02_dog_idle.ogg', '', true)
            self.video_dog_idle:AttachTo(Object('bg'))
            self.video_dog_idle:SetZ(29)
            self.video_dog_idle:SetXY(5, 175)
            self.video_dog_idle:SetLoop(true)
            self.video_dog_idle:SetHideOnStop(true)
        end
    
        if save then
            ObjectOpacity('layer_villain',   0)
            ObjectOpacity('layer_door_open', 0)
            
            if self.video_dog_idle then
                self.video_dog_idle:PlayVideo()
                
                self:startDogIdleSounds(self.video_dog_idle:GetDuration())
            end
        else
            self.video_dog_in = CreateVideoEntity('data/Video/Scenes/02_legran_gate/02_dog_in.ogg', '', true)
            self.video_dog_in:AttachTo(Object('bg'))
            self.video_dog_in:SetZ(29)
            self.video_dog_in:SetXY(-155, 175)
            self.video_dog_in:SetLoop(false)
            self.video_dog_in:SetHideOnStop(false)
            
            MakeSoundEntity('02_dog_in'):PlaySound(false)
            
            local villain_time = 0.5
            ObjectOpacity('layer_villain', 0, villain_time)
            self.video_dog_in:SetOnFirstFrame()
            
            self.timer:Add( function()
                                local door_time = 0.5
                                ObjectOpacity('layer_door_open', 0, door_time)
                                MakeSoundEntity('02_close_gate'):PlaySound(false)
                                
                                self.timer:Add( function()
                                                    SetCutSceneMode(self.video_dog_in:GetDuration())
                                                    self.video_dog_in:PlayVideo()
                                                    
                                                    self.timer:Add( function()
                                                                        self.video_dog_idle:PlayVideo()
                                                                        self:startDogIdleSounds(self.video_dog_idle:GetDuration())
                                                                        
                                                                        self.video_dog_in:SetHideOnStop(true)
                                                                        self.video_dog_in:StopVideo()
                                                                        
                                                                        ObjectVisible('area_zoom_dog', true)
                                                                    end,
                                                                    self.video_dog_in:GetDuration())
                                                end,
                                                door_time)
                            end,
                            villain_time)
        end
    end
    
    if IsComicsShown(comics_name) then
        comics_finish(true)
    else
        ObjectVisible('area_zoom_dog', false)
        

        local comics_dupin = l_02_legran_gate_comics_villain()
		comics_dupin:LoadData(play_field)
		
        local c = Comics(comics_name,
                            CreateComicsVideoFrame('Villain',  'C_02_VILLAIN_01', nil, {path='data/Video/Scenes/02_legran_gate/C_02_VILLAIN_01.ogg', x=553, y=376}, 'comics_villain_bg')
                            )
                                      
        c.on_finish = function()
            comics_finish(false)
        end
        
        self.mouseBlocker:Push()
        self.timer:Add( function()
                            self.mouseBlocker:Pop()
                            c:Start()
                        end,
                        1)
    end
end

function l_02_legran_gate:OnUpdate(dt)
    self.anim_bg:UpdatePeriodicAnimations(dt, {10, 15})
end

function l_02_legran_gate:OnAllItemsFound()
end

function l_02_legran_gate:OnObjectMouseEnter(object)
end

function l_02_legran_gate:OnObjectMouseLeave(object)
end

function l_02_legran_gate:OnScreenObjectMouseDown(object, key)
end

function l_02_legran_gate:OnObjectMouseDownProcessed(is_wrong_click)
end

function l_02_legran_gate:OnMouseMove(previous_pos, current_pos)
end

function l_02_legran_gate:OnObjectMouseUp(object)
end

function l_02_legran_gate:OnObjectFound(object)
end

function l_02_legran_gate:OnToolFound(tool)
end

function l_02_legran_gate:OnToolApply(tool, object)
end

function l_02_legran_gate:OnToolAddToInventory(tool)
end

function l_02_legran_gate:OnObjectReachPanel(object)
end

function l_02_legran_gate:OnButtonPress(title)
end

function l_02_legran_gate:OnSetTool(tool)
end

function l_02_legran_gate:OnLoaded()
end

function l_02_legran_gate:OnDestroy()
end

--------------------------------------------------------------------------------
-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function l_02_legran_gate:LoadData(screen)
	local anim
	anim = CreateAnimation('data/Texture/Scenes/02_Legran_Gate/main/layers/bg.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bg',anim,512,384,50,0,1,1)
	
	anim = CreateAnimation('data/Texture/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_link_01',anim,512,700,100,0,128,17)
	
	anim = CreateAnimation('data/Texture/Common/area_round.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_link_03',anim,331,433,100,0,1.28,1.6)
	
	anim = CreateAnimation('data/Texture/Common/area_round.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_link_mg',anim,331,433,100,0,1.27,1.6)
	
	anim = CreateAnimation('data/Texture/Scenes/02_legran_gate/main/layers/layer_door_open.png',0,0,258,198,1,0)
	anim:SetHotSpot(129,99)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('layer_door_open',anim,363,477,52,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/02_legran_gate/main/layers/layer_villain.png',0,0,154,168,1,0)
	anim:SetHotSpot(77,84)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('layer_villain',anim,278,463,53,0,1,1)
	
	anim = CreateAnimation('data/Texture/Common/area_round.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_hint_lighthouse',anim,728,159,100,0,1,1)
	
	anim = CreateAnimation('data/Texture/Common/area_round.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_hint_house',anim,216,182,100,0,2.32,1.38)
	
	anim = CreateAnimation('data/Texture/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(5,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_zoom_dog',anim,547,517,110,0,30,23)
	
end
-- CLEAR_ON_SAVE_END
