--------------------------------------------------------------------------------
--  File     : Scary_place_exit
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
function OnSerialize(system)
	visit_Tavern_ext = system:Serialize(visit_Tavern_ext)
end
--------------------------------------------------------------------------------
function OnFirstEntry()
	--inventory:CheatLoadAndAddItem("OPEN_ROAD_POTION", "Witch's_house_ext_Magic_place", 0)	
	
	visit_Tavern_ext = 0
	
	room:GetItem("SLINGSHOT_CENTER"):Disable ()
	room:GetItem("STUBVIDEO"):SetAnimPause (true)
	
	stars:Show("M_HOLLOW", "1")
	stars:Show("M_SKELETON3", "2")
	stars:Show("M_TO_TAVERN", "3")
	stars:Show("EGG", "4")
	stars:Show("NEST", "4")
	stars:Show("SLINGSHOT_CENTER", "4")
	stars:Show("M_STUB", "4")
	
	--TODO убрать в финале
	if not(cstore:IsKeyExists("Cheat_ScaryGlade1")) then	--!!! (условие для читов)
		comment:Add("DLG_Stub_Player")
		journal:Add("potion1")
	end
end
--------------------------------------------------------------------------------
function OnEntry()
	music:PlayLoopWithDelay(20, "Scary_place") 
	ambient:Play("Scary_place_exit")
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
	
	if room:GetItem("M_SLINGSHOT_EXIT"):IsVisible() then
		if itemName == "M_SLINGSHOT_EXIT" then
			ToHideSlingshot()
			ToShowMasks()
			
			if inventory.alt:IsItemExisting ("ROGATKA") then
				inventory:AddInventoryItem(inventory.alt:GetItem ("ROGATKA")) 
				inventory.alt:EraseOneItemFromCell("ROGATKA")
			end
						
			if inventory.alt:IsItemExisting ("STONE") then
				inventory:AddInventoryItem(inventory.alt:GetItem ("STONE"))
				inventory.alt:EraseOneItemFromCell ("STONE")
			end
			
			room:GetItem("SLINGSHOT_CENTER"):Disable ()
			item:Hide()
			room:Stop()
		end
		
		if itemName == "SLINGSHOT_CENTER" then
			if not(room:GetItem("SLINGSHOT_STONE"):IsVisible()) then
				comment:Add("CMT_SlngshtComm_Player")
				return true
			end
		end
	
	else
		if itemName == "M_TO_HOUSE_EXT" then
			chapter:JumpRoom("Witch's_house_ext")
			return true
		
		elseif itemName == "M_TO_TAVERN" then					--переход к таверне
			if visit_Tavern_ext == 0 then
				visit_Tavern_ext = 1
				chapter:ExitRoom()
				music:Stop(true)
				music:PlayLoopWithDelayEx(0, "m_cutscene_03", 1.0, false)

				IntroText = ui:GetDialog("IntroText")
				IntroText:SetOptions("Main:TAVERN_CUT_SCENE", 0.5, 0.5, "Tavern_ext");
				IntroText:Show()
				ambient:Play("tavern_ext")
			else
				chapter:JumpRoom("Tavern_ext")
			end
			return true
	
		elseif itemName == "M_HOLLOW" then						--переход в hidden object с дуплом
			chapter:JumpRoom("Scary_place_exit_Hollow_HO")
			return true
		
		elseif itemName == "M_SKELETON3" then					--переход в close-up со скелетом
			chapter:JumpCloseup("Scary_place_exit_Skeleton")
			return true
		
		elseif itemName == "NEST" then				--гнездо
			comment:Add("CMT_NestComm_Player")
			return true
		
		elseif itemName == "M_STUB" then			--пень
			if not(cstore:GetKey("OpenRoadPotion")) then
				comment:Add("CMT_ExitComm1_Player")
			else
				comment:Add("CMT_ExitComm2_Player")
			end
			return true
		
		elseif itemName == "EGG" then				--яйцо
			pick_panel:AddItems(itemName, false)
			room:GetItem("NEST_DROP"):SetClickable(false)
			journal:CompleteTask("p1_egg")
			return true
		end
	end
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)
	local itemName = item:GetName()
	
	if not(room:GetItem("M_SLINGSHOT_EXIT"):IsVisible()) then
		if itemName == "M_SKELETON3" then						--переход в close-up со скелетом
			chapter:JumpCloseup("Scary_place_exit_Skeleton")
			return true
		end
	
		if itemName == "NEST" then
			if cursor:IsItemNameEqual("STONE") then			--камень на гнездо
				comment:Add("CMT_NestStone_Player")
				inventory:AddItemFromCursor()
				room.sfx:Play ("throw_fail")				--звук "пытаемся кинуть камнем в гнездо (неудачно)"
			end
		
			if cursor:IsItemNameEqual("ROGATKA") then		--рогатку на гнездо 
				inventory.alt:AddItemFromCursor()
			
				room:GetItem("SLINGSHOT"):Show()
				room:GetItem("SLINGSHOT_CENTER"):Show()
				room:GetItem("SLINGSHOT_LEFT"):Show()
				room:GetItem("SLINGSHOT_RIGHT"):Show()
				room:GetItem("N_SLINGSHOT1"):Show()
				room:GetItem("N_SLINGSHOT2"):Show()
			
				room:GetItem("M_HOLLOW"):Hide()
				room:GetItem("M_SKELETON3"):Hide()
				room:GetItem("M_TO_HOUSE_EXT"):Hide()
			
				room.sfx:Play ("get_slingshot")	--звук "достаем рогатку"
				
				room:GetItem("M_SLINGSHOT_EXIT"):Show()
			end
			return true
		end
		
		if itemName == "M_STUB" then						--зелье на пень  
			if cursor:IsItemNameEqual("OPEN_ROAD_POTION") then	
				item:Hide()
				cursor:RemoveItem()
				
				local stubvial = room:GetItem("STUBVIAL")
				stubvial:Show()
				stubvial:PlayAnimation()
				room:AddCtrlAnimation("STUBVIAL", -1)
				
				local stubvial_part = room:GetItem("STUBVIAL_PART")
				stubvial_part:Show()
				stubvial_part:PlayAnimation()
				room:AddControlForParticleSys("STUBVIAL_PART")

				music:Stop(true)
				room.sfx:Play ("way_is_free")			--звук "льем зелье на корягу и она, скрипя, поднимает корни"
				return true
			end
		end
	
	else
		if itemName == "SLINGSHOT_CENTER" then			--камень на рогатку
			if cursor:IsItemNameEqual("STONE") then	
				inventory.alt:AddItemFromCursor()
			
				room:GetItem("SLINGSHOT_STONE"):Show()
				room:GetItem("SLINGSHOT_CENTER"):Enable()
				room:GetItem("SLINGSHOT_LEFT"):Hide()
				room:GetItem("SLINGSHOT_RIGHT"):Hide()
				room:GetItem("SIGHT"):Show()
				
				room.sfx:Play ("load_slingshot")		--звук "кладем в рогатку камень"
			
				room:Start()
				return true
			end
		end
	end
