--------------------------------------------------------------------------------
--  File     : Witch's_house_int_Scull_h
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
angle_jaw = -10
--------------------------------------------------------------------------------
function OnSerialize(system)
	skull = system:Serialize(skull)
	
	EndScullOpening()
end
--------------------------------------------------------------------------------
function OnFirstEntry()
	cstore:SetKey("Close_up_Scull_h", 0)
	
	room:GetItem("SCULL_OPENING"):SetAnimPause (true)
	local SceneSize = room:GetNoZoomSize()
	local jaw = room:GetItem("JAW")
	jaw:Disable()
	jaw:SetRotCenter(ctypes:Vector2(math.floor(SceneSize.x * 46/810), math.floor(SceneSize.y * 12/1080)))
	jaw:SetRotateAngle(angle_jaw)
	skull = 0

	stars:Show("M_SKULL", "4")
	stars:Show("WITCH_BOOK_KEY", "4")
end
--------------------------------------------------------------------------------
function OnMouseClickOnBack()
	chapter:ExitCloseup()
	return true
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
	
	if itemName == "M_SKULL" then						--череп
		comment:Add("CMT_Scull_Player")
		return true

	elseif (itemName == "JAW") and (skull == 0) then	--открытая челюсть
		item.effects:PlayRotate(0, 0.5)
		item:Enable()
		skull = 1
		room.sfx:Play ("push_jaw")	--звук "нажимаем снизу на челюсть, открывается люк )) у черепа сверху"
		return true
	
	elseif itemName == "WITCH_BOOK_KEY" then			--ключ от книги
		cstore:SetKey("Close_up_Scull_h", 2)
		pick_panel:AddItems(itemName, true)
		room:MarkCompleted()
		return true
	end
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)				
	local itemName = item:GetName()

	if cursor:IsItemNameEqual("SCULL_H_JAW") then	--челюсть на череп 
		if itemName == "M_SKULL" then
			item:Hide()
			cursor:RemoveItem()
			room:GetItem("JAW"):Show()
			room.sfx:Play ("install_jaw")			--звук "вставляем челюсть черепку"
			return true
		end
	end
end
--------------------------------------------------------------------------------
function OnUpdate (elapsedTime)
	local jaw = room:GetItem("JAW")
	
	if (skull == 1) and not(jaw.effects:IsPlayingRotate()) then
		skull = 2
		room:GetItem("WITCH_BOOK_KEY"):Show()
		local videoSkull = room:GetItem("SCULL_OPENING")
		videoSkull:PlayAnimation()
		room:AddCtrlAnimation("SCULL_OPENING", -1)
	end
	
end
--------------------------------------------------------------------------------
function OnExit()
	if room:GetItem("WITCH_BOOK_KEY"):IsVisible() then
		skull = 3
	end
	EndScullOpening()
end
--------------------------------------------------------------------------------
-- Окончание анимации открывания черепа
function OnAnimationComplete_SCULL_OPENING(forced)
	cstore:SetKey("Close_up_Scull_h", 1)
	
	if forced == true then
		room:VideoSeekToEnd(room:GetItem("SCULL_OPENING"))
	end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function EndScullOpening()
	if (skull == 1) or (skull == 3) then
		skull = 2
		room:GetItem("WITCH_BOOK_KEY"):Show()
		local jaw = room:GetItem("JAW")
		jaw.effects:StopRotate()
		jaw:SetRotateAngle(0)
		local skull_opening = room:GetItem("SCULL_OPENING")
		skull_opening:PlayAnimation()
		room:VideoSeekToEnd(skull_opening)
		cstore:SetKey("Close_up_Scull_h", 1)
	end
end

