dofile('data/Scripts/scenes/01a_dock_boat/anim/01a_dock_boat_anim.lua')

class 'l_01a_dock_boat' (LevelEventHandler)

function l_01a_dock_boat:__init(name)
	LevelEventHandler.__init(self, name)
	
	if IsMiniGameCompleted('l_01_Box') then
	    FindTool('01_shell')
	end
	
	-- init ----------------------------------------
	ObjectVisible('area_zoom_bag',    false)
	ObjectVisible('area_link_mg',     false)
	ObjectVisible('area_click_chest', false)
	
	-- video ---------------------------------------
	local video = CreateVideoEntity('data/Video/Scenes/01a_dock_boat/01a_water_back.ogg', '', false)
    video:AttachTo(play_field)
    video:SetZ(51)
    video:SetXY(512, 384)
    video:SetScale(2)
    video:SetLoop(true)
    video:PlayVideo()
	
	-- animations ----------------------------------
	self.anim_bg = AnimationBundle(Loader_01_Boat_Animations(Object('bg')))
	self.anim_bg:PlayAnimation('water_rings')
	self.anim_bg:PlayAnimation('boat')
	self.anim_bg:GetAnimation('boat'):GetElementByName('Layer_1'):SetOpacity(0)
	self.anim_bg:GetAnimation('boat'):GetElementByName('Layer_6'):SetOpacity(0)
	self.anim_bg:GetAnimation('boat'):GetElementByName('Layer_8'):SetOpacity(0)
	self.anim_bg:GetAnimation('boat'):GetElementByName('Layer_9'):SetOpacity(0)
	
	
	-- link scenes ---------------------------------
	LinkScene('area_link_01', 'CursorDoorDown', 'l_01_dock')
	LinkScene('area_link_mg', 'CursorGlass',    'l_01_Box')
	
	-- main logic ----------------------------------
	if not IsToolApplied('01_boathook') then
	    local basePos = Vector(-130, -270)
	    
	    self.raven_idle_video = CreateVideoEntity('data/Video/Scenes/01a_dock_boat/01a_raven_idle.ogg', '', true)
        self.raven_idle_video:AttachTo(Object('bg'))
        self.raven_idle_video:SetZ(100)
        self.raven_idle_video:SetXY(basePos.x, basePos.y)
        self.raven_idle_video:SetLoop(true)
        self.raven_idle_video:PlayVideo()
        
        
        self.raven_effector = CreateAnimationEffector()
        self.raven_effector:AttachTo(self.raven_idle_video)
        self.raven_effector:SetMode(_ANIM_FWD + _ANIM_LOOP)
        self.raven_effector:SetKey(_TRACK_X, 0,   basePos.x)
        self.raven_effector:SetKey(_TRACK_X, 2.5, basePos.x + 12)
        self.raven_effector:SetKey(_TRACK_X, 5,   basePos.x)
        self.raven_effector:SetKey(_TRACK_X, 7.5, basePos.x + 12)
        self.raven_effector:SetKey(_TRACK_X, 10,  basePos.x)
        self.raven_effector:SetKey(_TRACK_Y, 0,   basePos.y)
        self.raven_effector:SetKey(_TRACK_Y, 2.5, basePos.y - 16)
        self.raven_effector:SetKey(_TRACK_Y, 5,   basePos.y)
        self.raven_effector:SetKey(_TRACK_Y, 7.5, basePos.y - 16)
        self.raven_effector:SetKey(_TRACK_Y, 10,  basePos.y)
        self.raven_effector:Play()
	end
	
	local function on_apply_boathook(e)
	    ObjectVisible(e.target, false)
	    
	    local pos = Vector(432 - SCR_W/2, 525.7 - SCR_H/2)
	    
	    if e.save then
	        ObjectVisible('area_zoom_bag',    true)
	        ObjectVisible('area_click_chest', true)
	        
	        self.anim_bg:GetAnimation('boat'):SetXY(pos.x, pos.y)
	    else
	        self.raven_effector:Destroy()
	        local offset = Vector(262, -18)
	        
	        self.raven_out_video = CreateVideoEntity('data/Video/Scenes/01a_dock_boat/01a_raven_out.ogg', '', true)
            self.raven_out_video:AttachTo(Object('bg'))
            self.raven_out_video:SetZ(100)
            self.raven_out_video:SetXY(self.raven_idle_video:GetX() + offset.x, self.raven_idle_video:GetY() + offset.y)
            self.raven_out_video:SetLoop(false)
            self.raven_out_video:PlayVideo()
            self.raven_idle_video:StopVideo()
            
            MakeSoundEntity('01a_raven_out'):PlaySound(false)
            MakeSoundEntity('01a_apply_hook'):PlaySound(false)
	        
	        self.mouseBlocker:Push()
	        self.timer:Add( function()
                                self.anim_bg:PlayAnimation('hook')
                                
                                local effector = CreateAnimationEffector()
                                effector:SetDestroysOnEnd(true)
                                effector:AttachTo(self.anim_bg:GetAnimation('boat'))
                                effector:SetKey(_TRACK_X, 0, 0)
                                effector:SetKey(_TRACK_X, 1, 0)
                                effector:SetKey(_TRACK_X, 2, pos.x)
                                effector:SetKey(_TRACK_Y, 0, 0)
                                effector:SetKey(_TRACK_Y, 1, 0)
                                effector:SetKey(_TRACK_Y, 2, pos.y)
                                effector:Play()
                                
                                self.timer:Add( function()
                                                    self.mouseBlocker:Pop()
                                                    ObjectVisible('area_zoom_bag',    true)
                                                    ObjectVisible('area_click_chest', true)
                                                end,
                                                self.anim_bg:GetAnimation('hook'):GetDuration())
                            end,
                            self.raven_out_video:GetDuration() * 0.5)
	    end
	end
	-------------------------
	
	local function on_click_chest(e)
	    ObjectVisible(e.target, false)
	    
	    if e.save then
	        self.anim_bg:GetAnimation('boat'):GetElementByName('Layer_1'):SetOpacity(1)
	        ObjectVisible('area_link_mg', true)
	    else
	        local time = 0.5
	        self.anim_bg:GetAnimation('boat'):GetElementByName('Layer_1'):ChangeOpacity(0, 1, time)
	        MakeSoundEntity('01a_open_chest'):PlaySound(false)
	        
	        self.timer:Add( function()
	                            ObjectVisible('area_link_mg', true)
	                        end,
	                        time)
	    end
	end
	-------------------------
	
	RegisterToolApplyInfo('01_boathook', 'area_apply_boathook', on_apply_boathook, 'BBT_01A_NEED_APPLY_HOOK')
	RegisterActiveObject('area_click_chest', on_click_chest)
	
	-- zoom zones ----------------------------------
	
	-- zoom bag
	do
	    CreateSmartZoomZone('bag')
	    ---------------------
	    
	    if not IsToolApplied('01_shell') then
	        self.puppy_sound_idle = MakeSoundEntity('01a_puppy_in_bag_idle')
	        self.puppy_sound_idle:SetVolume(0.2)
	        self.puppy_sound_idle:PlaySound(true)
	    elseif not IsToolFound('01_puppy_dog') then
	        self.puppy_sound_idle = MakeSoundEntity('01a_puppy_idle')
	        self.puppy_sound_idle:SetVolume(0.2)
	        self.puppy_sound_idle:PlaySound(true)
	    end
	    
	    self.zoom_bag.on_open = function()
	        if self.puppy_sound_idle then
	            self.puppy_sound_idle:SetVolume(1)
	        end
	        
	        if not IsToolApplied('01_shell') then
	            if not self.z_bag_video_idle_1 then
                    self.z_bag_video_idle_1 = CreateVideoEntity('data/Video/Scenes/01a_dock_boat/01a_zoom_bag_idle1.ogg', '', false)
                    self.z_bag_video_idle_1:AttachTo(Object('z_bag_bg'))
                    self.z_bag_video_idle_1:SetZ(10)
                    self.z_bag_video_idle_1:SetXY(0, 0)
                    self.z_bag_video_idle_1:SetLoop(true)
                    self.z_bag_video_idle_1:PlayVideo()
                end
            elseif not IsToolFound('01_puppy_dog') and not self.z_bag_video_shell then
                if not self.z_bag_video_puppy then
                    self.z_bag_video_puppy = CreateVideoEntity('data/Video/Scenes/01a_dock_boat/01a_zoom_bag_puppy.ogg', '', false)
                    self.z_bag_video_puppy:AttachTo(Object('z_bag_bg'))
                    self.z_bag_video_puppy:SetZ(10)
                    self.z_bag_video_puppy:SetXY(0, 0)
                    self.z_bag_video_puppy:SetLoop(true)
                    self.z_bag_video_puppy:PlayVideo()
                end
	        end
	    end
	    ---------------------
	    
	    self.zoom_bag.on_close = function()
	        if self.puppy_sound_idle then
	            self.puppy_sound_idle:SetVolume(0.2)
	        end
	    end
	    ---------------------
	    
	    ObjectVisible('area_find_puppy_dog',        false)
	    ObjectVisible('area_find_mg_03_01_item_01', false)
	    
	    ObjectOpacity('z_bag_gekon', 0)
	    ---------------------
	    
	    local function on_apply_shell(e)
	        ObjectVisible(e.target, false)
	        
	        self.anim_bg:GetAnimation('boat'):GetElementByName('Layer_6'):SetOpacity(1)
	        self.anim_bg:GetAnimation('boat'):GetElementByName('Layer_7'):SetOpacity(0)
	        
	        if e.save then
	            --FindTool('01_rope')
	            ObjectVisible('area_find_puppy_dog', true)
	        else
	            do
                    self.z_bag_video_idle_1:StopVideo()
                    self.z_bag_video_idle_1 = nil
                    
                    self.z_bag_video_shell = CreateVideoEntity('data/Video/Scenes/01a_dock_boat/01a_zoom_bag_shell.ogg', '', false)
                    self.z_bag_video_shell:AttachTo(Object('z_bag_bg'))
                    self.z_bag_video_shell:SetZ(10)
                    self.z_bag_video_shell:SetXY(0, 0)
                    self.z_bag_video_shell:SetLoop(false)
                    self.z_bag_video_shell:PlayVideo()
                    
                    MakeSoundEntity('01a_apply_shell'):PlaySound(false)
                    
                    local fade_time = 0.2
                    
                    self.shell_action = self.timer:Add( function()
                                                            self.shell_action = nil
                                                            
                                                            self.z_bag_video_puppy = CreateVideoEntity('data/Video/Scenes/01a_dock_boat/01a_zoom_bag_puppy.ogg', '', false)
                                                            self.z_bag_video_puppy:AttachTo(Object('z_bag_bg'))
                                                            self.z_bag_video_puppy:SetZ(10)
                                                            self.z_bag_video_puppy:SetXY(0, 0)
                                                            self.z_bag_video_puppy:SetLoop(true)
                                                            self.z_bag_video_puppy:PlayVideo()
                                                            
                                                            --self.z_bag_video_shell:StopVideo()
                                                            self.z_bag_video_shell = nil
                                                        end,
                                                        self.z_bag_video_shell:GetDuration() - fade_time)
                end
	            
	            self.puppy_sound_idle:StopSound()
	            
	            self.mouseBlocker:Push()
	            self.timer:Add( function()
	                                self.mouseBlocker:Pop()
	                                self.puppy_sound_idle = MakeSoundEntity('01a_puppy_idle')
	                                self.puppy_sound_idle:PlaySound(true)
	                                
	                                --FindTool('01_rope')
	                                ObjectVisible('area_find_puppy_dog', true)
	                            end,
	                            2)
	        end
	    end
	    ---------------------
	    
	    local function on_find_puppy_dog(e)
	        self.anim_bg:GetAnimation('boat'):GetElementByName('Layer_6'):SetOpacity(0)
	        self.anim_bg:GetAnimation('boat'):GetElementByName('Layer_8'):SetOpacity(1)
	        self.anim_bg:GetAnimation('boat'):GetElementByName('Layer_9'):SetOpacity(1)
	        
	        if e.save then
	        else
                if self.shell_action then
                    self.shell_action:Kill()
                    self.shell_action = nil
                    
                    self.z_bag_video_shell:StopVideo()
                    self.z_bag_video_shell = nil
                else
                    self.z_bag_video_puppy:StopVideo()
                    self.z_bag_video_puppy = nil
                end
                
                self.puppy_sound_idle:StopSound()
                self.puppy_sound_idle = nil
            end
	        
	        ObjectOpacity('z_bag_gekon', 1)
	        ObjectVisible('area_find_mg_03_01_item_01', true)
	    end
	    ---------------------
	    
	    local function on_find_mg_03_01_item_01(e)
	        ObjectVisible(e.target, false)
	        
	        self.anim_bg:GetAnimation('boat'):GetElementByName('Layer_8'):SetOpacity(0)
	        ObjectOpacity('z_bag_gekon', 0)
	    end
	    ---------------------
	    
	    RegisterToolApplyInfo('01_shell', 'area_apply_zb_shell', on_apply_shell, 'BBT_01A_NEED_APPLY_SHELL')
	    RegisterToolFindInfo('01_puppy_dog', 'area_find_puppy_dog', on_find_puppy_dog, false)
	    RegisterToolFindInfo('MG_03_01_item_01', 'area_find_mg_03_01_item_01', on_find_mg_03_01_item_01)
	end
	
	if self.visited_first_time then
	    ShowBlackBar('BBT_01A_FIRST_VISIT')
	end
	
	self:check_interactive_help_tips()