end
--------------------------------------------------------------------------------
function OnUpdate (elapsedTime)
	local stubvial = room:GetItem("STUBVIAL")
	
	if stubvial:IsVisible() then
		local stub = room:GetItem("STUBVIDEO")
		if stub:IsAnimPaused() then
			local local_frame = room:GetVideoFrame(stubvial:GetName())
			
			if local_frame >= 90 then
				stub:PlayAnimation()
				room:AddCtrlAnimation("STUBVIDEO", -1)
			end
		end
	end
end
--------------------------------------------------------------------------------
-- Окончание анимации поливания пня зельем
function OnAnimationComplete_STUBVIAL(forced)
	room:GetItem("STUBVIAL"):Hide()
		
	if forced == true then
		room:GetItem("STUBVIDEO"):PlayAnimation()
		room:AddCtrlAnimation("STUBVIDEO", -1)
	end
end
--------------------------------------------------------------------------------
-- Окончание анимации поднятия ветвей пня
function OnAnimationComplete_STUBVIDEO(forced)
	room:GetItem("M_TO_TAVERN"):Show()
	
	room:GetItem("STUBVIDEO"):Hide()
	room:GetItem("STUB_OFF"):Show()
end
--------------------------------------------------------------------------------
-- Окончание анимации падения гнезда
function OnAnimationComplete_NESTVIDEO(forced)
	local nest_video = room:GetItem("NESTVIDEO")
	nest_video:Hide()
	room:GetItem("NEST_DROP"):Show()
	room:GetItem("EGG"):Show()
	room:GetItem("N_NAKL_EGG"):Show()
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Hollow_HO(value)				--закрыть hidden object с дуплом
	if (value == 1) or (value == 3) then
		room:GetItem("M_HOLLOW"):Hide()
	elseif value == 2 then
		room:GetItem("M_HOLLOW"):Show()
	end
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Close_up_Skeleton(value)		--закрыть close-up со скелетом
	if value == 1 then
		room:GetItem("SKELETON3_STONE"):Hide()
	elseif value == 2 then
		room:GetItem("SKELETON3"):Hide()
		room:GetItem("M_SKELETON3"):Hide()
	end
