--------------------------------------------------------------------------------
--  File     : PickSkeletons.lua
--  Author(s): Prokopovich Larisa
--
--  Summary  : Вспомогательные функции для собирания скелетов.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
-------------------------------------------------------------------------------
right_runes = 
{
	"RUNE_EARTH",
	"RUNE_FIRE",
	"RUNE_AIR",
	"RUNE_WATER"
}
clone_runes = 
{
}
-------------------------------------------------------------------------------
effect_rune_begin = 0.5
effect_rune_end = 0.5
effect_rune_blink = 0.5
effects_skeleton = 0.5
----------------Применение рун на скелет----------------------------------------
function RunesOnSkeletons(mask_name)
	if runesOnSkeleton < #right_runes then
		runesOnSkeleton = runesOnSkeleton + 1
	
		--занести имя руны в список примененных рун
		local name = cursor:GetItem():GetName()
		clone_runes[runesOnSkeleton] = name
	
		--отобразить руну и свет вокруг нее
		local rune 
		local rune_light
		if room.im:IsItemExisting(name .. tostring(runesOnSkeleton)) then
			rune = room:GetItem(name .. tostring(runesOnSkeleton))
			rune_light = room:GetItem(name .. "_LIGHT" .. tostring(runesOnSkeleton))
		else
			rune = room.im:CloneItem(room:GetItem(name), tostring(runesOnSkeleton), true)
			rune_light = room.im:CloneItem(room:GetItem(name .. "_LIGHT"), tostring(runesOnSkeleton), true)
		end
		local pos = pos_runes_init + ctypes:Vector2((runesOnSkeleton - 1) * pos_runes_dt, 0)
		rune:SetPosition(pos)
		ShowFade(rune, effect_rune_begin)
	
		pos = pos_runes_light_init + ctypes:Vector2((runesOnSkeleton - 1) * pos_runes_dt, 0)
		rune_light:SetPosition(pos)
		ShowFade(rune_light, effect_rune_begin)
	
		--склонировать частицы для руны
		local rune_part
		if room.im:IsItemExisting(clone_runes[runesOnSkeleton] .. "_PART" .. tostring(runesOnSkeleton)) then
			rune_part = room:GetItem(clone_runes[runesOnSkeleton] .. "_PART" .. tostring(runesOnSkeleton))
		else	
			rune_part = room.im:CloneItem(room:GetItem(clone_runes[runesOnSkeleton] .. "_PART"), tostring(runesOnSkeleton), true)	
		end
		local pos_rune_part = pos_runes_init + ctypes:Vector2((runesOnSkeleton - 1) * pos_runes_dt + 15, rune:GetHeight() - 20)
		rune_part:SetPosition(pos_rune_part)
	
		--отобразить частицы проявления для руны
		local rune_show_part
		if room.im:IsItemExisting("RUNE_SHOW" .. tostring(runesOnSkeleton)) then
			rune_show_part = room:GetItem("RUNE_SHOW" .. tostring(runesOnSkeleton))
		else
			rune_show_part = room.im:CloneItem(room:GetItem("RUNE_SHOW"), tostring(runesOnSkeleton), true)	
			pos = pos_runes_init + ctypes:Vector2((runesOnSkeleton - 1) * pos_runes_dt + rune:GetWidth() / 2, rune:GetHeight() / 2) 
			rune_show_part:SetPosition(pos)
		end
		rune_show_part:Show()
		rune_show_part:PlayAnimation()
		room:AddControlForParticleSys("RUNE_SHOW" .. tostring(runesOnSkeleton))
		
		room.sfx:Play ("put_rune")		--звук "прикладываем к скелету камень руны"
		
		--проверить, правильная ли руна
		if name ~= right_runes[runesOnSkeleton] then
			right = false
		end
	
		inventory:AddItemFromCursor()
	
		--изменить музыку
		if use_rune == 0 then
			use_rune = 1
			name_cur_music = music:GetName()
			music:SetPause(true, true)
			music:Play("horror_7", true)
		end
	
		--проверить, расколдован ли скелет
		if runesOnSkeleton == #right_runes then
			if right then
				if mask_name == "SKELETON1_STONE" then
					room:GetItem("SKELETON"):Show()
					HideFade(room:GetItem(mask_name), effects_skeleton)
					local part1 = room:GetItem("STONES1_HIDE")
					part1:Show()
					part1:PlayAnimation()
					room:AddControlForParticleSys("STONES1_HIDE")
					cstore:SetKey("Skeleton_Scary_glade", true)
				elseif mask_name == "M_SKELETON" then
					room:GetItem(mask_name):Hide()
					HideFade(room:GetItem("SKELETON3_STONE"), effects_skeleton)
					local part2 = room:GetItem("STONES2_HIDE")
					part2:Show()
					part2:PlayAnimation()
					room:AddControlForParticleSys("STONES2_HIDE")
					cstore:SetKey("Close_up_Skeleton", 1)
				elseif 	mask_name == "SKELETON2_STONE" then
					room:GetItem("SKELETON"):Show()
					HideFade(room:GetItem(mask_name), effects_skeleton)
					local part3 = room:GetItem("STONES3_HIDE")
					part3:Show()
					part3:PlayAnimation()
					room:AddControlForParticleSys("STONES3_HIDE")
					cstore:SetKey("Close_up_Flytrap", 2)
				end
		
				--убрать руны из инвентаря
				if (cstore:IsKeyExists("Skeleton_Scary_glade")) and
				   (cstore:GetKey("Close_up_Skeleton") >= 1) and
				   (cstore:GetKey("Close_up_Flytrap") >= 2) then
					inventory:EraseOneItemFromCell("RUNE_EARTH")
					inventory:EraseOneItemFromCell("RUNE_FIRE")
					inventory:EraseOneItemFromCell("RUNE_AIR")
					inventory:EraseOneItemFromCell("RUNE_WATER")
				end
			end
		end
	end
