--------------------------------------------------------------------------------
--  File     : Wizard's_tower_ext
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
speed_move_tower = 0.8		--скорость перемещения башни
dist_move_tower = 12/1536	--расстояние перемещения башни вверх или вниз от первоначального положения
speed_wiggle_tower = 1		--скорость покачивания башни
ang_wiggle_tower = 1		--угол покачивания башни влево или вправо от первоначального положения
speed_rotation_clouds = 10	--скорость вращения облаков
--------------------------------------------------------------------------------
function OnSerialize(system)
	initPos_tower = system:Serialize(initPos_tower)
	initPos_tower_n = system:Serialize(initPos_tower_n)
	dist_move_tower_dt = system:Serialize(dist_move_tower_dt)
	ang_wiggle_tower_dt = system:Serialize(ang_wiggle_tower_dt)
end
--------------------------------------------------------------------------------
function OnFirstEntry()
	music:Stop(true)
	music:Play ("Mystery_Quest_scare", false)
	room.sfx:Play ("jingle_1")				--звук джингла при первом входе

	local tower_fly = room:GetItem("TOWER_FLY")
	local tower_fly_n = room:GetItem("N_WATERSPOUT")
	local clouds = room:GetItem("CLOUD_CIRCLE")
	
	initPos_tower = tower_fly:GetPosition()
	initPos_tower_n = tower_fly_n:GetPosition()
	dist_move_tower_dt = 0
	ang_wiggle_tower_dt = 0
	
	local dist_rot 	= ctypes:Vector2(initPos_tower.x + tower_fly:GetWidth() / 2, initPos_tower.y + tower_fly:GetHeight() / 2)
					- ctypes:Vector2(initPos_tower_n.x + tower_fly_n:GetWidth() / 2, initPos_tower_n.y + tower_fly_n:GetHeight() / 2)
	local rot_tower_fly_n = tower_fly_n:GetRotCenter()
	local new_rot_tower_fly_n = rot_tower_fly_n + dist_rot
	tower_fly_n:SetRotCenter(new_rot_tower_fly_n)
	
	stars:Show("TOWER_FLY", "4")
	stars:Show("WATERSPOUT", "4")
	
	if not(cstore:IsKeyExists ("Cheat_Wizard_tower")) then 	 --!!! (условие для читов)
		comment:Add("CMT_Explosion_Player")
		journal:Add("explosion")
		
		--вызов функции из кода
		room:RotateStart()
	end

end
--------------------------------------------------------------------------------
function OnEntry()
		if not (cstore:IsKeyExists("Explosion_Tower")) then
			if music:GetName() ~= "Mystery_Quest_scare" then
				music:Stop(true)
				music:PlayLoopWithDelay(0, "wizard_tower")
			end
			music:PlayLoopWithDelay(0, "wizard_tower")
			ambient:Play("Wizard's_tower_ext")
		else
			music:PlayLoopWithDelay(0, "wizard_tower")
			ambient:Play("Wizard's_Tower_ext_silence")
		end
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
	
	if itemName == "M_TO_DAM" then										--переход к дамбе					
		chapter:JumpRoom("Dam")
		return true
	elseif itemName == "TOWER_GROUND" then								--переход в башню	
		chapter:JumpRoom("Wizard's_tower_int")
		return true
	end
		

	if itemName == "TOWER_FLY" then						--башня в воздухе
		comment:Add("CMT_Tower_Player")
		return true	
	elseif itemName == "WATERSPOUT" then				--вихрь
		comment:Add("CMT_Waterspout_Player")
		return true	
	end
end--------------------------------------------------------------------------------
function OnUpdate(elapsedTime)
	local tower_fly = room:GetItem("TOWER_FLY")
	local tower_fly_n = room:GetItem("N_WATERSPOUT")
	
	if tower_fly:IsVisible() then
		
		--перемещение
		dist_move_tower_dt = dist_move_tower_dt + elapsedTime * speed_move_tower
		local pos_tower_y = dist_move_tower * (room:GetNoZoomSize()).y * math.sin(dist_move_tower_dt)
		local pos_tower = initPos_tower + ctypes:Vector2(0, pos_tower_y)
		tower_fly:SetPosition(pos_tower)
		local pos_tower_n = initPos_tower_n + ctypes:Vector2(0, pos_tower_y)
		tower_fly_n:SetPosition(pos_tower_n)
		
		--покачивание
		ang_wiggle_tower_dt = ang_wiggle_tower_dt + elapsedTime * speed_wiggle_tower
		local ang = ang_wiggle_tower * math.sin(ang_wiggle_tower_dt)
		tower_fly:SetRotateAngle(ang)
		tower_fly_n:SetRotateAngle(ang)
	end
end
--------------------------------------------------------------------------------
function OnExit()
	if music:GetName() == "wizard_tower" then
		music:Stop(true)
	end
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Explosion_Tower(value)						--открыть переход в башню
	room:GetItem("TOWER_FLY"):Hide()
	room:GetItem("N_WATERSPOUT"):Hide()
	room:GetItem("TOWER_GROUND"):Show()
	room:GetItem("WATER"):Hide()
	room:GetItem("WATERSPOUT"):Hide()
	room:GetItem("WATERSPOUT_PART"):Hide()
	room:GetItem("TOWERSPOUT_PART"):Hide()
	
	--вызов функции из кода
	room:RotateEnd()
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Cheat_Wizard_tower(value)			--выполнить все в локации  !!!
	cstore:SetKey("Explosion_Tower", true)
end
