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

M_GOBACK				= "M_GOBACK"			--выход из локации
M_PIECE					= "M_PIECE_PLACE"		--маска на пазл

BLOCKER					= "BLOCKER_MAIN"		--механизм для ключа
BLOCKER_BACK			= "BLOCKER_BACK"
BLOCKER_ON				= "BLOCKER_B_ON"
BLOCKER_OFF				= "BLOCKER_B_OFF"		

I_PIECE					= "PUZZLE_PIECE"		--ключ
V_CABINET_KEY			= "CABINETMG_KEY_VIDEO"	--видео поворота ключа
--------------------------------------------------------------------------------
BlockTimeRotate = 0.25
BlockTimeFade = 0.3
BlockTimePause = 0.3
--------------------------------------------------------------------------------
function OnSerialize(system)
	Block = system:Serialize(Block)
	BlockTime = system:Serialize(BlockTime)
end
--------------------------------------------------------------------------------
function OnFirstEntry()
	Block = 0
	BlockTime = 0
	
	local Blocker = room:GetItem(BLOCKER)
	local pos1 = Blocker:GetPosition()
	pos1.x = pos1.x + Blocker:GetWidth() / 2
	pos1.y = pos1.y + Blocker:GetHeight() / 2
	
	local BlockerOff = room:GetItem(BLOCKER_OFF)
	local pos = BlockerOff:GetPosition()
	pos.x = pos1.x - pos.x
	pos.y = pos1.y - pos.y
	BlockerOff:SetRotCenter (pos)
	BlockerOff:SetRotateAngle (90)
	
	stars:Show(M_PIECE, "4")
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
	
	if itemName == M_GOBACK then
		chapter:JumpRoom(LOC_1FLOOR)
		return true
	end
	
	
	if itemName == M_PIECE then						--маска на пазле							
		comment:Add("CMT_Drawer1_Player")
		return true
	end
	if itemName == BLOCKER then
		comment:Add("CMT_Drawer2_Player")
		return true
	end
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)											
	local itemName = item:GetName()
	
	if cursor:IsItemNameEqual(I_PIECE) then  		--кусок пазла на головоломку
		if itemName == BLOCKER then
			cursor:RemoveItem()
			
			local cabinet_key = room:GetItem(V_CABINET_KEY)
			cabinet_key:Show()
			cabinet_key:PlayAnimation()
			room:AddCtrlAnimation(V_CABINET_KEY, -1)
			room.sfx:Play ("insert_key")			--звук "вставляем ключ"
			return true
		end
	end
end
--------------------------------------------------------------------------------
function OnUpdate(elapsedTime)
	local BlockerOff = room:GetItem(BLOCKER_OFF)
	local Blocker = room:GetItem(BLOCKER)
	local BlockerOn = room:GetItem(BLOCKER_ON)
	local BlockerBack = room:GetItem(BLOCKER_BACK)
	local cabinet_key = room:GetItem(V_CABINET_KEY)
	
	
	if cabinet_key:IsVisible() then											--запустить поворот
		if cabinet_key:IsAnimPlaying () then
			if Block == 0 then
				local frame = room:GetVideoFrame(cabinet_key:GetName())
				
				if frame >= 33 then
					BlockerOff.effects:PlayRotate (0, BlockTimeRotate)	
					Block = 1
				end
			end
		end
	end
	
	if (Block == 1) and (not(BlockerOff.effects:IsPlayingRotate())) then	--окончание поворота
		Block = 2
	end
	
	if (Block == 2) or (Block == 3) then									--пауза
		BlockTime = BlockTime + elapsedTime
		if BlockTime >= BlockTimePause then
			if Block == 2 then
				BlockerOff:Hide()
				BlockerOn:Show()
			end
			Block = Block + 1
			BlockTime = 0
		end
	end
	
	if Block == 4 then														--исчезание
		local color1 = Blocker:GetColor()
		color1:SetNormalAlpha(0)
		Blocker.effects:PlayColor(color1, BlockTimeFade)
		
		local color2 = BlockerOn:GetColor()
		color2:SetNormalAlpha(0)
		BlockerOn.effects:PlayColor(color2, BlockTimeFade)
		
		local color3 = BlockerBack:GetColor()
		color3:SetNormalAlpha(0)
		BlockerBack.effects:PlayColor(color3, BlockTimeFade)
		
		Block = 5
	end
	
	if (Block == 5) and (not(Blocker.effects:IsPlayingColor())) then
		Blocker:Hide()
		BlockerOn:Hide()
		BlockerBack:Hide()
		room:GetItem(M_PIECE):Hide()
		solve:Enable(true)
		Block = 6
	end
end
--------------------------------------------------------------------------------
-- Окончание анимации поворота ключа
function OnAnimationComplete_CABINETMG_KEY_VIDEO(forced)
	room:GetItem(V_CABINET_KEY):Hide()
	
	if forced == true then
		room:GetItem(BLOCKER_OFF):Hide()
		room:GetItem(BLOCKER):Hide()
		room:GetItem(BLOCKER_ON):Hide()
		room:GetItem(BLOCKER_BACK):Hide()
		room:GetItem(M_PIECE):Hide()
		
		Block = 6
		solve:Enable(true)
	end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function OnPuzzleWin()												--мини-игра окончена
	room.sfx:Play ("open_lock")				--звук "открыли замок (дверцу)"
	solve:Enable(false)
	cstore:SetKey("Close_up_Cabinet", 1)
	room:MarkCompleted()
	chapter:JumpCloseup(LOC_1FLOOR_CABINET)
end