--------------------------------------------------------------------------------
--  File     : Mill_ext_Cellar
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
function OnSerialize(system)
	posLatch = system:Serialize(posLatch)
	posHandle = system:Serialize(posHandle)
	open_door = system:Serialize(open_door)
	
	ExtrExit()
end
--------------------------------------------------------------------------------
function OnFirstEntry()
	open_door = 0
	
	cstore:SetKey("Close_Cellar", 0)
	
	room:GetItem("DOOR_CLOSED"):Disable()
	room:GetItem("HANDLE"):Disable()
	
	local sceneSize = room:GetNoZoomSize()
	local posX = 72/1440 * sceneSize.x 
	local posY = -10/1080 * sceneSize.y 
	posLatch = room:GetItem("LATCH"):GetPosition() + ctypes:Vector2(posX, posY)
	posHandle = room:GetItem("HANDLE"):GetPosition() + ctypes:Vector2(posX, posY)
		
	stars:Show("BAR_RUSTY", "4")
	stars:Show("HANDLE", "4")
	stars:Show("M_SALT", "4")
end
--------------------------------------------------------------------------------
function OnMouseClickOnBack()
	chapter:ExitCloseup()
	return true
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
	
	if itemName == "DOOR_CLOSED" then				--закрытая дверь погреба
		if not(item:IsEnabled()) then
			comment:Add("CMT_Door_Player")
		else
			room:GetItem("LATCH"):Hide()
			room:GetItem("HANDLE"):Hide()
			room:GetItem("BAR"):Hide()
			room:GetItem("DOOR_CLOSED"):Hide()
			room:GetItem("DOOR_OPEN1"):Show()
			room:GetItem("DOOR_OPEN2"):Show()
			room:GetItem("M_SALT"):Show()

			room.sfx:Play ("open_door")		--звук "открывается дверь"
			cstore:SetKey("Close_Cellar", 1)
		end
		return true
		
	elseif itemName == "BAR_RUSTY" then				--ржавый засов погреба
		comment:Add("CMT_Bar_Player")
		return true
		
	elseif itemName == "HANDLE" then				--засов погреба
		if item:IsEnabled() then
			item:Disable()
			
			local timeLock = 0.5
			room:GetItem("LATCH").effects:PlayMove(posLatch, timeLock)
			item.effects:PlayMove(posHandle, timeLock)
			open_door = 3
		
			room.sfx:Play ("open_cellar")		--звук "отодвигается засов"
			return true
		end
		
	elseif itemName == "M_SALT" then			--соль
		comment:Add("CMT_Solt_Player")
		return true
	end
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)					
	local itemName = item:GetName()

	if itemName == "BAR_RUSTY" then					--масленка на ржавый засов  
		if cursor:IsItemNameEqual("OIL_CAN") then			
			cursor:RemoveItem()
			
			local oil_lock = room:GetItem("OIL_LOCK")
			oil_lock:Show()
			oil_lock:PlayAnimation()
			room:AddCtrlAnimation("OIL_LOCK", -1)
			room.sfx:Play ("pour_oil")	--звук "льем масло на засов"
			return true
		end
	end
	
	if itemName == "M_SALT" then					--стакан на соль  
		if cursor:IsItemNameEqual("GLASS") then	
			cursor:RemoveItem()
			item:Hide()
			room:GetItem("GLASS_PRINT"):Show()
			
			pick_panel:AddItems("SALT_GLASS", true)
			room.sfx:Play ("get_salt")		--звук "набираем соль в стакан"
			cstore:SetKey("Close_Cellar", 2)
			cstore:SetKey("Tools_HO", 2)
			
			cstore:SetKey("Ingredient_salt", true)
			if cstore:IsKeyExists("Ingredient_flower") then
				comment:Add("FLOWER_SALT")
			end
			room:MarkCompleted()
			return true
		end
	end
end
--------------------------------------------------------------------------------
function OnUpdate (elapsedTime)
	
	local bur_rusty = room:GetItem("BAR_RUSTY")
	
	if not(bur_rusty.effects:IsPlayingColor ()) 
	   and (bur_rusty:IsVisible()) then
		
		local color_br = bur_rusty:GetColor()
		if color_br:GetNormalAlpha() == 0 then
			bur_rusty:Hide()
			room:GetItem("HANDLE"):Enable()
			open_door = 2
		end
	end
	
	if not(room:GetItem("LATCH").effects:IsPlayingMove()) and
	   not(room:GetItem("DOOR_CLOSED"):IsEnabled()) and
	   not(room:GetItem("HANDLE"):IsEnabled()) and
	   not(room:GetItem("BAR_RUSTY"):IsVisible()) then
		room:GetItem("DOOR_CLOSED"):Enable()
		open_door = 4
	end
	
end
--------------------------------------------------------------------------------
function OnExit()
	ExtrExit()
end
--------------------------------------------------------------------------------
function ExtrExit()
	local bur_rusty = room:GetItem("BAR_RUSTY")
	local handle = room:GetItem("HANDLE")
	
	if open_door == 1 then
	   bur_rusty:Hide()
	   handle:Enable()
	   open_door = 2
	end
	
	if  open_door == 3 then
		room:GetItem("LATCH"):SetPosition(posLatch)
		handle:SetPosition(posHandle)
		room:GetItem("DOOR_CLOSED"):Enable()
		open_door = 4
	end
end
--------------------------------------------------------------------------------
-- Окончание анимации поливания замка маслом
function OnAnimationComplete_OIL_LOCK(forced)
	room:GetItem("OIL_LOCK"):Hide()
	
	if forced == true then
		room:GetItem("BAR_RUSTY"):Hide()
		room:GetItem("HANDLE"):Enable()
	else
		local bur_rusty = room:GetItem("BAR_RUSTY")
		local color_br = bur_rusty:GetColor()
		color_br:SetNormalAlpha(0)
		bur_rusty.effects:PlayColor(color_br, 0.5)
		open_door = 1
	end
end
--------------------------------------------------------------------------------
