--------------------------------------------------------------------------------
--  File     : Witch's_house_ext_Flytrap
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
dofile("gamedata:_Chapters/Main/Helper/PickSkeletons.lua")
--------------------------------------------------------------------------------
chance_init_hand = 4			--шанс появления хватающей лапы мухоловки - 1 к chance
chance_init_head = 4			--шанс появления клацающей челюсти мухоловки - 1 к chance
-------------------------------------------------------------------------------
function OnSerialize(system)
	flytrap = system:Serialize(flytrap)
	chance_hand = system:Serialize(chance_hand)
	chance_dt_hand = system:Serialize(chance_dt_hand)
	chance_head = system:Serialize(chance_head)
	chance_dt_head = system:Serialize(chance_dt_head)
	time_small_bine = system:Serialize(time_small_bine)
	
	runesOnSkeleton = system:Serialize(runesOnSkeleton)
	pos_runes_init = system:Serialize(pos_runes_init)
	pos_runes_light_init = system:Serialize(pos_runes_light_init)
	pos_runes_dt = system:Serialize(pos_runes_dt)
	right = system:Serialize(right)
	use_rune = system:Serialize(use_rune)
	name_cur_music = system:Serialize(name_cur_music)
	
	OnSerializeSkeletons(system)
end
--------------------------------------------------------------------------------
function OnFirstEntry()
	flytrap = 0
	chance_hand = 0
	chance_dt_hand = 0
	chance_head = 0
	chance_dt_head = 0
	time_small_bine = room:GetVideoTime(room:GetItem("FLYTRAP_BINES_OPEN"))
	
	right = true
	runesOnSkeleton = 0
	use_rune = 0
	name_cur_music = "Scary_place"
	pos_runes_init = room:GetItem("RUNE_FIRE"):GetPosition()
	pos_runes_light_init = room:GetItem("RUNE_FIRE_LIGHT"):GetPosition()
	pos_runes_dt = room:GetItem("RUNE_AIR"):GetPosition().x - pos_runes_init.x
	
	comment:Add("CMT_Flytrap_Player")
	
	cstore:SetKey("Flytrap_seen", 1)
	cstore:SetKey("Close_up_Flytrap", 0)
	room:GetItem("FLYTRAP_POISON2"):Disable()
	room:GetItem("FLYTRAP_BINES_OPEN"):SetAnimPause (true)
	
	stars:Show("M_BINES", "4")
	stars:Show("M_POISON", "4")
	stars:Show("SKELETON", "4")
	stars:Show("KEY", "4")
end
--------------------------------------------------------------------------------
function OnEntry()
	room.sfx:PlayEx ("plant_loop", true, 1, 0)	--звук "мухоловка шевелится"
