--------------------------------------------------------------------------------
--  File     : Trunk
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
time_effects_begin = 40 / 30
time_effects_end = 50 / 30
frame_end = 346
--------------------------------------------------------------------------------
function OnSerialize(system)
	proc = system:Serialize(proc)
end
--------------------------------------------------------------------------------
function OnFirstEntry()
	inventory:CreateAndAddItem(room:GetItem("MATCHES"))
	inventory:CreateAndAddItem(room:GetItem("KNIFE"))
	music:Stop(false)
	music:PlayLoopWithDelay(10, "horror_7")
	ambient:Play("Trunk")
	room.sfx:Play("intrunk")				-- я в коробке!
	room.sfx:PlayEx ("breath_fast", true, 1, 0)		--звук "дышит часто"
	
	--вызов функции запуска пазла из кода
	room:JiggleBreathStart("breath_fast", 1)
		
	comment:Add("DLG_Awake_10_Player")
	
	proc = 0
	room:GetItem("PLANK1"):Disable ()
	room:GetItem("PLANK2"):Disable ()
	room:GetItem("KNIFE_BOARD"):Disable ()
	
	stars:Show("NAIL2", "4")
	stars:Show("PLANK2", "4")
	stars:Show("M_LOCK_PLANK", "4")
	stars:Show("WIRE", "4")
	stars:Show("WIRE_NAIL1", "4")
	stars:Show("WIRE_NAIL2", "4")
