--------------------------------------------------------------------------------
--  File     : Hut_1st_floor_Chest
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
LOC_1FLOOR				= "Hut_1st_floor"

M_KEYHOLE				= "M_KEYHOLE"		--маска на замок
I_EYESTONE				= "EYESTONE"		--камень-глаз
I_COVER_CLOSED			= "COVER_CLOSED"	--крышка
I_CHEST_KEY				= "CHEST_KEY"		--часть замка
--------------------------------------------------------------------------------
pause_time = 0.5
effect_time = 0.5
--------------------------------------------------------------------------------
function OnSerialize(system)
	pause_time_dt = system:Serialize(pause_time_dt)
	open_chest = system:Serialize(open_chest)
	
	if (open_chest == 1) or (open_chest == 2) then
		HideCover()
	end
end
--------------------------------------------------------------------------------
function OnFirstEntry()
	open_chest = 0
	pause_time_dt = 0
	cstore:SetKey("Hut_1st_floor_Chest", 0)
	
	stars:Show(M_KEYHOLE, "4")
end
--------------------------------------------------------------------------------
function OnMouseClickOnBack()
	chapter:ExitCloseup()
	return true
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
		
	if itemName == I_EYESTONE then						--камень-глаз
		pick_panel:AddItems(itemName, true)
		cstore:SetKey("Hut_1st_floor_Chest", 2)
		room:MarkCompleted()
		return true
	end
	
	if itemName == M_KEYHOLE then						--замок
		comment:Add("CMT_Chest_Player")
		room.sfx:Play ("cant_open_chest")	--звук "пробуем открыть сундук - не открывается"
		return true
	end
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)
	local itemName = item:GetName()
					
	if cursor:IsItemNameEqual(I_CHEST_KEY) then			--часть замка на замок   
		if itemName == M_KEYHOLE then
			cursor:RemoveItem()
			item:Hide()
			room:GetItem(I_CHEST_KEY):Show()
			open_chest = 1
			room.sfx:Play ("open_chest")	--звук "открывается сундук ключом"
			return true
		end
	end
end
--------------------------------------------------------------------------------
function OnUpdate (elapsedTime)
	local cover = room:GetItem(I_COVER_CLOSED)
	local key = room:GetItem(I_CHEST_KEY)
	
	if open_chest == 1 then
		pause_time_dt = pause_time_dt + elapsedTime
		--закончить паузу
		if pause_time_dt >= pause_time then		
			open_chest = 2
			--запустить эффект исчезания крышки
			local color = cover:GetColor()
			color:SetNormalAlpha(0)
			cover.effects:PlayColor(color, effect_time)
			local color1 = key:GetColor()
			color1:SetNormalAlpha(0)
			key.effects:PlayColor(color1, effect_time)
		end
	end
	
	if not(cover.effects:IsPlayingColor()) and (open_chest == 2) then
		HideCover()
	end
	
end
--------------------------------------------------------------------------------
function OnExit()
	if (open_chest == 1) or (open_chest == 2) then
		HideCover()
	end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function HideCover()
	open_chest = 3
	room:GetItem(I_COVER_CLOSED):Hide()
	room:GetItem(I_CHEST_KEY):Hide()
	stars:Show(I_EYESTONE, "4")
	cstore:SetKey("Hut_1st_floor_Chest", 1)
end