end
--------------------------------------------------------------------------------
function OnMouseClickOnBack()
	chapter:ExitCloseup()
	return true
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()

	if itemName == "M_BINES" then						--мухоловка
		if flytrap == 0 then
			comment:Add("CMT_Bines1_Player")
		elseif flytrap == 2 then
			comment:Add("CMT_Bines2_Player")
		end
		return true
	
	elseif (itemName == "FLYTRAP_HEAD_IDLE1") or		-- челюсти мухоловки
		   (itemName == "FLYTRAP_HEAD_IDLE2") then
		comment:Add("CMT_Jaws_Player")
		FlytrapAttack()
		return true
	
	elseif itemName == "M_POISON" then					-- место для надреза и пузырька
		if not(room:GetItem("FLYTRAP_POISON1"):IsVisible()) then
			comment:Add("CMT_Cut_Player")
		else
			comment:Add("CMT_Vial_Player")
		end
		return true
	
	elseif itemName == "KEY" then						-- ключ от домика ведьмы
		pick_panel:AddItems(itemName, false)
		return true
	
	elseif itemName == "FLYTRAP_POISON2" then			-- пузырек с ядом мухоловки
		item:Hide()
		room:GetItem("FLYTRAP_PUT_VIAL"):Hide()
		pick_panel:AddItems("FLYTRAP_POISON", false)
		journal:CompleteTask("p1_poison")
		return true
	
	elseif itemName == "SKELETON2_STONE" then			--скелет в камне
		comment:Add("SKELETON_COMM1")
		return true
	
	elseif itemName == "SKELETON" then					--скелет
		pick_panel:AddItemsSet(itemName, 3, true)
		PickSkeletons()
		cstore:SetKey("Close_up_Flytrap", 3)
		room:MarkCompleted()
		return true	
	end
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)						
	local itemName = item:GetName()

	if (itemName == "FLYTRAP_HEAD_IDLE1") or			--лоскут с мухами на челюсть мухоловки
	   (itemName == "FLYTRAP_HEAD_IDLE2") then
		if cursor:IsItemNameEqual("RAG_INSECT") then		
			cursor:RemoveItem()
			item:Hide()
			flytrap = 1
			--пасть ест тряпку
			local head_rug_attack = room:GetItem("FLYTRAP_HEAD_RUG_ATTACK")
			head_rug_attack:Show()
			head_rug_attack:PlayAnimation()
			room:AddCtrlAnimation("FLYTRAP_HEAD_RUG_ATTACK", -1)
			cstore:SetKey("Close_up_Flytrap", 1)
			room.sfx:Play ("chattering")	--звук "растение ест тряпку"
			return true
			
		elseif cursor:IsItemNameEqual("SPADE_Q") then
			inventory:AddItemFromCursor()
			FlytrapAttack()
			return false
		end	
	end
		
	if itemName == "M_BINES" then						--лопату на плети
		if flytrap >= 1 then
			if cursor:IsItemNameEqual("SPADE_Q") then
				inventory:AddItemFromCursor()
				item:Hide()
				room:GetItem("FLYTRAP_BINES_OPEN"):SetAnimPause(false)
				room:AddCtrlAnimation("FLYTRAP_BINES_OPEN", -1)
			
				local bines_small = room:GetItem("BINES_SMALL")
				local color = bines_small:GetColor()
				color:SetNormalAlpha(0)
				bines_small.effects:PlayColor(color, time_small_bine)
				room.sfx:Play ("remove_plant_thorn")	--звук "отодвигаем колючки"
				return true
			end
			
		else
			inventory:AddItemFromCursor()
			FlytrapAttack()
			return false		
		end
	end
	
	
	if itemName == "M_POISON" then						
		local videoKnife = room:GetItem("FLYTRAP_KNIFE")	--нож на мухоловку
		if not(videoKnife:IsVisible()) and
		   not(room:GetItem("FLYTRAP_POISON1"):IsVisible()) then
			if cursor:IsItemNameEqual("KNIFE_Q") then		
				inventory.alt:AddItemFromCursor()
				videoKnife:Show()
				videoKnife:PlayAnimation()
				room:AddCtrlAnimation("FLYTRAP_KNIFE", -1)
				room.sfx:Play ("cut_plant")	--звук "надрезаем растение"
				return true
			end
		end
			
		if cursor:IsItemNameEqual("VIAL") then				--пузырек на место для пузырька
			cursor:RemoveItem()
			local videoVial = room:GetItem("FLYTRAP_PUT_VIAL")
			videoVial:Show()
			videoVial:PlayAnimation()
			room:AddCtrlAnimation("FLYTRAP_PUT_VIAL", -1)
			return true
		end
	end
	
	if itemName == "SKELETON2_STONE" then					--руны на скелет
		if (cursor:IsItemNameEqual("RUNE_FIRE")) or 
		   (cursor:IsItemNameEqual("RUNE_EARTH")) or 
		   (cursor:IsItemNameEqual("RUNE_WATER")) or 
		   (cursor:IsItemNameEqual("RUNE_AIR")) then
			RunesOnSkeletons(itemName)
			return true
		end
	end

end
--------------------------------------------------------------------------------
function OnExit()
	ExitSkeletons()
end
--------------------------------------------------------------------------------
function OnUpdate (elapsedTime)

	UpdateSkeleton(elapsedTime, "SKELETON2_STONE")

	--мухоловка еще не съела тряпку
	if flytrap == 0 then
		--пасть
		local head_idle1 = room:GetItem("FLYTRAP_HEAD_IDLE1")
		local head_idle2 = room:GetItem("FLYTRAP_HEAD_IDLE2")
		local head_attack = room:GetItem("FLYTRAP_HEAD_ATTACK")
		
		if chance_head ~= chance_dt_head then
			if (head_idle1:IsVisible()) and not(head_idle1:IsAnimPlaying ()) then
				head_idle1:PlayAnimation()
				chance_dt_head = chance_dt_head + 1
			end
		else
			if (head_idle1:IsVisible()) and not(head_idle1:IsAnimPlaying ()) then
				head_idle1:Hide()
				head_idle2:Show()
				head_idle2:PlayAnimation()
			end
		end
			
		if (head_idle2:IsVisible()) and not(head_idle2:IsAnimPlaying ()) then
			head_idle2:Hide()
			head_idle1:Show()
			head_idle1:PlayAnimation()
			
			chance_head = math.random(chance_init_head)
			chance_dt_head = 0
		end
		
		if (head_attack:IsVisible()) and not(head_attack:IsAnimPlaying ()) then
			head_attack:Hide()
			head_idle1:Show()
			head_idle1:PlayAnimation()
		end
	end
	
	--рука
	local hand_idle = room:GetItem("FLYTRAP_HAND_IDLE")
	local hand_attack = room:GetItem("FLYTRAP_HAND_ATTACK")
	if chance_hand ~= chance_dt_hand then
		if (hand_idle:IsVisible()) and not(hand_idle:IsAnimPlaying ()) then
			hand_idle:PlayAnimation()
			chance_dt_hand = chance_dt_hand + 1
		end
	else
		if (hand_idle:IsVisible()) and not(hand_idle:IsAnimPlaying ()) then
			hand_idle:Hide()
			hand_attack:Show()
			hand_attack:PlayAnimation()
		end
	end

	if (hand_attack:IsVisible()) and not(hand_attack:IsAnimPlaying ()) then
		hand_attack:Hide()
		hand_idle:Show()
		hand_idle:PlayAnimation()
		
		chance_hand = math.random(chance_init_hand)
		chance_dt_hand = 0
	end
