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

LOC_1FLOOR				= "Hut_1st_floor"
LOC_2FLOOR				= "Hut_2nd_floor"
LOC_2FLOOR_CAGE			= "Hut_2nd_floor_Cage"
LOC_2FLOOR_TELESCOPE	= "Hut_2nd_floor_Telescope"

M_LADDER				= "M_LADDER"					--переход на 1-й этаж
M_CAGE					= "M_CAGE"						--переход в close-up с клеткой
M_TELESCOPE				= "M_TELESCOPE"					--переход в close-up с телескопом
M_SOLDIER_FACE			= "M_SOLDIER_FACE"				--лицо солдата
M_MEDALLION				= "M_MEDALLION"					--маска на медальон

I_HAMMER				= "HAMMER"						--молоток
I_KERCHIEF				= "KERCHIEF"					--платок
I_PUZZLE_PIECE			= "PUZZLE_PIECE"				--кусок пазла
I_HAND1					= "HAND1"						--рука солдата
I_HAND2					= "HAND2"
I_POTION				= "POTION"						--зелье
I_MEDALLION				= "MEDALLION"					--медальон
I_WIRE					= "WIRE"						--проволока 
I_CAGE_WIRE				= "CAGE_WIRE"
I_CAGE_DOOR_CLOSED		= "CAGE_DOOR_CLOSED"			--закрытая дверь клетки
I_CAGE_DOOR_OPENED		= "CAGE_DOOR_OPENED"			--открытая дверь клетки
I_CAGE_MIRROR			= "CAGE_MIRROR"					--зеркало
I_TEL_PART2				= "TEL_PART2"					--части телескопа
I_TEL_PART2_LYING		= "TEL_PART2_LYING"
I_TEL_PART1				= "TEL_PART1"


--------------------------------------------------------------------------------
KerchiefSpeed = 85
TimeHand = 0.7
frame_potion_part = 27
handAngle = -20
--------------------------------------------------------------------------------
function OnSerialize(system)
	Kerchief = system:Serialize(Kerchief)
	KerchiefDt = system:Serialize(KerchiefDt)
	KerchiefPos = system:Serialize(KerchiefPos)
	hand = system:Serialize(hand)
	handPos = system:Serialize(handPos)
	usePotion = system:Serialize(usePotion)
	
	ExitKerchief()
	ExitHand()
end
--------------------------------------------------------------------------------
function OnFirstEntry()
	room:GetItem(I_KERCHIEF):Disable ()
	room:GetItem(I_PUZZLE_PIECE):Disable ()
	
	local sceneSize = room:GetNoZoomSize()
	KerchiefPos = ctypes:Vector2(math.floor(250/2048 * sceneSize.x), math.floor(1230/1536 * sceneSize.y))
	Kerchief = 0
	KerchiefDt = 0
	hand = 0	
	handPos = ctypes:Vector2(math.floor(53/2048 * sceneSize.x), math.floor(247/1536 * sceneSize.y))
	usePotion = 0
	
	stars:Show(M_LADDER, "3")
	stars:Show(M_TELESCOPE, "2")
	stars:Show(M_CAGE, "2")
	stars:Show(M_MEDALLION, "4")
	stars:Show(M_SOLDIER_FACE, "4")
	stars:Show(I_HAMMER, "4")
	stars:Show(I_KERCHIEF, "4")
	stars:Show(I_PUZZLE_PIECE, "4")
