--------------------------------------------------------------------------------
--  File     : Scary_place_exit_Skeleton
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
dofile("gamedata:_Chapters/Main/Helper/PickSkeletons.lua")
--------------------------------------------------------------------------------
function OnSerialize(system)
	effect = system:Serialize(effect)
	
	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)
	EndOffSkeletonPick()
end
--------------------------------------------------------------------------------
function OnFirstEntry()
	cstore:SetKey("Close_up_Skeleton", 0)
	
	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
	effect = 0
	
	room.im:CloneItem(room:GetItem("PICK_EFFECTS"), "1", true):Hide()
	room.im:CloneItem(room:GetItem("PICK_EFFECTS"), "2", true):Hide()
	
	room:GetItem("STATUETTE_HAND"):Disable()

	stars:Show("M_SKELETON", "4")
	stars:Show("M_STATUETTE_HAND", "4")
	stars:Show("STATUETTE_HAND", "4")
	stars:Show("STONE", "4")
end
--------------------------------------------------------------------------------
function OnMouseClickOnBack()
	chapter:ExitCloseup()
	return true
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
	
	if itemName == "STONE" then						--камень
		pick_panel:AddItems(itemName, false)
		return true
	
	elseif	itemName == "M_SKELETON" then			--скелет3
		comment:Add("SKELETON_COMM1")
		return true
	
	elseif	itemName == "M_STATUETTE_HAND" then		--рука статуэтки в камне
		comment:Add("CMT_Hand_Player")
		return true
	
	elseif (itemName == "STATUETTE_HAND") and		--рука статуэтки
		   (item:IsEnabled()) then
		cstore:SetKey("Shelf_HO", 2)
		pick_panel:AddItems(itemName, false)
		return true
	
	elseif itemName == "SKELETON" then				--скелет
		pick_panel:AddItemsSet(itemName, 3, true)
		PickSkeletons()
		cstore:SetKey("Close_up_Skeleton", 2)
		room:MarkCompleted()
		return true	
	end
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)							
	local itemName = item:GetName()

	if itemName == "M_STATUETTE_HAND" then			--кайло по руке статуэтки 
		if cursor:IsItemNameEqual("PICK") then	
			cursor:RemoveItem()
			item:Hide()
			local videoPick = room:GetItem("SKELETON_PICK")
			videoPick:Show()
			videoPick:PlayAnimation()
			room:AddCtrlAnimation("SKELETON_PICK", -1)
			room.sfx:Play ("hacking")				--звук "отбиваем кайлом руку статуэтки"
			return true
		end
	end
	
	if itemName == "M_SKELETON" 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()
	EndOffSkeletonPick()
end
--------------------------------------------------------------------------------
function OnUpdate (elapsedTime)
	
	UpdateSkeleton(elapsedTime, "M_SKELETON")
		
	local videoPick = room:GetItem("SKELETON_PICK")
	local effectsPick = room:GetItem("PICK_EFFECTS")
	local effectsPick1 = room:GetItem("PICK_EFFECTS1")
	local effectsPick2 = room:GetItem("PICK_EFFECTS2")
	local nakl = room:GetItem("STATUETTE_HAND_STONE")
	
	if videoPick:IsVisible() then
		if videoPick:IsAnimPlaying() then
			local frame = room:GetVideoFrame(videoPick:GetName())
			if (frame >= 10) and (effect == 0) then
				effectsPick:Show()
				effectsPick:PlayAnimation()
				room:AddControlForParticleSys("PICK_EFFECTS")
				effect = 1
			end
			if (frame >= 26) and (effect == 1) then
				effectsPick1:Show()
				effectsPick1:PlayAnimation()
				room:AddControlForParticleSys("PICK_EFFECTS1")
				effect = 2
			end
			if (frame >= 42) and (effect == 2) then
				effectsPick2:Show()
				effectsPick2:PlayAnimation()
				room:AddControlForParticleSys("PICK_EFFECTS2")
				effect = 3
				
				local color = nakl:GetColor()
				color:SetNormalAlpha(0)
				nakl.effects:PlayColor(color, 0.5)
			end
		end
	end
	
	if not(nakl.effects:IsPlayingColor ()) 
	   and (nakl:IsVisible()) and (effect == 3) then
		nakl:Hide()
		effect = 4
	end
end
--------------------------------------------------------------------------------
-- Окончание анимации отбивания руки статуэтки
function OnAnimationComplete_SKELETON_PICK(forced)
	room:GetItem("SKELETON_PICK"):Hide()
	room:GetItem("STATUETTE_HAND"):Enable()
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Cheat_Scary_place_exit_Skeleton(value)						--выполнить все в локации  !!!
	room:GetItem("STONE"):Hide()
	room:GetItem("M_STATUETTE_HAND"):Hide()
	room:GetItem("STATUETTE_HAND"):Hide()
	room:GetItem("STATUETTE_HAND_STONE"):Hide()
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function EndOffSkeletonPick()
	if (effect > 0) and (effect < 4) then
		room:GetItem("PICK_EFFECTS"):Hide()
		room:GetItem("PICK_EFFECTS1"):Hide()
		room:GetItem("PICK_EFFECTS2"):Hide()
		room:GetItem("STATUETTE_HAND_STONE"):Hide()
		effect = 4
	end
end