end

function l_01a_dock_boat:check_interactive_help_tips()
    if IsToolFound('01_boathook') and not IsToolApplied('01_boathook') then
        local delay = self.visited_first_time and 2.5 or nil
        InteractiveHelp.ShowTip('TOOL_APPLY', delay)
    end
end

function l_01a_dock_boat:OnUpdate(dt)
end

function l_01a_dock_boat:OnAllItemsFound()
end

function l_01a_dock_boat:OnObjectMouseEnter(object)
end

function l_01a_dock_boat:OnObjectMouseLeave(object)
end

function l_01a_dock_boat:OnScreenObjectMouseDown(object, key)
end

function l_01a_dock_boat:OnObjectMouseDownProcessed(is_wrong_click)
end

function l_01a_dock_boat:OnMouseMove(previous_pos, current_pos)
end

function l_01a_dock_boat:OnObjectMouseUp(object)
end

function l_01a_dock_boat:OnObjectFound(object)
end

function l_01a_dock_boat:OnToolFound(tool)
end

function l_01a_dock_boat:OnToolApply(tool, object)
end

function l_01a_dock_boat:OnToolAddToInventory(tool)
end

function l_01a_dock_boat:OnObjectReachPanel(object)
end

function l_01a_dock_boat:OnButtonPress(title)
end

function l_01a_dock_boat:OnSetTool(tool)
end

function l_01a_dock_boat:OnLoaded()
end

function l_01a_dock_boat:OnDestroy()
end

--------------------------------------------------------------------------------
-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function l_01a_dock_boat:LoadData(screen)
	local anim
	anim = CreateAnimation('data/Texture/Scenes/01a_dock_boat/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/Scenes/01a_dock_boat/main/layers/layer_stairs.png',0,0,1024,480,1,0)
	anim:SetHotSpot(512,240)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('layer_stairs',anim,512,528,80,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,101,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_apply_boathook',anim,511,185,100,0,8,3)
	
	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_zoom_bag',anim,406,252,100,-30,1.4,1.73)
	
	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,809,253,100,-17,1.2,1.8)
	
	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_click_chest',anim,809,253,100,-17,1.2,1.8)
	
end
-- CLEAR_ON_SAVE_END