end
--------------------------------------------------------------------------------
function OnSetItemOnCursor (InventoryItem)
	if cursor:IsItemNameEqual("MATCHES") then				
		if (proc == 0) or (proc == 2) then					--зажечь спичку
			inventory:AddItemFromCursor()
			
			stars:Show("LID_CLOSED", "4")
			stars:Show("NAIL1", "4")
			stars:Show("PLANK1", "4")
			stars:Show("BAG_CLOSED", "4")
			stars:Show("M_BOARD", "4")
			stars:Show("M_MATCH", "4")
			stars:Show("CANDLE", "4")
			stars:Show("KNIFE_BOARD", "4")
							
			--запустить фоновые эффекты
			room:GetItem("FOREGROUND_EFFECTS"):Show()
			--запустить видео горящей спички
			local match_video = room:GetItem("MATCH_VIDEO")
			match_video:Show()
			match_video:PlayAnimation()
			local match_flame_video = room:GetItem("MATCH_FLAME_VIDEO")
			match_flame_video:Show()
			match_flame_video:PlayAnimation()
			--проявить свет вокруг спички
			local light_center = room:GetItem("LIGHT_CENTER")
			light_center:Show()
			local color_light1 = light_center:GetColor()
			local color_light2 = light_center:GetColor()
			color_light1:SetNormalAlpha(1)
			color_light2:SetNormalAlpha(0)
			light_center:SetColor(color_light2)
			light_center.effects:PlayColor(color_light1, time_effects_begin)
			--проявить тень от ножа
			if room:GetItem("KNIFE_BOARD"):IsVisible() then
				local shadow = room:GetItem("KNIFE_BOARD_SHADOW")
				shadow:Show()
				local color_shadow1 = shadow:GetColor()
				local color_shadow2 = shadow:GetColor()
				color_shadow1:SetNormalAlpha(1)
				color_shadow2:SetNormalAlpha(0)
				shadow:SetColor(color_shadow2)
				shadow.effects:PlayColor(color_shadow1, time_effects_begin)
			end
			--убрать темноту		
			local dark = room:GetItem("PITCH_BLACK")
			dark:Show()
			local color_dark1 = dark:GetColor()
			local color_dark2 = dark:GetColor()
			color_dark1:SetNormalAlpha(0)
			color_dark2:SetNormalAlpha(1)
			dark:SetColor(color_dark2)
			dark.effects:PlayColor(color_dark1, time_effects_begin)
			--убрать лучи
			local beams = room:GetItem("TRUNK_BEAMS")
			beams:Show()
			local color_beams1 = beams:GetColor()
			local color_beams2 = beams:GetColor()
			color_beams1:SetNormalAlpha(0)
			color_beams2:SetNormalAlpha(1)
			beams:SetColor(color_beams2)
			beams.effects:PlayColor(color_beams1, time_effects_begin)
						
			room.sfx:Play ("match")	--звук "зажигаем спичку"
			
			--если зажгли спичку первый раз
			if proc == 0 then	
				room:GetItem("HAND"):Show()
				room:GetItem("HAND_MATCH"):Show()
				
				comment:Add("DLG_Light_10_Player")
				music:Stop(true)
				music:PlayLoopWithDelay(10, "Coffin_Trip")		
				room.sfx:Stop ("breath_fast")	
				room.sfx:PlayEx ("breath_slow", true, 1, 0)		--звук "дышит реже"
				
				--вызов функции запуска пазла из кода
				room:JiggleBreathStart("breath_slow", 2)
				room:JiggleHandStart()
			end
			
			proc = 1
			
		elseif proc == 1 then								--пока горит спичка, спички в инвентаре не трогать
			inventory:AddItemFromCursor()
			comment:Add("CMT_Matches_Player")
		end
	end
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
	
	if not(room:GetItem("MATCH_VIDEO"):IsVisible()) and	--если темно
	   not(room:GetItem("CANDLE_KNIFE"):IsVisible()) then
		comment:Add("CMT_Dark_Player")
	
	else
		if itemName == "BAG_CLOSED" then						--открыть сумку
			item:Hide()
			room:GetItem("BAG_OPENED"):Show()
			room:GetItem("CANDLE"):Show()
			room:GetItem("N_NAKL_CANDLE"):Show()
			room.sfx:Play ("open_bag")	--звук "открываем сумку"
			return true
	
		elseif itemName == "CANDLE" then						--свеча
			pick_panel:AddItems(itemName, false)
			room:GetItem("N_NAKL_CANDLE"):Hide()
			return true
		
		elseif itemName == "M_BOARD" then						--боковая стенка
			comment:Add("CMT_Board_Player")
			return true
		
		elseif (itemName == "KNIFE_BOARD") and					--кинжал
			not(item:IsEnabled()) then	
			comment:Add("CMT_Knife_Player")
			return true
		
		elseif itemName == "LID_CLOSED" then					--крышка
			if not(room:GetItem("PLANK1_VIDEO"):IsAnimPlaying()) or
			   not(room:GetItem("PLANK1_VIDEO"):IsVisible()) then
				if not(room:GetItem("CANDLE_KNIFE"):IsVisible()) then
					comment:Add("CMT_Lid1_Player")
				else
					room:GetItem("LID_CLOSED"):Hide()
					room:GetItem("LID_OPENED"):Show()
					room:GetItem("M_LOCK_PLANK"):Show()
					room:GetItem("LOCK_PLANK"):Show()
					room:GetItem("N_LOCK_PLANK"):Show()
					room:GetItem("SKY"):Show()
					if room:GetItem("PLANK1"):IsVisible() then
						room:GetItem("PLANK1"):Hide()
						room:GetItem("PLANK2"):Show()
					end
					if room:GetItem("NAIL1"):IsVisible() then
						room:GetItem("NAIL1"):Hide()
						room:GetItem("N_NAIL1"):Hide()
						room:GetItem("NAIL2"):Show()
						room:GetItem("N_NAIL2"):Show()
					end
					if room:GetItem("WIRE_NAIL1"):IsVisible() then
						room:GetItem("WIRE_NAIL1"):Hide()
						room:GetItem("WIRE_NAIL2"):Show()
					end
			
					comment:Add("CMT_Lid2_Player")
					room.sfx:Play ("preopen")	--звук "приоткрывается гроб"
					--вызов функции запуска пазла из кода
					room:JiggleSkyStart()
				end
				return true
			end
	
		elseif itemName == "WIRE" then							--проволочка
			pick_panel:AddItems(itemName, false)
			return true
	
		elseif (itemName == "NAIL1") or							--гвоздь
			   (itemName == "NAIL2") then
			comment:Add("CMT_Nail_Player")
			return true
	
		elseif (itemName == "WIRE_NAIL1") or					--проволочка на гвозде
			   (itemName == "WIRE_NAIL2") then
			item:Hide()
			room:GetItem("NAIL1"):Hide()
			room:GetItem("N_NAIL1"):Hide()
			room:GetItem("NAIL2"):Hide()
			room:GetItem("N_NAIL2"):Hide()
			
			local name_video = "NAIL_OUT2"
			if itemName == "WIRE_NAIL1" then
				name_video = "NAIL_OUT1"
			end
			local nail_out = room:GetItem(name_video)
			nail_out:Show()
			nail_out:PlayAnimation()
			room:AddCtrlAnimation(name_video, -1)
			
			cstore:SetKey("wire_nail_trunk", true)
			room.sfx:Play ("get_nail")	--звук "проволокой вытаскиваем гвоздь"
			return true
		
		elseif (itemName == "PLANK1") or						--планка
			   (itemName == "PLANK2") then
			if item:IsEnabled() then
				item:Hide()
			
				local name_video = "PLANK2_VIDEO"
				if itemName == "PLANK1" then
					name_video = "PLANK1_VIDEO"
				end
				local plank_video = room:GetItem(name_video)
				plank_video:Show()
				plank_video:PlayAnimation()
				room:AddCtrlAnimation(name_video, -1)
			
				cstore:SetKey("plank_trunk", true)
				room.sfx:Play ("get_plank")	--звук "отрываем планку"
			else
				comment:Add("CMT_Plank_Player")
			end
			return true
		
		elseif itemName == "M_LOCK_PLANK" then					--щеколда
			comment:Add("CMT_Hook_Player")
			return true
		end
	end
