--------------------------------------------------------------------------------
--  File     : Cemetery
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
function OnSerialize(system)
	time_hole_dig = system:Serialize(time_hole_dig)
	time_hole_bury = system:Serialize(time_hole_bury)
	
	comment_ghost = system:Serialize(comment_ghost)
end
--------------------------------------------------------------------------------
function OnFirstEntry()
	time_hole_dig = room:GetVideoTime(room:GetItem("DIG_GRAVE"))
	time_hole_bury = room:GetVideoTime(room:GetItem("BURY_GRAVE"))
	
	comment_ghost = 0
	
	stars:Show("M_STATUE", "2")
	stars:Show("M_LABEL", "4")
	stars:Show("M_DIG_PLACE", "4")
end
--------------------------------------------------------------------------------
function OnEntry()
	music:PlayLoopWithDelay(5, "horror_2")
	ambient:Play("Cemetery")
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
	
	if itemName == "M_TO_CHAPEL" then							--переход к часовне
		chapter:JumpRoom("Chapel_ext")
		return true
		
	elseif itemName == "M_STATUE" then							--переход в close-up со статуей
		chapter:JumpCloseup("Cemetery_Statue")
		return true	

	elseif itemName == "M_DIG_PLACE" then			
		if not(room:GetItem("HOLE"):IsVisible()) then			--перекопанное место
			comment:Add("CMT_DigPlace_Player")					
		elseif not(room:GetItem("SKELETONS"):IsVisible()) then	--яма
			comment:Add("CMT_Hole_Player")						
		else													--яма со скелетами
			comment:Add("CMT_HoleSkeleton_Player")
		end
		return true	
		
	elseif itemName == "M_LABEL" then							--табличка на статуе
		comment:Add("CMT_Label_Player")	
		return true	
	end
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)					
	local itemName = item:GetName()
	
	if itemName == "M_STATUE" then								--переход в close-up со статуей
		chapter:JumpCloseup("Cemetery_Statue")
		return true	
	end
	
	if itemName == "M_DIG_PLACE" then					
		if cursor:IsItemNameEqual("SPADE_Q") then	
			local hole = room:GetItem("HOLE")
			
			if not(hole:IsVisible()) then						--раскопать лопатой землю
				inventory.alt:AddItemFromCursor()
				
				local dig_grave = room:GetItem("DIG_GRAVE")
				dig_grave:Show()
				dig_grave:PlayAnimation()
				room:AddCtrlAnimation("DIG_GRAVE", -1)
				
				ShowFade(hole, time_hole_dig)
								
				room.sfx:Play ("spade")
				return true
			
			elseif room:GetItem("SKELETONS"):IsVisible() then	--закопать лопатой скелеты
				inventory.alt:AddItemFromCursor()
				item:Hide()
				
				local bury_grave = room:GetItem("BURY_GRAVE")
				bury_grave:Show()
				bury_grave:PlayAnimation()
				room:AddCtrlAnimation("BURY_GRAVE", -1)
				
				HideFade(hole, time_hole_bury)
				HideFade(room:GetItem("SKELETONS"), time_hole_bury)
				ShowFade(room:GetItem("HOLE_BURIED"), time_hole_bury)
				room.sfx:Play ("spade")
				music:Stop(true)
				
				cstore:SetKey("Mill_Ghosts", true)
				return true
			end
		end
	
		if cursor:IsItemNameEqual("SKELETON") then				--положить скелеты в яму
			if  cursor:GetItem():IsTaskSetOfItemsCompleted () then
				if room:GetItem("HOLE"):IsVisible() then
					cursor:RemoveItem()
					room:GetItem("SKELETONS"):Show()
					room.sfx:Play ("bones_drop")		--звук "положить скелеты"
					return true
					
				else
					comment:Add("CMT_Skeleton2_Player")	
					return true				
				end
			end
		end
	end
end
--------------------------------------------------------------------------------
function OnUpdate(elapsedTime)
	if comment_ghost == 1 then
		comment:Add("CMT_Skeleton1_Player")	
		comment_ghost = 0
	end
end
--------------------------------------------------------------------------------
-- Окончание анимации раскапывания ямы
function OnAnimationComplete_DIG_GRAVE(forced)
	room:GetItem("DIG_GRAVE"):Hide()
	
	inventory:AddInventoryItem(inventory.alt:GetItem("SPADE_Q"))
	inventory.alt:EraseOneItemFromCell("SPADE_Q")
	
	if forced == true then
		local hole = room:GetItem("HOLE")
		hole.effects:StopColor()
		local color = hole:GetColor()
		color:SetNormalAlpha(1)
		hole:SetColor(color)
	end
end
--------------------------------------------------------------------------------
-- Окончание анимации закапывания ямы
function OnAnimationComplete_BURY_GRAVE(forced)
	room:GetItem("BURY_GRAVE"):Hide()
	room:GetItem("HOLE"):Hide()
	room:GetItem("SKELETONS"):Hide()
	
	inventory:AddInventoryItem(inventory.alt:GetItem("SPADE_Q"))
	inventory.alt:EraseOneItemFromCell("SPADE_Q")
	
	if forced == true then
		local hole_buried = room:GetItem("HOLE_BURIED")
		hole_buried.effects:StopColor()
		local color = hole_buried:GetColor()
		color:SetNormalAlpha(1)
		hole_buried:SetColor(color)
		
		comment_ghost = 1
		
	else
		local ghosts_rest = room:GetItem("GHOSTS_REST")
		ghosts_rest:Show()
		ghosts_rest:PlayAnimation()
		room:AddCtrlAnimation("GHOSTS_REST", -1)

		room.sfx:Play ("jingle_8_boom")			--звук бууум
		room.sfx:Play ("ghosts_disappear")		--звук "духи вылетают"
		music:PlayLoopWithDelay(15, "horror_3") 
	end
end
--------------------------------------------------------------------------------
-- Окончание анимации вылетания духов
function OnAnimationComplete_GHOSTS_REST(forced)
	room:GetItem("GHOSTS_REST"):Hide()
	
	if forced == true then
		comment_ghost = 1
	else	
		comment:Add("CMT_Skeleton1_Player")	
		room.sfx:Play ("jingle_3")
	end
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Close_up_Statue(value)				--закрыть close-up со статуей
	room:GetItem("M_STATUE"):Hide()
	room:GetItem("M_LABEL"):Hide()
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Cheat_Cemetery(value)				--выполнить все в локации  !!!
	cstore:SetKey("Close_up_Statue", true)
	room:GetItem("M_DIG_PLACE"):Hide()
	room:GetItem("HOLE_BURIED"):Show()
end
--------------------------------------------------------------------------------
-----------------Проявление по фэйду--------------------------------------------
function ShowFade(item, time_effect)
	item:Show()
	local color = item:GetColor()
	local color1 = item:GetColor()
	color:SetNormalAlpha(1)
	color1:SetNormalAlpha(0)
	item:SetColor(color1)
	item.effects:PlayColor(color, time_effect)
end
-----------------Исчезновение по фэйду--------------------------------------------
function HideFade(item, time_effect)
	local color = item:GetColor()
	color:SetNormalAlpha(0)
	item.effects:PlayColor(color, time_effect)
end