end
--------------------------------------------------------------------------------
function OnEntry()
	local lamp = cstore:GetKey("Close_up_Lantern")
	
	if (lamp ~= 2) and (lamp ~= 3) then
		music:PlayLoopWithDelay(15, "Mystic_Forest") 
	end
	
	ambient:Play("Hut_2nd_floor")
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
	
	if itemName == M_LADDER then
		chapter:JumpRoom(LOC_1FLOOR)
		return true

	elseif itemName == M_CAGE  then
		chapter:JumpCloseup(LOC_2FLOOR_CAGE)						--переход в close-up с клеткой
		return true
	elseif itemName == M_TELESCOPE  then
		chapter:JumpCloseup(LOC_2FLOOR_TELESCOPE)					--переход в close-up с телескопом
		return true	
	end
	
	
	if itemName == M_SOLDIER_FACE  then				--лицо солдата
		if (cstore:GetKey("Soldier1") == 0) and (cstore:GetKey("Soldier2") == 0) then
			comment:Add("SOLDIER1")
			cstore:SetKey("Soldier2", 1)
			stars:Hide(M_SOLDIER_FACE)
		elseif (cstore:GetKey("Soldier2") == 0) and (cstore:GetKey("Soldier1") == 1) then
			comment:Add("SOLDIER2")
			cstore:SetKey("Soldier2", 1)
			stars:Hide(M_SOLDIER_FACE)
		else
			comment:Add("SOLDIER3")
		end
		stars:Hide(M_SOLDIER_FACE)
		return true	
	elseif itemName == I_KERCHIEF  then				--платок
		if not(item:IsEnabled()) then
			Kerchief = 1
			room:GetItem(M_MEDALLION):Show()
			room:GetItem(I_KERCHIEF):Enable()
			stars:Hide(I_KERCHIEF)
			room.sfx:Play ("move_rag")				--звук "отодвигаем тряпку"
			return true
		end
	elseif itemName == M_MEDALLION  then			--медальон
		comment:Add("CMT_Medal_Player")
		return true
	elseif itemName == I_HAND1  then				--рука солдата
		if hand == 0 then
			hand = 1
			--TODO если еще не кликали на руку, а зелье уже собрано?
			journal:Add("hand_potion")
			if inventory:IsItemExisting ("PLANT_ROOT") then
				journal:CompleteTask("hand_potion_plant")
			end
			if inventory:IsItemExisting ("WATER_POT") then
				journal:CompleteTask("hand_potion_pot")
			end
			comment:Add("DLG_Hand_10_Player")
		else
			comment:Add("CMT_Hand_Player")
		end
		return true
	elseif itemName == I_PUZZLE_PIECE  then			--кусок пазла
		if item:IsEnabled() then
			pick_panel:AddItems(I_PUZZLE_PIECE, false)
			cstore:SetKey("puzzle_piece", true)
			return true	
		end
	elseif itemName == I_HAMMER  then				---молоток
		pick_panel:AddItems(I_HAMMER, false)
		return true	
	end
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)					
	local itemName = item:GetName()

	if itemName == M_CAGE  then
		chapter:JumpCloseup(LOC_2FLOOR_CAGE)						--переход в close-up с клеткой
		return true
	elseif itemName == M_TELESCOPE  then
		chapter:JumpCloseup(LOC_2FLOOR_TELESCOPE)					--переход в close-up с телескопом
		return true	
	end
	
	
	if cursor:IsItemNameEqual(I_WIRE) then			--проволока на медальон 
		if itemName == M_MEDALLION then
			inventory:AddItemFromCursor()
			item:Hide()
			pick_panel:AddItems(I_MEDALLION, false)
			room.sfx:Play ("use_wire")				--звук "выколупываем амулет проволокой"
			
			local dialog = ui:GetDialog("PickItems")
			dialog:SetUserTag("pick_medalion")
		
			cstore:SetKey("Close_up_Lantern", 1)
			return true
		end
	end
	
	if cursor:IsItemNameEqual(I_POTION) then		--зелье на руку 
		if itemName == I_HAND1 then
			cursor:RemoveItem()
		
			local potion_hand = room:GetItem("POTION_HAND")
			potion_hand:Show()
			potion_hand:PlayAnimation()
			room:AddCtrlAnimation("POTION_HAND", -1)
			
			room.sfx:Play ("use_potion")	--звук "применили отвар из корня, полив им руку трупа"
			return true
		end
	end
end	
--------------------------------------------------------------------------------
function OnUpdate(elapsedTime)
	--передвинуть платок
	if Kerchief == 1 then
		local KerchiefItem = room:GetItem(I_KERCHIEF)
		local KerchiefPos = KerchiefItem:GetPosition()
		local dt = elapsedTime * KerchiefSpeed
		KerchiefPos.x = KerchiefPos.x + dt
		KerchiefPos.y = KerchiefPos.y + dt
		KerchiefItem:SetPosition(KerchiefPos)
		KerchiefDt = KerchiefDt + dt
		
		if KerchiefDt >= 100/2048 * (room:GetNoZoomSize()).x then
			Kerchief = 2
		end
	end
	
	--запустить дымок во время анимации поливания руки
	local potion_hand = room:GetItem("POTION_HAND")	
	local potion_hand_part = room:GetItem("POTION_HAND_PART")
	if (potion_hand:IsVisible()) and not(potion_hand_part:IsVisible()) then
		local local_frame = room:GetVideoFrame(potion_hand:GetName())
		if local_frame >= frame_potion_part then
			potion_hand_part:Show()
			potion_hand_part:PlayAnimation()
			room:AddControlForParticleSys("POTION_HAND_PART")
		end
	end
	
	--повернуть руку после анимации поливания руки
	local hand1 = room:GetItem(I_HAND1)	
	local hand2 = room:GetItem(I_HAND2)
	if not(hand1.effects:IsPlayingColor()) 
	   and (hand1:IsVisible()) and (hand2:IsVisible()) then
		hand1:Hide()
		room:GetItem(I_PUZZLE_PIECE):Enable ()
		hand2:SetRotCenter(handPos)
		hand2.effects:PlayRotate (handAngle, 0.6)
		usePotion = 2
	end
	
	if (usePotion == 2) and not(hand2.effects:IsPlayingRotate()) then
		usePotion = 3
	end