end	
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)
	local itemName = item:GetName()
	
	if not(room:GetItem("MATCH_VIDEO"):IsVisible()) and	--если темно
	   not(room:GetItem("CANDLE_KNIFE"):IsVisible()) then
		comment:Add("CMT_Dark_Player")
	
	else
		if cursor:IsItemNameEqual("CANDLE") then				
			
			if itemName == "M_MATCH" then							--свеча на спичку
				if room:GetItem("KNIFE_BOARD"):IsVisible() then
					cursor:RemoveItem()
					item:Hide()
					
					room:GetItem("KNIFE_BOARD"):Enable()
					stars:Hide("KNIFE_BOARD")
			
					room:GetItem("CANDLE_KNIFE"):Show()
					room:GetItem("HAND"):Hide()
					room:GetItem("HAND_MATCH"):Hide()
					room:GetItem("MATCH_FLAME_VIDEO"):Hide()
					room:GetItem("MATCH_VIDEO"):Hide()
					room:GetItem("LIGHT_CENTER"):Hide()
					room:GetItem("PITCH_BLACK"):Hide()
					room:GetItem("HAND_LYING"):Show()
					room:GetItem("WIRE"):Show()
					
					room:GetItem("LID_CLOSED"):Disable()
			
					--запустить частицы свечки, стоящей на ноже	
					local candle_knife_particles = room:GetItem("CANDLE_KNIFE_PARTICLES")
					candle_knife_particles:Show()
					candle_knife_particles:PlayAnimation()
			
					comment:AddSilent("CMT_Light_Player")
					room.sfx:Play ("candle_put")	--звук "свеча ставится на кинжал"
					room:JiggleHandStop()
					proc = 3
				else
					inventory:AddItemFromCursor()
					comment:Add("CMT_Candle1_Player")
				end
				return true
			end
			
			if itemName == "KNIFE_BOARD" then							--свеча на кинжал
				comment:Add("CMT_Candle2_Player")
				return true
			end
		end

		if cursor:IsItemNameEqual("KNIFE") then					--кинжал на боковую стенку
			if itemName == "M_BOARD" then
				cursor:RemoveItem()
				item:Hide()
				room:GetItem("KNIFE_BOARD"):Show()
				room:GetItem("KNIFE_BOARD_SHADOW"):Show()
				cstore:SetKey("knife_board", true)
				room.sfx:Play ("knife_in_wall")	--звук "втыкаем кинжал в стенку гроба"
				return true
			end
		end

		if cursor:IsItemNameEqual("WIRE") then					--проволочка на гвоздь
			if itemName == "NAIL1" then
				cursor:RemoveItem()
				room:GetItem("WIRE_NAIL1"):Show()
				stars:Hide("NAIL1")
				cstore:SetKey("nail_trunk", true)
				room.sfx:Play ("wired")	--звук "проволоку наматываем на гвоздь"
				return true
			end
		
			if itemName == "NAIL2" then			
				cursor:RemoveItem()
				room:GetItem("WIRE_NAIL2"):Show()
				stars:Hide("NAIL2")
				cstore:SetKey("nail_trunk", true)
				room.sfx:Play ("wired")	--звук "проволоку наматываем на гвоздь"
				return true
			end
		end
		
		if cursor:IsItemNameEqual("PLANK1") then				--планка на щеколду
			if itemName == "M_LOCK_PLANK" then
				item:Hide()
				room:GetItem("LOCK_PLANK"):Hide()
				room:GetItem("N_LOCK_PLANK"):Hide()
				cursor:RemoveItem()
				room.sfx:Play ("break_cover")	--звук "планкой выламываем крышку"
				
				local lock_hack_video = room:GetItem("LOCK_HACK_VIDEO")
				lock_hack_video:Show()
				lock_hack_video:PlayAnimation()
				room:AddCtrlAnimation("LOCK_HACK_VIDEO", -1)
				return true
			end
		end
	end