end
----------------Выход из локации-----------------------------------------------
function ExitSkeletons()
	ExitSkeletons1()
	
	if music:GetName() == "horror_7" then
		use_rune = 0
		music:Stop(true)
		music:PlayLoopWithDelay(10, name_cur_music)
	end
end
-------------------------------------------------------------------------------
function ExitSkeletons1()
	for i = 1, #clone_runes, 1 do
		room:GetItem(clone_runes[i] .. tostring(i)):Hide()
		
		local item_light = room:GetItem(clone_runes[i] .. "_LIGHT" .. tostring(i))
		local color = item_light:GetColor()
		color:SetNormalAlpha(1)
		item_light:SetColor(color)
		item_light:Hide()
		
		room:GetItem(clone_runes[i] .. "_PART".. tostring(i)):Hide()
	end
	runesOnSkeleton = 0
	right = true
	clone_runes = {}
end
-----------------Проявление по фэйду--------------------------------------------
function ShowFade(item, time_effect)
	item:Show()
	local color = item:GetColor()
	local color1 = item:GetColor()
	color:SetNormalAlpha(1)
	color1:SetNormalAlpha(0)
	item:SetColor(color1)
	item.effects:PlayColor(color, time_effect)
end
-----------------Исчезновение по фэйду--------------------------------------------
function HideFade(item, time_effect)
	local color = item:GetColor()
	color:SetNormalAlpha(0)
	item.effects:PlayColor(color, time_effect)
