--------------------------------------------------------------------------------
--  File     : Scary_glade
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
dofile("gamedata:_Chapters/Main/Helper/PickSkeletons.lua")
--------------------------------------------------------------------------------
chance_init = 7			--шанс появления CROCODILE_IDLE2 - 1 к chance
--------------------------------------------------------------------------------
function OnSerialize(system)
	chance = system:Serialize(chance)
	chance_dt = system:Serialize(chance_dt)
	resin = system:Serialize(resin)

	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()
	--inventory:CheatLoadAndAddItem("RUNE_AIR", "Chapel_int_Casket", 0)
	--inventory:CheatLoadAndAddItem("RUNE_EARTH", "Chapel_int_Casket", 0)
	--inventory:CheatLoadAndAddItem("RUNE_FIRE", "Chapel_int_Casket", 0)
	--inventory:CheatLoadAndAddItem("RUNE_WATER", "Chapel_int_Casket", 0)
	
	music:Stop(true)
	music:PlayLoopWithDelay(10, "Scary_place")
	room.sfx:Play ("jingle_4")	--звук джингла при первом входе
	
	right = true
	runesOnSkeleton = 0
	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
		
	resin = 0
	chance = 0
	chance_dt = 0
	use_rune = 0
	name_cur_music = "Scary_place"
	
	cstore:SetKey("Close_up_Lantern", 4)
	
	stars:Show("M_TO_HOUSE", "3")
	stars:Show("M_BOAT", "4")
	stars:Show("M_RESIN", "4")
	stars:Show("M_CROCODILE", "4")
	stars:Show("SKELETON1_STONE", "4")
end
--------------------------------------------------------------------------------
function OnEntry()
	music:PlayLoopWithDelay(20, "horror_6") 
	ambient:Play("Scary_glade")
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
	
	if itemName == "M_TO_GATE" then										--к охотничьему домику
		chapter:JumpRoom("Gate")
		return true
	elseif itemName == "M_TO_HOUSE" then								--к домику ведьмы
		chapter:JumpRoom("Witch's_house_ext")
		return true
	elseif itemName == "M_BOAT" then									--лодка
		if cstore:IsKeyExists("scary_glade_croco") then
			chapter:JumpRoom("Scary_glade_Boat_HO")
		else
			comment:Add("CMT_Croco2_Player")
		end
		return true
	end
	
	
	if itemName == "M_RESIN" then										--смола на дереве
		if resin == 0 then
			comment:Add("CMT_Rezin1_Player")
			resin = 1
		else
			comment:Add("CMT_Rezin2_Player")
		end
		return true
	
	elseif itemName == "M_CROCODILE" then								--крокодил
		local croco1 = room:GetItem("CROCODILE_IDLE1")
		local croco2 = room:GetItem("CROCODILE_IDLE2")
		if (croco1:IsVisible()) or (croco2:IsVisible()) then
			comment:Add("CMT_Croco1_Player")
			croco1:Hide()
			croco2:Hide()
			
			local croco_attack = room:GetItem("CROCODILE_ATTAC")
			croco_attack:Show()
			croco_attack:PlayAnimation()	
			room.sfx:Play ("croco_rrrr")	--звук "трогаем крокодила, он рычит, не дает ничего делать"
			return true
		end

	elseif itemName == "SKELETON1_STONE" then							--скелет в камне
		comment:Add("SKELETON_COMM1")
		return true
	
	elseif itemName == "SKELETON" then									--скелет
		pick_panel:AddItemsSet(itemName, 3, false)
		PickSkeletons()
		return true
	end
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)
	local itemName = item:GetName()
	
	if itemName == "M_CROCODILE" then								
		local croco1 = room:GetItem("CROCODILE_IDLE1")
		local croco2 = room:GetItem("CROCODILE_IDLE2")
		local croco3 = room:GetItem("CROCODILE_ATTAC")
		
		if (croco1:IsVisible()) or (croco2:IsVisible()) or (croco3:IsVisible()) then
		
			if cursor:IsItemNameEqual("SLEEP_POWDER") then					--сонный порошок на крокодила
				cursor:RemoveItem()
				item:Hide()
			
				local powder_crocodile = room:GetItem("POWDER_CROCODILE")
				powder_crocodile:Show()
				powder_crocodile:PlayAnimation()
				room:AddCtrlAnimation("POWDER_CROCODILE", -1)
			
				room.sfx:Play ("croco_zzzz")	--звук "сыпем сонный порошок и крокодил засыпает"
				return true
		
			elseif cursor:IsItemNameEqual("SPADE_Q") then					--лопата на крокодила
				croco1:Hide()
				croco2:Hide()
				croco3:Hide()
				inventory:AddItemFromCursor()
				comment:Add("CMT_Croco3_Player")
				
				local croco_attack = room:GetItem("CROCODILE_ATTAC")
				croco_attack:Show()
				croco_attack:PlayAnimation()	
				room.sfx:Play ("croco_rrrr")
				return true
			end   
				
		end
	end
	
	if cursor:IsItemNameEqual("RAG") then								--лоскут на смолу
		if itemName == "M_RESIN" then
			cursor:RemoveItem()
			item:Hide()
			
			local videoRag = room:GetItem("RUG_RESIN")
			videoRag:Show()
			videoRag:PlayAnimation()
			room:AddCtrlAnimation("RUG_RESIN", -1)
			room.sfx:Play ("smear_rag")		--звук "лоскут вымазываем смолой"
			return true
		end
	end
	
	if itemName == "SKELETON1_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 OnUpdate (elapsedTime)
		
	UpdateSkeleton(elapsedTime, "SKELETON1_STONE")
	
	if not(cstore:IsKeyExists("scary_glade_croco")) then
		local croco_attack = room:GetItem("CROCODILE_ATTAC")
		local croco_idle1 = room:GetItem("CROCODILE_IDLE1")
		local croco_idle2 = room:GetItem("CROCODILE_IDLE2")
		
		if chance ~= chance_dt then
			if (croco_idle1:IsVisible()) and not(croco_idle1:IsAnimPlaying ()) then
				croco_idle1:PlayAnimation()
				chance_dt = chance_dt + 1
			end
		else
			if (croco_idle1:IsVisible()) and not(croco_idle1:IsAnimPlaying ()) then
				croco_idle1:Hide()
				croco_idle2:Show()
				croco_idle2:PlayAnimation()
				chance = math.random(chance_init)
				chance_dt = 0
			end
		end
	
		if (croco_idle2:IsVisible()) and not(croco_idle2:IsAnimPlaying ()) then
			croco_idle2:Hide()
			croco_idle1:Show()
			croco_idle1:PlayAnimation()
		end
	
		if (croco_attack:IsVisible()) and not(croco_attack:IsAnimPlaying ()) then
			croco_attack:Hide()
			croco_idle1:Show()
			croco_idle1:PlayAnimation()
		end
	end