end
--------------------------------------------------------------------------------
function OnUpdate (elapsedTime)
	if (proc == 1) or (proc == 2) then							--погасить спичку
		local match_video = room:GetItem("MATCH_VIDEO")
		local match_flame_video = room:GetItem("MATCH_FLAME_VIDEO")
		local light_center = room:GetItem("LIGHT_CENTER")
		local dark = room:GetItem("PITCH_BLACK")
		local light = room:GetItem("TRUNK_BEAMS")
		
		stars:SetAlpha( 1.0 - dark:GetColor():GetNormalAlpha() )
		
		if match_video:IsVisible() then
			if match_video:IsAnimPlaying() then
				
				--закончить исчезновение темноты и лучей (конец стадии разгорания)
				if not(dark.effects:IsPlayingColor ()) and dark:IsVisible() and
				   (proc == 1) then
					dark:Hide()
					light:Hide()
				end
			
				--начать проявление темноты и лучей (начало стадии затухания)
				local local_frame = room:GetVideoFrame(match_video:GetName())
				if (local_frame >= frame_end) and not(dark:IsVisible()) then
					local color = light_center:GetColor()
					color:SetNormalAlpha(0)
					light_center.effects:PlayColor(color, time_effects_end)
					
					if room:GetItem("KNIFE_BOARD"):IsVisible() then
						local shadow = room:GetItem("KNIFE_BOARD_SHADOW")
						local color_shadow = shadow:GetColor()
						color_shadow:SetNormalAlpha(0)
						shadow.effects:PlayColor(color_shadow, time_effects_end)
					end
					
					dark:Show()
					local color1 = dark:GetColor()
					local color2 = dark:GetColor()
					color1:SetNormalAlpha(1)
					color2:SetNormalAlpha(0)
					dark:SetColor(color2)
					dark.effects:PlayColor(color1, time_effects_end)
					
					light:Show()
					local color3 = light:GetColor()
					local color4 = light:GetColor()
					color3:SetNormalAlpha(1)
					color4:SetNormalAlpha(0)
					light:SetColor(color4)
					light.effects:PlayColor(color3, time_effects_end)
					
					proc = 2
				end
				
			else
				stars:Hide("LID_CLOSED")
				stars:Hide("NAIL1")
				stars:Hide("PLANK1")
				stars:Hide("BAG_CLOSED")
				stars:Hide("CANDLE")
				stars:Hide("KNIFE_BOARD")
				stars:Hide("M_BOARD")
				stars:Hide("M_MATCH")
							
				--скрыть фоновые эффекты
				room:GetItem("FOREGROUND_EFFECTS"):Hide()
				--срыть видео горящей спички
				match_video:Hide()
				match_flame_video:Hide()
				--убрать свет вокруг спички
				light_center:Hide()
				if room:GetItem("KNIFE_BOARD"):IsVisible() then
					room:GetItem("KNIFE_BOARD_SHADOW"):Hide()
				end
			end
		end
	end 