end
--------------------------------------------------------------------------------
-- Окончание анимации хватания тряпки
function OnAnimationComplete_FLYTRAP_HEAD_RUG_ATTACK(forced)
	room:GetItem("FLYTRAP_HEAD_RUG_ATTACK"):Hide()
	local head_rug_idle = room:GetItem("FLYTRAP_HEAD_RUG_IDLE")
	head_rug_idle:Show()
	head_rug_idle:PlayAnimation()
	flytrap = 2
	
--	if forced == false then
--		room.sfx:Play ("jingle_4")	--звук джингла
--	end
end
--------------------------------------------------------------------------------
-- Окончание анимации раздвигания плетей
function OnAnimationComplete_FLYTRAP_BINES_OPEN(forced)
	room:GetItem("BINES_SMALL"):Hide()
	
	if forced == true then
		room:VideoSeekToEnd(room:GetItem("FLYTRAP_BINES_OPEN"))
	end
end
--------------------------------------------------------------------------------
-- Окончание анимации разрезания ножом мухоловки
function OnAnimationComplete_FLYTRAP_KNIFE(forced)
	room:GetItem("FLYTRAP_KNIFE"):Hide()
	inventory:AddInventoryItem(inventory.alt:GetItem("KNIFE_Q"))
	inventory.alt:EraseOneItemFromCell("KNIFE_Q")
	
	local particl_poison = room:GetItem("FLYTRAP_POISON1")
	particl_poison:Show()
	particl_poison:PlayAnimation()
	
	StartPoison()
end
--------------------------------------------------------------------------------
-- Окончание анимации установки пузырька
function OnAnimationComplete_FLYTRAP_PUT_VIAL(forced)
	StartPoison()
	
	if forced == true then
		room:VideoSeekToEnd(room:GetItem("FLYTRAP_PUT_VIAL"))
	end
end
--------------------------------------------------------------------------------
-- Окончание анимации наполнения пузырька
function OnAnimationComplete_FLYTRAP_POISON2(forced)
	room:GetItem("FLYTRAP_POISON2"):Enable()
	
	if forced == true then
		room:VideoSeekToEnd(room:GetItem("FLYTRAP_POISON2"))
	end
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_VialFind(value)						--открыть зоны для получения яда мухоловки
	room:GetItem("M_POISON"):Show()
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Cheat_WitchHouseExtFlytrap(value)					--выполнить все в локации  !!!
	room:GetItem("FLYTRAP_BINES_OPEN"):Hide()
	room:GetItem("FLYTRAP_HEAD_IDLE1"):Hide()
	room:GetItem("FLYTRAP_HEAD_IDLE2"):Hide()
	room:GetItem("FLYTRAP_HEAD_RUG_IDLE"):Show()
	room:GetItem("FLYTRAP_HEAD_RUG_IDLE"):PlayAnimation()
	room:GetItem("FLYTRAP_POISON1"):Hide()
	room:GetItem("FLYTRAP_POISON2"):Hide()
	room:GetItem("FLYTRAP_PUT_VIAL"):Hide()
	room:GetItem("M_POISON"):Hide()
	room:GetItem("M_BINES"):Hide()
	room:GetItem("BINES_SMALL"):Hide()
	room:GetItem("KEY"):Hide()
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function StartPoison()
	if (room:GetItem("FLYTRAP_POISON1"):IsVisible()) and
	   (room:GetItem("FLYTRAP_PUT_VIAL"):IsVisible()) then
		
		room:GetItem("M_POISON"):Hide()
		
		local videoFill = room:GetItem("FLYTRAP_POISON2")
		videoFill:Show()
		videoFill:PlayAnimation()
		room:AddCtrlAnimation("FLYTRAP_POISON2", -1)
		room.sfx:Play("fill_vial")	--звук "подставили пузырек, в который накапал яд из листа"
	end
end
--------------------------------------------------------------------------------
function FlytrapAttack()
	room:GetItem("FLYTRAP_HEAD_IDLE1"):Hide()
	room:GetItem("FLYTRAP_HEAD_IDLE2"):Hide()
	--пасть атакует
	local head_attack = room:GetItem("FLYTRAP_HEAD_ATTACK")
	head_attack:Show()
	head_attack:PlayAnimation()	
	room.sfx:Play ("argh")	--звук "пасть атакует"
end