--------------------------------------------------------------------------------
--  File     : Dam
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
time_explosion = 3
time_fade = 0.5
--------------------------------------------------------------------------------
function OnSerialize(system)
	time_explosion_dt = system:Serialize(time_explosion_dt)
	
	ChangeDam()
end
--------------------------------------------------------------------------------
function OnFirstEntry()
	--inventory:CheatLoadAndAddItem("NITROGLYCERINE", "Smithy_Chemical_lab", 0)
	
	time_explosion_dt = 0
	
	stars:Show("M_TO_TOWER", "3")
	stars:Show("GIANT_FLOWER", "4")
	stars:Show("M_WEAK_SPOT", "4")

	music:Stop(true)
	music:PlayLoopWithDelay(10, "Mystery_Quest_light")
	room.sfx:Play ("jingle_10_boom")			--звук джингла при первом входе
	room.sfx:PlayEx ("dam_working", true, 1, 0)	--зацикленный звук проигрывается до взрыва
end
--------------------------------------------------------------------------------
function OnEntry()
	if music:GetName() ~= "Mystery_Quest_light" then
		music:PlayLoopWithDelay(1, "Mystery_Quest_light") 
	else
		music:PlayLoopWithDelay(1, "horror_2") 
	end
	ambient:Play("Dam")
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
	
	if itemName == "M_TO_MILL" then							--переход к мельнице
		chapter:JumpRoom("Mill_ext")
		return true
		
	elseif itemName == "M_TO_TOWER" then					--переход к башне
		chapter:JumpRoom("Wizard's_tower_ext")
		return true
	end
	
	
	if itemName == "M_FLOWER" then						--цветок
		comment:Add("CMT_Flower1_Player")
		if cstore:GetKey("Cupboard_HO") < 2 then
			cstore:SetKey("Cupboard_HO", 2)
		end
		return true
		
	elseif itemName == "GIANT_FLOWER" then				-- бутон цветка
		pick_panel:AddItems(itemName, false)
		cstore:SetKey("Ingredient_flower", true)
		if cstore:IsKeyExists("Ingredient_salt") then
			comment:Add("FLOWER_SALT")
		end
		return true
		
	elseif itemName == "M_BUILDING" then				--постройка
		comment:Add("CMT_Building_Player")
		return true
		
	elseif itemName == "M_PIPE" then					--труба
		comment:Add("CMT_Pipe_Player")
		return true
		
	elseif itemName == "M_WEAK_SPOT" then				--слабое место
		comment:Add("CMT_WeakSpot_Player")
		return true
	end
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)					
	local itemName = item:GetName()

	if itemName == "M_FLOWER" then						
		if cursor:IsItemNameEqual("KNIFE_Q") then		--нож на цветок
			comment:Add("CMT_Flower2_Player")
			return true
		end
		
		if cursor:IsItemNameEqual("SPADE_Q") then		--лопата на цветок
			comment:Add("CMT_Flower3_Player")
			return true
		end
			
		if cursor:IsItemNameEqual("GARDEN_SHEARS") then	--садовые ножницы на цветок
			cursor:RemoveItem()
			item:Hide()
			room:GetItem("FLOWER_PLANT"):Hide()
			
			local flower_cut = room:GetItem("FLOWER_CUT")
			flower_cut:Show()
			flower_cut:PlayAnimation()
			room:AddCtrlAnimation("FLOWER_CUT", -1)
			room.sfx:Play ("cut_flower")	--звук "срезаем кусачками цветок"
			return true
		end
	end
	
	if itemName == "M_WEAK_SPOT" then					--взрыв
		if cursor:IsItemNameEqual("NITROGLYCERINE") then				
			inventory.alt:AddItemFromCursor()
			
			local explosive_throw = room:GetItem("EXPLOSIVE_THROW")
			explosive_throw:Show()
			explosive_throw:PlayAnimation()
			room:AddCtrlAnimation("EXPLOSIVE_THROW", -1)
			room.sfx:Play ("throw")				--звук "бросания"
			return true
		end
	end