end
--------------------------------------------------------------------------------
function OnExit()
	ExitSkeletons()
end
--------------------------------------------------------------------------------
-- Окончание анимации вымазывания тряпки в смолу
function OnAnimationComplete_RUG_RESIN(forced)
	room:GetItem("RUG_RESIN"):Hide()
		
	if forced == true then
		inventory:CreateAndAddItem(room:GetItem("RAG_STICKY"))
	else
		pick_panel:AddItems("RAG_STICKY", false)	
	end
end
--------------------------------------------------------------------------------
-- Окончание анимации посыпания крокодила сонным порошком
function OnAnimationComplete_POWDER_CROCODILE(forced)
	room:GetItem("CROCODILE_IDLE1"):Hide()
	room:GetItem("CROCODILE_IDLE2"):Hide()
	room:GetItem("CROCODILE_ATTAC"):Hide()
	
	local croco_go_sleep = room:GetItem("CROCODILE_GO_SLEEP")
	croco_go_sleep:Show()
	croco_go_sleep:PlayAnimation()
	room:AddCtrlAnimation("CROCODILE_GO_SLEEP", -1)
		
	if forced == true then
		room:VideoSeekToEnd(room:GetItem("POWDER_CROCODILE"))
	end
end
--------------------------------------------------------------------------------
-- Окончание анимации засыпание крокодила
function OnAnimationComplete_CROCODILE_GO_SLEEP(forced)
	
	room:GetItem("CROCODILE_GO_SLEEP"):Hide()
	
	local croco_sleep = room:GetItem("CROCODILE_SLEEP_LOOP")
	croco_sleep:Show()
	croco_sleep:PlayAnimation()
	cstore:SetKey("scary_glade_croco", true)
	stars:Hide("M_BOAT")
	stars:Show("M_BOAT", "1")
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Boat_HO(value)									--закрыть hidden object с лодкой
	if (value == 1) or (value == 3) then
		room:GetItem("M_BOAT"):Hide()
		if value == 1 then
			cstore:SetKey("Sleep_Powder", 2)
		end
	elseif value == 2 then
		room:GetItem("M_BOAT"):Show()
	end
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Cheat_ScaryGlade1(value)			--выполнить все в локации  !!!
	room:GetItem("M_RESIN"):Hide()
	room:GetItem("M_CROCODILE"):Hide()
	room:GetItem("CROCODILE_IDLE1"):Hide()
	room:GetItem("CROCODILE_IDLE2"):Hide()
	room:GetItem("CROCODILE_ATTAC"):Hide()
	room:GetItem("CROCODILE_GO_SLEEP"):Hide()
	room:GetItem("CROCODILE_SLEEP_LOOP"):Show()
	room:GetItem("CROCODILE_SLEEP_LOOP"):PlayAnimation()
	cstore:SetKey("scary_glade_croco",true)
	stars:Hide("M_BOAT")
	stars:Show("M_BOAT", "1")
	if cstore:GetKey("Boat_HO") ~= 2 then
		cstore:SetKey("Boat_HO", 1)
	end
end
function OnChapterStoreEvent_Cheat_ScaryGlade2(value)						
	room:GetItem("SKELETON"):Hide()
	room:GetItem("SKELETON1_STONE"):Hide()
end
function OnChapterStoreEvent_Cheat_ScaryGlade3(value)						
	cstore:SetKey("Boat_HO", 3)
end
--------------------------------------------------------------------------------
function CheatSkip () 											--выполнить все в предыдущих локациях   !!!
	--добавить инвентарные предметы
	inventory:CheatLoadAndAddItem ("SPADE_Q", "Hut_1st_floor", 0)
	inventory:CheatLoadAndAddItem ("KNIFE_Q", "Hut_1st_floor_Cabinet", 0)
	
	if inventory:IsItemExisting("MATCHES") then
		inventory:EraseOneItemFromCell("MATCHES")
	end
	if inventory:IsItemExisting("KNIFE") then
		inventory:EraseOneItemFromCell("KNIFE")
	end

	-- откл ветки хинтов
	hint:Cheat("MAGIC_BUSHES")	
	
	--локация Gate
	cstore:SetKey("Cheat_Gate", true)
	--локация Hut_1st_floor
	cstore:SetKey("Cheat_Hut1stFloor1", true)
	--локация Hut_2nd_floor
	cstore:SetKey("Cheat_Hut2ndFloor", true)
end