end
--------------------------------------------------------------------------------
function OnPuzzleWin(item)
	ToHideSlingshot()
		
	room:GetItem("SLINGSHOT_STONE"):Hide()
	room:GetItem("SIGHT"):Hide()
	room:GetItem("NEST"):Hide()
	local nest_video = room:GetItem("NESTVIDEO")
	nest_video:Show()
	nest_video:PlayAnimation()
	room:AddCtrlAnimation("NESTVIDEO", -1)
	
	ToShowMasks()
	
	if inventory.alt:IsItemExisting ("STONE") then
		inventory.alt:EraseOneItemFromCell ("STONE")
	end
	if inventory.alt:IsItemExisting ("ROGATKA") then
		inventory.alt:EraseOneItemFromCell ("ROGATKA")
	end
		
	room:GetItem("M_SLINGSHOT_EXIT"):Hide()
end
--------------------------------------------------------------------------------
function OnNotPuzzleWin(item)
	if inventory.alt:IsItemExisting ("STONE") then
		inventory:AddInventoryItem(inventory.alt:GetItem ("STONE"))
		inventory.alt:EraseOneItemFromCell ("STONE")
	end
	
	room:GetItem("SLINGSHOT_CENTER"):Disable ()
	room:GetItem("SLINGSHOT_STONE"):Hide()
	room:GetItem("SLINGSHOT_LEFT"):Show()
	room:GetItem("SLINGSHOT_RIGHT"):Show()
	room:GetItem("SIGHT"):Hide()
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function ToShowMasks()
	if (cstore:GetKey("Hollow_HO") == 0) or (cstore:GetKey("Hollow_HO") == 2) then
		room:GetItem("M_HOLLOW"):Show()
	end
	room:GetItem("M_SKELETON3"):Show()
	room:GetItem("M_TO_HOUSE_EXT"):Show()
end
--------------------------------------------------------------------------------
function ToHideSlingshot()
	room:GetItem("SLINGSHOT"):Hide()
	room:GetItem("SLINGSHOT_CENTER"):Hide()
	room:GetItem("SLINGSHOT_LEFT"):Hide()
	room:GetItem("SLINGSHOT_RIGHT"):Hide()
	room:GetItem("N_SLINGSHOT1"):Hide()
	room:GetItem("N_SLINGSHOT2"):Hide()
	room:GetItem("SLINGSHOT_STONE"):Hide()
	room:GetItem("SIGHT"):Hide()
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Cheat_ScaryPlaceExit1(value)						--выполнить все в локации  !!!
	if cstore:GetKey("Hollow_HO") ~= 2 then
		cstore:SetKey("Hollow_HO", 1)
	end
	room:GetItem("M_STUB"):Hide()
	room:GetItem("STUBVIDEO"):Hide()
	room:GetItem("STUB_OFF"):Show()
	room:GetItem("NEST"):Hide()
	room:GetItem("NEST_DROP"):Show()
	room:GetItem("EGG"):Hide()
	room:GetItem("M_TO_TAVERN"):Show()
	visit_Tavern_ext = 1
end
function OnChapterStoreEvent_Cheat_ScaryPlaceExit2(value)
	cstore:SetKey("Hollow_HO", 3)
end
function OnChapterStoreEvent_Cheat_ScaryPlaceExit3(value)
	room:GetItem("M_SKELETON3"):Hide()
	room:GetItem("SKELETON3"):Hide()
	room:GetItem("SKELETON3_STONE"):Hide()
end