end
--------------------------------------------------------------------------------
function OnUpdate (elapsedTime)
	
	local explosive_throw = room:GetItem("EXPLOSIVE_THROW")
	if explosive_throw:IsVisible() then
		if explosive_throw:IsAnimPlaying () then

			local dam_explosion = room:GetItem("DAM_EXPLOSION")
			if not(dam_explosion:IsVisible()) then
				
				local frame = room:GetVideoFrame(explosive_throw:GetName())
				if frame >= 27 then
					room:GetItem("M_WEAK_SPOT"):Hide()
					cstore:SetKey("Explosion_Tower", true)
		
					dam_explosion:Show()
					dam_explosion:PlayAnimation()
			
					room.sfx:Play ("badabum")	--звук "взрываем нитроглицерином дамбу (вдали звук падающей башни)"
					room.sfx:Stop("dam_working")
					music:Stop(true)
					music:PlayLoopWithDelay(0, "wizard_tower")
				end
			end
		end
	end
	
	local dam_explosion = room:GetItem("DAM_EXPLOSION")
	if dam_explosion:IsVisible() then
		if time_explosion_dt < time_explosion then
			time_explosion_dt = time_explosion_dt + elapsedTime
		else
			local m_pipe = room:GetItem("M_PIPE")
			if m_pipe:IsVisible() then
				m_pipe:Hide()
				room:GetItem("M_BUILDING"):Hide()
				room:GetItem("DAM_WORKING"):Hide()
			
				--отобразить взорванную дамбу
				local dam_exploded = room:GetItem("DAM_EXPLODED")
				dam_exploded:Show()
				local color = dam_exploded:GetColor()
				local color1 = dam_exploded:GetColor()
				color1:SetNormalAlpha(0)
				dam_exploded:SetColor(color1)
				dam_exploded.effects:PlayColor(color, time_fade)
				
				--скрыть целую дамбу
				local flower_bole = room:GetItem("FLOWER_BOLE")
				local color2 = flower_bole:GetColor()
				color2:SetNormalAlpha(0)
				flower_bole.effects:PlayColor(color2, time_fade)
								
				local dam = room:GetItem("DAM")
				local color3 = dam:GetColor()
				color3:SetNormalAlpha(0)
				dam.effects:PlayColor(color3, time_fade)
				
				comment:Add("CMT_Explosion_Player")
			end
		end
	end
end
--------------------------------------------------------------------------------
function OnExit()
	ChangeDam()
end
--------------------------------------------------------------------------------
-- Окончание анимации отрезания цветка
function OnAnimationComplete_FLOWER_CUT(forced)
	room:GetItem("FLOWER_CUT"):Hide()
	room:GetItem("GIANT_FLOWER"):Show()
end
--------------------------------------------------------------------------------
-- Окончание анимации бросания нитроглицерина в дамбу
function OnAnimationComplete_EXPLOSIVE_THROW(forced)
	room:GetItem("EXPLOSIVE_THROW"):Hide()

	if forced == true then
		if inventory.alt:IsItemExisting ("NITROGLYCERINE") then
			if not(room:GetItem("DAM_EXPLOSION"):IsVisible()) then
				inventory:AddInventoryItem(inventory.alt:GetItem ("NITROGLYCERINE")) 
			end
			inventory.alt:EraseOneItemFromCell("NITROGLYCERINE")
		end
		
	else
		if inventory.alt:IsItemExisting ("NITROGLYCERINE") then
			inventory.alt:EraseOneItemFromCell("NITROGLYCERINE")
		end
	end
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Cheat_Dam1(value)					--выполнить все в локации  !!!
	if inventory:IsItemExisting("CIRCLE") then
		inventory:EraseOneItemFromCell("CIRCLE")
	end
end
function OnChapterStoreEvent_Cheat_Dam2(value)					
	room:GetItem("M_WEAK_SPOT"):Hide()
	room:GetItem("M_PIPE"):Hide()
	room:GetItem("M_BUILDING"):Hide()
	room:GetItem("M_FLOWER"):Hide()
	room:GetItem("DAM"):Hide()
	room:GetItem("DAM_WORKING"):Hide()
	room:GetItem("FLOWER_BOLE"):Hide()
	room:GetItem("FLOWER_PLANT"):Hide()
	room:GetItem("FLOWER_CUT"):Hide()
	room:GetItem("DAM_EXPLOSION"):Hide()
	room:GetItem("DAM_EXPLODED"):Show()
end
--------------------------------------------------------------------------------
------------  Изменить дамбу при выходе и сохранении  --------------------------
function ChangeDam()
	local dam_explosion = room:GetItem("DAM_EXPLOSION")
	if dam_explosion:IsVisible() then
		room:GetItem("M_PIPE"):Hide()
		room:GetItem("M_BUILDING"):Hide()
		room:GetItem("DAM_EXPLODED"):Show()
		dam_explosion:Hide()
		room:GetItem("FLOWER_BOLE"):Hide()
		room:GetItem("DAM"):Hide()
		room:GetItem("DAM_WORKING"):Hide()
	end
end