end
--------------------------------------------------------------------------------
function OnExit()
	ExitKerchief()
	ExitHand()
end
--------------------------------------------------------------------------------
-- Окончание анимации поливания руки зельем
function OnAnimationComplete_POTION_HAND(forced)
	room:GetItem("POTION_HAND"):Hide()
	
	local hand1 = room:GetItem(I_HAND1)	
	local hand2 = room:GetItem(I_HAND2)	
	
	if forced == true then
		room:GetItem("POTION_HAND_PART"):Hide()
			
		hand1:Hide()
		hand2:Show()
		hand2:SetRotCenter(handPos)
		hand2:SetRotateAngle(handAngle)
		
		room:GetItem(I_PUZZLE_PIECE):Enable ()
	else
		--стеклянная рука изчезает
		local color = hand1:GetColor()
		color:SetNormalAlpha(0)
		hand1.effects:PlayColor(color, TimeHand)
			
		--обычная рука появляется
		hand2:Show()
		local color1 = hand2:GetColor()
		local color2 = hand2:GetColor()
		color2:SetNormalAlpha(0)
		hand2:SetColor(color2)
		hand2.effects:PlayColor(color1, TimeHand)
		
		usePotion = 1
	end
end
--------------------------------------------------------------------------------
function OnDialogClose_PickItems(dialog)
	if dialog:GetUserTag() == "pick_medalion" then
		journal:Add("magic_lantern")
		comment:Add("DLG_Lant_10_Player")
		dialog:SetUserTag("")
	end
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Close_up_Cage(value)					--закрыть close-up с клеткой
	if value == 1 then
		room:GetItem(I_CAGE_WIRE):Hide()
	elseif value == 2 then
		room:GetItem(I_CAGE_DOOR_CLOSED):Hide()
		room:GetItem(I_CAGE_DOOR_OPENED):Show()
	elseif value == 3 then
		room:GetItem(I_CAGE_MIRROR):Hide()
		room:GetItem(M_CAGE):Hide()
	end
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Close_up_Telescope(value)				--закрыть close-up с телескопом
	if value == 1 then
		room:GetItem(I_TEL_PART2):Hide()
		room:GetItem(I_TEL_PART2_LYING):Show()
	elseif value == 2 then
		room:GetItem(M_TELESCOPE):Hide()
	end
end
--------------------------------------------------------------------------------
function CheatSkip () --!!!
	inventory:CheatLoadAndAddItem (I_PUZZLE_PIECE, LOC_2FLOOR, 0)
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Cheat_Hut2ndFloor(value)					--выполнить все в локации   !!!
	cstore:SetKey("Close_up_Cage", 1)
	cstore:SetKey("Close_up_Cage", 2)
	cstore:SetKey("Close_up_Cage", 3)
	cstore:SetKey("Close_up_Telescope", 1)
	cstore:SetKey("Close_up_Telescope", 2)
		
	room:GetItem(M_MEDALLION):Hide()
	room:GetItem(I_MEDALLION):Hide()
	room:GetItem(I_KERCHIEF):Enable()
	stars:Hide(I_KERCHIEF)
	
	room:GetItem(M_SOLDIER_FACE):Hide()
	room:GetItem(I_HAMMER):Hide()
	room:GetItem(I_PUZZLE_PIECE):Hide()
	room:GetItem(I_HAND1):Hide()
	room:GetItem(I_HAND2):Show()
end
--------------------------------------------------------------------------------
----------------передвинуть платок----------------------------------------------
function ExitKerchief()
	if Kerchief == 1 then
		Kerchief = 2
		room:GetItem(I_KERCHIEF):SetPosition(KerchiefPos)
	end
end
----------------передвинуть руку----------------------------------------------
function ExitHand()
	local hand1 = room:GetItem(I_HAND1)
	local hand2 = room:GetItem(I_HAND2)

	if usePotion == 1 then
		hand1.effects:StopColor()
		hand1:Hide()
		
		local color = hand2:GetColor()
		color:SetNormalAlpha(1)
		hand2:SetColor(color)
		
		hand2:SetRotCenter(handPos)
		
		room:GetItem(I_PUZZLE_PIECE):Enable ()
	end
	
	if usePotion == 2 then
		hand2.effects:StopRotate()
	end
	
	if (usePotion == 1) or (usePotion == 2) then
		hand2:SetRotateAngle(handAngle)
		usePotion = 3
	end
end