end
--------------------------------------------------------------------------------
-- Окончание анимации вырывания планки
function OnAnimationComplete_PLANK1_VIDEO(forced)
	room:GetItem("PLANK1_VIDEO"):Hide()
	EndOffPlankVideo(forced)
end
function OnAnimationComplete_PLANK2_VIDEO(forced)
	room:GetItem("PLANK2_VIDEO"):Hide()
	EndOffPlankVideo(forced)
end
--------------------------------------------------------------------------------
-- Окончание анимации вытаскивания гвоздя
function OnAnimationComplete_NAIL_OUT1(forced)
	room:GetItem("NAIL_OUT1"):Hide()
	room:GetItem("PLANK1"):Enable()
	room:GetItem("PLANK2"):Enable()
end
function OnAnimationComplete_NAIL_OUT2(forced)
	room:GetItem("NAIL_OUT2"):Hide()
	room:GetItem("PLANK2"):Enable()
end
--------------------------------------------------------------------------------
-- Окончание анимации выбивания щеколды
function OnAnimationComplete_LOCK_HACK_VIDEO(forced)
	room:GetItem("LOCK_HACK_VIDEO"):Hide()
	
	if forced == true then
		room:GetItem("M_LOCK_PLANK"):Show()
		room:GetItem("LOCK_PLANK"):Show()
		room:GetItem("N_LOCK_PLANK"):Show()
		inventory:CreateAndAddItem(room:GetItem("PLANK1"))
	else
		if inventory:IsItemExisting("MATCHES") then
			inventory:EraseOneItemFromCell("MATCHES")
		end
		if cursor:IsItemExisting() then
			cursor:RemoveItem()
		end
		cstore:SetKey("ToGate", true)
		GotoGate()
	end
end
--------------------------------------------------------------------------------
function EndOffPlankVideo(forced)
	if forced == true then
		inventory:CreateAndAddItem(room:GetItem("PLANK1"))
	else
		pick_panel:AddItems("PLANK1", false)
	end
end
--------------------------------------------------------------------------------
function GotoGate()
	chapter:ExitRoom()
	
	ambient:Play("dam")
	music:Stop(true)
	music:PlayLoopWithDelayEx(0, "m_cutscene_01", 1.0, false)

	IntroText = ui:GetDialog("IntroText")
	-- Настройки ( идентификатор строки с группой, время проявления, время затухания, 
	-- имя локации в которую перейдем после закрытия диалога )
	IntroText:SetOptions("Main:GATE_CUT_SCENE", 0.5, 0.5, "Gate")
	IntroText:PlaySound("snd_gate_cut_scene")
	IntroText:Show()
end
--------------------------------------------------------------------------------
function CheatSkip()
	GotoGate()
end
--------------------------------------------------------------------------------