--------------------------------------------------------------------------------
--  File     : Bridge
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
time_effects_bridge = 0.5
--------------------------------------------------------------------------------
function OnSerialize(system)
	bridgeComment = system:Serialize(bridgeComment)
	visit_Village_street = system:Serialize(visit_Village_street)
	bridge_effect = system:Serialize(bridge_effect)
	
	EndOffBridgeEffect()
end
--------------------------------------------------------------------------------
function OnFirstEntry()
	--inventory:CheatLoadAndAddItem ("BRIDGE_POTION", "Witch's_house_ext_Magic_place", 0)
	
	room:GetItem("M_TO_MILL"):Disable ()
	bridgeComment = 0
	visit_Village_street = 0
	bridge_effect = 0
	
	if not(cstore:IsKeyExists ("Cheat_Bridge1")) then 		 --!!! (условие для читов)
		comment:Add("CMT_Bridge1_Player")
	end
	
	stars:Show("M_GARBAGE", "1")
	stars:Show("M_SENTRY_BOX", "2")
	stars:Show("M_BRIDGES", "4")
	
	room.sfx:PlayEx ("ghost_bridge", true, 1, 0)
end
--------------------------------------------------------------------------------
function OnEntry()
	music:PlayLoopWithDelay(10, "The_Mist") 
	ambient:Play("Bridge")
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
	
	if itemName == "M_TO_TAVERN" then							
		chapter:JumpRoom("Tavern_ext")
		return true
	
	elseif (itemName == "M_TO_MILL") and 						--переход в деревню
	       (room:GetItem("M_TO_MILL"):IsEnabled()) then									
		if visit_Village_street == 0 then
			visit_Village_street = 1
			chapter:ExitRoom()
			ambient:Play("dam")
			music:Stop(true)
			music:PlayLoopWithDelay(0, "horror_7")		
			IntroText = ui:GetDialog("IntroText")
			IntroText:SetOptions("Main:VILLAGE_CUT_SCENE", 0.5, 0.5, "Village_street");
			IntroText:Show()
		else
			chapter:JumpRoom("Village_street")
		end
		return true
			
	
	elseif itemName == "M_GARBAGE" then							--переход в hidden object с мусором
		chapter:JumpRoom("Bridge_Garbage_HO")
		return true
	
	elseif itemName == "M_SENTRY_BOX" then						--переход в close-up с охранной будкой
		chapter:JumpCloseup("Bridge_Senry_box")
		return true
		
	
	elseif itemName == "M_BRIDGES" then					--мост
		if bridgeComment == 0 then
			comment:Add("CMT_Bridges_Player") 
			bridgeComment = 1
		elseif bridgeComment == 1 then
			comment:Add("CMT_Tomill_Player")
		end
		return true
	end
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)	
	local itemName = item:GetName()
	
	if itemName == "M_SENTRY_BOX" then					--переход в close-up с охранной будкой
		chapter:JumpCloseup("Bridge_Senry_box")
		return true
	end
	
	if itemName == "M_BRIDGES" then						--зелье на мост
		if cursor:IsItemNameEqual("BRIDGE_POTION") then
			cursor:RemoveItem()
			item:Hide()
		
			local bridge_part = room:GetItem("BRIDGE_PART")
			local color = bridge_part:GetColor()
			color:SetNormalAlpha(0)
			bridge_part.effects:PlayColor(color, time_effects_bridge)
		
			local bridge = room:GetItem("BRIDGE")
			bridge:Show()
			local color1 = bridge:GetColor()
			local color2 = bridge:GetColor()
			color1:SetNormalAlpha(1)
			color2:SetNormalAlpha(0)
			bridge:SetColor(color2)
			bridge.effects:PlayColor(color1, time_effects_bridge)
		
			room:GetItem("BRIDGE_APPEAR"):Show()
			room:AddControlForParticleSys("BRIDGE_APPEAR")
		
			bridge_effect = 1
			room:GetItem("M_TO_MILL"):Enable ()
			stars:Show("M_TO_MILL", "3")
			comment:Add("CMT_Potion_Player") 
			room.sfx:Play ("pour_potion")		--звук "льем зелье ясновиденья на мост"
			return true
		end
	end
end
--------------------------------------------------------------------------------
function OnUpdate (elapsedTime)
	if (bridge_effect == 1) and not(room:GetItem("BRIDGE").effects:IsPlayingColor()) then
		bridge_effect = 2
		room:GetItem("BRIDGE_PART"):Hide()
		room.sfx:Stop("ghost_bridge")
	end
end
--------------------------------------------------------------------------------
function OnExit()
	EndOffBridgeEffect()
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Close_Bridge_Senry(value)	--выход из close-up охранной будкой
	room:GetItem("M_SENTRY_BOX"):Hide()
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Garbage_HO(value)			--закрыть hidden object с мусором
	if (value == 1) or (value == 3) then
		room:GetItem("M_GARBAGE"):Hide()
	elseif value == 2 then
		room:GetItem("M_GARBAGE"):Show()
	end
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Cheat_Bridge1(value)		--выполнить все в локации  !!!
	cstore:SetKey("Close_Bridge_Senry", true)
	if cstore:GetKey("Garbage_HO") ~= 2 then
		cstore:SetKey("Garbage_HO", 1)
	end
	room:GetItem("M_BRIDGES"):Hide()
	room:GetItem("BRIDGE_PART"):Hide()
	room:GetItem("BRIDGE"):Show()
	room:GetItem("M_TO_MILL"):Enable ()
	stars:Show("M_TO_MILL", "3")
	room.sfx:Stop("ghost_bridge")
	visit_Village_street = 1
end
function OnChapterStoreEvent_Cheat_Bridge2(value)		
	cstore:SetKey("Garbage_HO", 3)
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function EndOffBridgeEffect()
	if bridge_effect == 1 then
		bridge_effect = 2
		room:GetItem("BRIDGE_PART"):Hide()
		
		local bridge = room:GetItem("BRIDGE")
		bridge.effects:StopColor()
		local color = bridge:GetColor()
		color:SetNormalAlpha(1)
		bridge:SetColor(color)
		
		room.sfx:Stop("ghost_bridge")
	end
end