--------------------------------------------------------------------------------
--  File     : Wizard's_tower_int_Tank
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
lock_time = 1.0
open_time = 0.5
--------------------------------------------------------------------------------
function OnSerialize(system)
	key_effect = system:Serialize(key_effect)
	
	OnEndOffKeyEffect()
end
--------------------------------------------------------------------------------
function OnFirstEntry()
	--inventory:CheatLoadAndAddItem ("RAVEN_KEY", "Smithy_Workplace", 0)
	
	key_effect = 0
	
	room:GetItem("RAVEN"):Disable()
	cstore:SetKey("Close_up_Tank", 0)
end
--------------------------------------------------------------------------------
function OnMouseClickOnBack()
	chapter:ExitCloseup()
	return true
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
	
	if itemName == "M_LOCK" then						--замок
		comment:Add("CMT_Lock_Player")
		return true
	
	elseif itemName == "WAX_MOLD" then					--восковой слепок
		room:GetItem("M_LOCK"):Show()
		pick_panel:AddItems(itemName, false)
		return true
		
	elseif (itemName == "RAVEN") and					--череп ворона
			(item:IsEnabled()) then
		pick_panel:AddItems("RAVEN_SKULL", true)
		cstore:SetKey("Close_up_Tank", 2)
		room:MarkCompleted()
		chapter:ExitCloseup ()
		return true
	end
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)					
	local itemName = item:GetName()
	
	if itemName == "M_LOCK" then						
		if cursor:IsItemNameEqual("WAX") then			--воск на замок	
			cursor:RemoveItem()
			item:Hide()
			room:GetItem("WAX_MOLD"):Show()
			room.sfx:Play ("make_the_sample")	--звук "делаем слепок из воска"
			return true
		end
		
		if cursor:IsItemNameEqual("RAVEN_KEY") then		--ключ на замок 
			cursor:RemoveItem()
			item:Hide()
			
			room:GetItem("LOCK_CIRCLE"):Hide()
			
			local key = room:GetItem("LOCK_CIRCLE_KEY")
			key:Show()
			key.effects:PlayRotate(-90, lock_time)
						
			local lock_bar_closed = room:GetItem("LOC_BAR_CLOSED")
			local color1 = lock_bar_closed:GetColor()
			color1:SetNormalAlpha(0)
			lock_bar_closed.effects:PlayColor(color1, lock_time)
			
			local lock_bar_opened = room:GetItem("LOC_BAR_OPENED")
			lock_bar_opened:Show()
			local color1 = lock_bar_opened:GetColor()
			local color2 = lock_bar_opened:GetColor()
			color2:SetNormalAlpha(0)
			lock_bar_opened:SetColor(color2)
			lock_bar_opened.effects:PlayColor(color1, lock_time)
			
			key_effect = 1
			room.sfx:Play ("use_key")	--звук "применяем ключ, жидкость уходит, дверца открывается"
			return true
		end
	end
end
--------------------------------------------------------------------------------
function OnUpdate(elapsedTime)
	
	local key = room:GetItem("LOCK_CIRCLE_KEY")
	if (key_effect == 1) and not(key.effects:IsPlayingRotate()) then 
		room:GetItem("LOC_BAR_CLOSED"):Hide()
		
		--вызов функции из кода
		room:SinkStart()
		key_effect = 2
	end
	
	local door_opened = room:GetItem("DOOR_OPENED")
	 if (key_effect == 3) and not(room:GetItem("DOOR_OPENED").effects:IsPlayingColor()) then
	   HideItems()
	end
end
--------------------------------------------------------------------------------
function OnExit()
	OnEndOffKeyEffect()
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function SinkEnd()
	room:GetItem("RAVEN"):Enable()
	room:GetItem("LIQUID_MAIN"):Hide()
	room:GetItem("LIQUID_N1"):Hide()
	room:GetItem("LIQUID_N2"):Hide()
	
	room:GetItem("M_LOCK"):Hide()
	HideFade("DOOR_CLOSED", open_time)
	HideFade("DOOR_FIX", open_time)
	HideFade("LOC_BAR_OPENED", open_time)
	HideFade("LOCK_BASE", open_time)
	HideFade("LOCK_CIRCLE_KEY", open_time)
	HideFade("LOCK_SLOTS", open_time)
	ShowFade("DOOR_OPENED", open_time)
	cstore:SetKey("Close_up_Tank", 1)
	
	key_effect = 3
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function HideFade(itemName, timeFade)
	local item = room:GetItem(itemName)
	
	local color = item:GetColor()
	color:SetNormalAlpha(0)
	item.effects:PlayColor(color, timeFade)
end
--------------------------------------------------------------------------------
function ShowFade(itemName, timeFade)
	local item = room:GetItem(itemName)
	item:Show()
	
	local color1 = item:GetColor()
	local color2 = item:GetColor()
	color2:SetNormalAlpha(0)
	item:SetColor(color2)
	item.effects:PlayColor(color1, timeFade)
end
--------------------------------------------------------------------------------
function HideItems()
	room:GetItem("DOOR_CLOSED"):Hide()
	room:GetItem("DOOR_FIX"):Hide()
	room:GetItem("LOC_BAR_OPENED"):Hide()
	room:GetItem("LOCK_BASE"):Hide()
	room:GetItem("LOCK_CIRCLE_KEY"):Hide()
	room:GetItem("LOCK_SLOTS"):Hide()
	
	local door_opened = room:GetItem("DOOR_OPENED")
	if door_opened.effects:IsPlayingColor() then
		door_opened.effects:StopColor()
		local color = door_opened:GetColor()
		color:SetNormalAlpha(1)
		door_opened:SetColor(color)
	end
	
	key_effect = 4
end
--------------------------------------------------------------------------------
function OnEndOffKeyEffect()
	if key_effect == 1 then			--выход во время поворота замка
		local lock_circle_key = room:GetItem("LOCK_CIRCLE_KEY")
		lock_circle_key.effects:StopRotate()
		local ang1 = 90 - lock_circle_key:GetRotateAngle()
		lock_circle_key:SetRotateAngle(ang1)
		
		local lock_bar_opened = room:GetItem("LOC_BAR_OPENED")
		local color = lock_bar_opened:GetColor()
		color:SetNormalAlpha(1)
		lock_bar_opened:SetColor(color)
		
	elseif key_effect == 2 then		--выход во время сливания жидкости
		--вызов функции из кода
		room:SinkExit()
		HideItems()
	
	elseif key_effect == 3 then		--выход во время исчезновения двери
		room:GetItem("DOOR_CLOSED").effects:StopColor()
		room:GetItem("DOOR_FIX").effects:StopColor()
		room:GetItem("LOC_BAR_OPENED").effects:StopColor()
		room:GetItem("LOCK_BASE").effects:StopColor()
		room:GetItem("LOCK_CIRCLE_KEY").effects:StopColor()
		HideItems()
	end
end
