--------------------------------------------------------------------------------
--  File     : Wizard's_tower_int_Console
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
ManholeSwitchTime = 1.0
ManholeSwitchRestTime = 1.0
--------------------------------------------------------------------------------
function OnSerialize(system)
	ManholeSwitch = system:Serialize(ManholeSwitch)
	ManholeSwitchDt = system:Serialize(ManholeSwitchDt)
	ManholeSwitchPosBegin = system:Serialize(ManholeSwitchPosBegin)
	ManholeSwitchPosDt = system:Serialize(ManholeSwitchPosDt)

	ExtrExitManhole()
end
--------------------------------------------------------------------------------
function OnFirstEntry()
	cstore:SetKey("Close_up_Console", 0)
	room:GetItem("MANHOLE_SWITCH_CLOSE"):Disable()
	ManholeSwitchPosBegin = room:GetItem("MANHOLE_SWITCH_CLOSE"):GetPosition()
	
	local SceneSize = room:GetNoZoomSize()
	ManholeSwitchPosDt = ctypes:Vector2(30/1000 * SceneSize.x, -224/750 * SceneSize.y)
	ManholeSwitch = 0
	ManholeSwitchDt = 0
	
	room.sfx:Play ("puzzle_done")	--звук "лабиринт пройден"
		
	stars:Show("M_RAVEN_DESCR", "4")
	stars:Show("M_MANHOLE_DESCR", "4")
	stars:Show("M_WINGSWITCH", "4")
	stars:Show("MANHOLE_SWITCH_CLOSE", "4")
	stars:Show("WINGS_SWITCH_CLOSE", "4")
end
--------------------------------------------------------------------------------
function OnMouseClickOnBack()
	if cstore:IsKeyExists("Water_flush") then
		chapter:ExitCloseup()
	else
		comment:Add("CMT_Back_Player")
		return false
	end
	return true
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
	
	if 	itemName == "MANHOLE_SWITCH_CLOSE" then
		if not(item:IsEnabled()) then		
			if cstore:IsKeyExists("Water_flush") then
				if cstore:IsKeyExists("Tower_chain") then
					--рычаг подымается
					ManholeSwitch = 5
					room:GetItem("MANHOLE_SWITCH_CLOSE"):Enable()
					stars:Hide("MANHOLE_SWITCH_CLOSE")
					cstore:SetKey("Close_up_Console", 1)
					room.sfx:Play ("hatch_open")	--звук "рычаг в другую сторону - люк открывается"
				else
					--рычаг не поднимается, так как цепь порвана
					comment:Add("CMT_Chain_Player")
					room.sfx:Play ("hatch_not_open")	--звук "рычаг в другую сторону - люк не открывается"
				end
			else
				--вода сливается, рычаг подымается и опускается
				ManholeSwitch = 1				
				cstore:SetKey("Water_flush", true)
				comment:Add("CMT_Flush_Player")
				room.sfx:Play ("water_leave")	--звук "дергаем за рычаг, вода сливается, цепь рвется, крышка падает"
			end
			return true
		end

	elseif itemName == "M_WINGSWITCH" then
		comment:Add("CMT_Wingswitch_Player")
		return true

	elseif itemName == "WINGS_SWITCH_CLOSE" then
		item:Hide()
		room:GetItem("WINGS_SWITCH_OPEN"):Show()
		cstore:SetKey("Close_up_Console", 2)
		room:MarkCompleted()
		chapter:ExitCloseup ()
		return true
	
	elseif itemName == "M_MANHOLE_DESCR" then
		comment:Add("CMT_HoleDescr_Player")
		return true
	
	elseif itemName == "M_RAVEN_DESCR" then
		comment:Add("CMT_RavenDescr_Player")
		return true
	end
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)					
	local itemName = item:GetName()
	
	if itemName == "M_WINGSWITCH" then						--деталь от пульта на пульт
		if cursor:IsItemNameEqual("WINGS_SWITCH") then		
			cursor:RemoveItem()
			item:Hide()
			room:GetItem("WINGS_ON"):Show()
			room:GetItem("WINGS_SWITCH_CLOSE"):Show()		--активизировать рычаг управления крыльями
			room.sfx:Play ("set_switch")	--звук "устанавливаем контрльную панель"
			return true
		end
	end
end
--------------------------------------------------------------------------------
function OnUpdate(elapsedTime)
	
	--рычаг подымается
	if (ManholeSwitch == 1) or (ManholeSwitch == 5) then
		ManholeSwitchDt = ManholeSwitchDt + elapsedTime
		local posX = ManholeSwitchPosBegin.x + ManholeSwitchPosDt.x * math.sin((math.pi / 2) * ManholeSwitchDt / ManholeSwitchTime)
		local posY = ManholeSwitchPosBegin.y + ManholeSwitchPosDt.y * math.sin((math.pi / 2) * ManholeSwitchDt / ManholeSwitchTime)
		room:GetItem("MANHOLE_SWITCH_CLOSE"):SetPosition(ctypes:Vector2(posX, posY))
		
		if ManholeSwitchDt >= ManholeSwitchTime then
			if ManholeSwitch == 1 then
				ManholeSwitch = 2
				ManholeSwitchDt = 0
			elseif ManholeSwitch == 5 then
				ManholeSwitch = 6
			end
		end
	end
	
	--рычаг остается в верхнем положении
	if ManholeSwitch == 2 then
		ManholeSwitchDt = ManholeSwitchDt + elapsedTime
		if ManholeSwitchDt >= ManholeSwitchRestTime then
			ManholeSwitch = 3
			ManholeSwitchDt = 0
		end
	end
	
	--рычаг опускается
	if ManholeSwitch == 3 then
		ManholeSwitchDt = ManholeSwitchDt + elapsedTime
		local posX = ManholeSwitchPosBegin.x + ManholeSwitchPosDt.x - ManholeSwitchPosDt.x * math.sin((math.pi / 2) * ManholeSwitchDt / ManholeSwitchTime)
		local posY = ManholeSwitchPosBegin.y + ManholeSwitchPosDt.y - ManholeSwitchPosDt.y * math.sin((math.pi / 2) * ManholeSwitchDt / ManholeSwitchTime)
		room:GetItem("MANHOLE_SWITCH_CLOSE"):SetPosition(ctypes:Vector2(posX, posY))
		
		if ManholeSwitchDt >= ManholeSwitchTime then
			ManholeSwitch = 4
			ManholeSwitchDt = 0
		end
	end
	
end
--------------------------------------------------------------------------------
function OnExit()
	ExtrExitManhole()
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_TowerIntMG(value)								--миниигра пройдена
	comment:Add("CMT_Done_Player")
end
--------------------------------------------------------------------------------
function ExtrExitManhole()
	if (ManholeSwitch >= 1) and (ManholeSwitch <= 3) then
		room:GetItem("MANHOLE_SWITCH_CLOSE"):SetPosition(ManholeSwitchPosBegin)
		ManholeSwitch = 4
		ManholeSwitchDt = 0
	end
	
	if ManholeSwitch == 5 then
		room:GetItem("MANHOLE_SWITCH_CLOSE"):SetPosition(ManholeSwitchPosBegin + ManholeSwitchPosDt)
		ManholeSwitch = 6
		ManholeSwitchDt = 0
	end
end