-- Код, который должен выполняться на уровнях

function LevelCommonFunction(message)
	if message:is("LoadLevel") or message:is("LoadSavedLevel") then
		LevelStartCommonFunction()
		
		local startFunc = "LevelStart_"..message:getData()
		if (_G[startFunc] ~= nil) then
			_G[startFunc]()
		end
		
		local updateFunc = "LevelUpdate_"..message:getData()
		if (_G[updateFunc] ~= nil) then
			gameInfo:game():SetLevelUpdateFunc(updateFunc)
		end
	end
end

-- Функция, которая выполняется во время загрузки любого уровня
-- до выполнения специфичной для этого уровня функции
function LevelStartCommonFunction()
	GUI:getLayer("Game"):getWidget("SkipLevel"):AcceptMessage(Message("HideNow"))
	GUI:getLayer("Game"):getWidget("SkipLevel"):AcceptMessage(Message("SetOnClickLuaHandler", ""))

	GUI:getLayer("Game"):getWidget("SkipSnowbattle"):AcceptMessage(Message("HideNow"))
end

messagesTable = {}
currentMsg = 0

function LevelStart_RE2_01_04()
	currentMsg = 0
	messagesTable = {
		[0] = { str = "CaveHuman1Dialog1", showTime = 1.0, hideTime = 3.0, visible = false, bubbleNum = 0 },
		[1] = { str = "CaveHuman1Dialog2", showTime = 3.5, hideTime = 5.5, visible = false, bubbleNum = 1 },
		[2] = { str = "CaveHuman1Dialog3", showTime = 6.0, hideTime = 8.0, visible = false, bubbleNum = 0 },
		[3] = { str = "CaveHuman1Dialog4", showTime = 8.5, hideTime = 10.5, visible = false, bubbleNum = 1 }
	}
	gameInfo:game():AddSpeachBubble(IPoint(182, 397))
	gameInfo:game():AddSpeachBubble(IPoint(538, 485))
end

function LevelUpdate_RE2_01_04()
	local msg = messagesTable[currentMsg]
	if (msg == nil) then
		gameInfo:game():SetLevelUpdateFunc("")
	else
		if (msg.visible == false and gameInfo:game():GetLuaUpdateFunctionTimer() > msg.showTime) then
			-- Если рабочие недоступны - то показываем облачко разговора
			local leftWorker = (gameInfo:game():isObjectReachable(59554) == false and msg.bubbleNum == 0)
			local rightWorker = (gameInfo:game():isObjectReachable(59556) == false and msg.bubbleNum == 1)
		
			if (leftWorker or rightWorker) then
				gameInfo:game():SetSpeachBubbleText(msg.bubbleNum, msg.str)
				gameInfo:game():ShowSpeachBubble(msg.bubbleNum)
				msg.visible = true
			else
				currentMsg = currentMsg + 1
			end
		elseif (msg.visible == true and gameInfo:game():GetLuaUpdateFunctionTimer() > msg.hideTime) then
			gameInfo:game():HideSpeachBubble(msg.bubbleNum)
			currentMsg = currentMsg + 1
		end
	end
end

-----------------------------------------------------------------------------------------------------------------
function LevelStart_RE2_08_07_b()
	if gameInfo:GetRuneLevelFailsCount() >= 1 then
		GUI:getLayer("Game"):getWidget("SkipLevel"):AcceptMessage(Message("Show"))
		GUI:getLayer("Game"):getWidget("SkipLevel"):AcceptMessage(Message("SetOnClickLuaHandler", "PlyPushSkipBonusLevelConfirm"))
	end
end

-----------------------------------------------------------------------------------------------------------------
function LevelStart_RE2_02_04()
	gameInfo:SetCurrentSnowbattle("Snowbattle1")
	gameInfo:SetSkipSnowbattle(false)
end

function LevelStart_RE2_09_04()
	gameInfo:SetCurrentSnowbattle("Snowbattle2")
	gameInfo:SetSkipSnowbattle(false)
end