--------------------------------------------------------------------------------
--  File     : Wizard's_tower_lab_Unit
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
battery_time = 1.0
--------------------------------------------------------------------------------
function OnSerialize(system)
	battery = system:Serialize(battery)
	battery_time_dt = system:Serialize(battery_time_dt)
end
--------------------------------------------------------------------------------
function OnFirstEntry()
	battery = 0
	battery_time_dt = 0

	stars:Show("M_CRYSTAL", "4")
end
--------------------------------------------------------------------------------
function OnMouseClickOnBack()
	chapter:ExitCloseup()
	return true
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()

	if 	itemName == "M_CRYSTAL" then
		comment:Add("CMT_Crystal_Player") 
		return true
	end
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)					
	local itemName = item:GetName()
	
	if itemName == "M_CRYSTAL" then
		if cursor:IsItemNameEqual("BATTERY") then			
			cursor:RemoveItem()
			item:Hide()

			room.sfx:Play ("use_power_cylinder")	--звук "вставляем кристалл"
			music:Stop(true)

			local battery_using = room:GetItem("BATTERY_USING")
			battery_using:Show()
			battery_using:PlayAnimation()
			room:AddCtrlAnimation("BATTERY_USING", -1)
			return true
		end
	end
end
--------------------------------------------------------------------------------
function OnUpdate (elapsedTime)

	local battery_using = room:GetItem("BATTERY_USING")
	local battery_part = room:GetItem("BATTERY_PART")
	local crystal_glow_part = room:GetItem("CRYSTAL_GLOW_PART")
	
	if battery_using:IsVisible() then
		if battery_using:IsAnimPlaying () then
			if not(battery_part:IsVisible()) then
				local frame = room:GetVideoFrame(battery_using:GetName())
				if frame >= 24 then
					battery_part:Show()
					battery_part:PlayAnimation()
					room:AddControlForParticleSys("BATTERY_PART")
				end
			end
			
			if not(crystal_glow_part:IsVisible()) then
				local frame = room:GetVideoFrame(battery_using:GetName())
				if frame >= 24 then
					crystal_glow_part:Show()
					crystal_glow_part:PlayAnimation()
					room:AddControlForParticleSys("CRYSTAL_GLOW_PART")
				end
			end
		end
	end
	
	--выход из клоузапа
	if battery == 1 then
		battery_time_dt = battery_time_dt + elapsedTime
		if battery_time_dt >= battery_time then
			battery = 2
			chapter:ExitCloseup()
			cstore:SetKey("Close_up_Unit", true)
		end
	end
end
--------------------------------------------------------------------------------
-- Окончание анимации батарейки на кристалл
function OnAnimationComplete_BATTERY_USING(forced)
	room:GetItem("BATTERY_USING"):Hide()
	room:MarkCompleted()
	
	if forced == false then
		battery = 1
	else
		cstore:SetKey("Close_up_Unit", true)
	end
end