end
------------------------------------------------------------------------------
function UpdateSkeleton(elapsedTime, mask_name)

	--скрыть камни на скелетах
	local item_mask = room:GetItem(mask_name)
	if (mask_name == "SKELETON1_STONE") or (mask_name == "SKELETON2_STONE") then
		if (item_mask:IsVisible()) and
		   not(item_mask.effects:IsPlayingColor()) and
		   (room:GetItem("SKELETON"):IsVisible()) then
			item_mask:Hide()
		end
	
	elseif mask_name == "M_SKELETON" then
		local stone = room:GetItem("SKELETON3_STONE")
		if not(item_mask:IsVisible()) and
		   (stone:IsVisible()) and
		   not(stone.effects:IsPlayingColor()) then
			stone:Hide()  
		end
	end
	
	--показать частицы для горящих рун
	if runesOnSkeleton > 0 then
		local cur_rune = room:GetItem(clone_runes[runesOnSkeleton] .. tostring(runesOnSkeleton))
		local color_cur_rune = cur_rune:GetColor()
		if not(cur_rune.effects:IsPlayingColor()) and
		  (color_cur_rune:GetNormalAlpha() == 1) then		
	  
			local rune_part = room:GetItem(clone_runes[runesOnSkeleton] .. "_PART" .. tostring(runesOnSkeleton))
			if not(rune_part:IsVisible()) then
				rune_part:Show()
				rune_part:PlayAnimation()
			end
		end
	end
		
	--скрыть руны
	local length = #clone_runes
	if length == #right_runes then
		local rune = room:GetItem(clone_runes[length] .. tostring(length))
		local color_rune = rune:GetColor()
	
		--начать исчезновение всех рун
		if not(rune.effects:IsPlayingColor ()) and
		   (color_rune:GetNormalAlpha() == 1) then
			for i = 1, length, 1 do
				HideFade(room:GetItem(clone_runes[i] .. tostring(i)), effect_rune_end)
				HideFade(room:GetItem(clone_runes[i] .. "_LIGHT" .. tostring(i)), effect_rune_end)
				
				local rune = room:GetItem(clone_runes[i] .. tostring(i))
				local rune_part = room:GetItem(clone_runes[i] .. "_PART" .. tostring(i))
				rune_part:Hide()
				
				if right == false then
					local rune_hide_part
					if room.im:IsItemExisting("RUNE_HIDE" .. tostring(i)) then
						rune_hide_part = room:GetItem("RUNE_HIDE" .. tostring(i))
					else
						rune_hide_part = room.im:CloneItem(room:GetItem("RUNE_HIDE"), tostring(i), true)	
						pos = pos_runes_init + ctypes:Vector2((i - 1) * pos_runes_dt + rune:GetWidth() / 2, rune:GetHeight() / 2) 
						rune_hide_part:SetPosition(pos)
					end
					rune_hide_part:Show()
					rune_hide_part:PlayAnimation()
					room:AddControlForParticleSys("RUNE_HIDE" .. tostring(i))
				end
	
			end
			
			if right then
				room.sfx:Play ("sequence_done")		--звук "последовательность верная"
			else
				room.sfx:Play ("sequence_wrong")	--звук "последовательность неверная"
			end
		end
	
		--скрыть руны
		if not(rune.effects:IsPlayingColor ()) and
		   (color_rune:GetNormalAlpha() == 0) then
			ExitSkeletons1()
		end
	end
	
	--мерцание рун
	for i = 1, #clone_runes, 1 do
		local item_light = room:GetItem(clone_runes[i] .. "_LIGHT" .. tostring(i))
		if not(item_light.effects:IsPlayingColor()) then
			local color = item_light:GetColor()
			if color:GetNormalAlpha() == 1 then
				color:SetNormalAlpha(0.6)
			else
				color:SetNormalAlpha(1)
			end
			item_light.effects:PlayColor(color, effect_rune_blink)
		end
	end
	
end
----------------Собирание скелетов---------------------------------------------
function PickSkeletons()
	room.sfx:Play ("get_bones")		--звук "забираем скелет"

	local fl = cstore:GetKey("Skeletons") + 1
	cstore:SetKey("Skeletons", fl)
				
	if fl == 3 then
		local dialog = ui:GetDialog("PickItems")
		dialog:SetUserTag("pick_skeletons")
	end
	
	music:Stop(true)
	music:PlayLoopWithDelay(10, name_cur_music)
end
----------------Вывод комментария после поднятия последнего скелета-------------
function OnDialogClose_PickItems(dialog)
	if dialog:GetUserTag() == "pick_skeletons" then
		comment:Add("SKELETON_COMM2")
		dialog:SetUserTag("")
	end
end
--------------------------------------------------------------------------------
function OnSerializeSkeletons(system)
	local elements = 0
	if system:IsSaving() then
		elements = #clone_runes
	end
	elements = system:Serialize(elements)

	if system:IsSaving() then
		for i = 1, #clone_runes, 1 do
			system:Put(clone_runes[i])
		end

	else
		local k
		local part
		for i = 1, elements, 1 do
			k = system:Serialize(k)
			clone_runes[i] = k
			part = room:GetItem(k .. "_PART".. tostring(i))
			part:Show()
			part:PlayAnimation()
		end
		
		if elements == #right_runes then
			ExitSkeletons1()
		end
	end
	
end
