class 'GameSceneBase' (BaseScreen)

function GameSceneBase:__init(level_name)
	BaseScreen.__init(self, level_name)
	current_scene = self
	GetScoreManager():setPopupTextsParent(self:getMainEntity())

	self.toolFoundScreen = nil

	if not TEMP_DONT_USE_NEW_TUTORIAL then
		self.tutorial = Tutorial(self, Tutorial.tips)
	end

	self.panel				 = Panel(self)
	self.achievments_manager = AchievmentsManager(self)
	self.music_manager		 = MusicManager(self)
    self.autoplayer 		 = AutoPlayer(self)

	self:registerAchievments()
	self:getInventory():postInit()		-- В postInit необходимо вынести все кросс-панельные обращения

	if not isBuildForCustomerMode() then
		if resourceExists('Data/Textures/Screens/IngameEditor/') then -- временно
			self.ingame_editor = IngameEditor(self)
			self.ingame_editor:disable()
			
			self:getWindow():getHotkeySignal(0x70):removeAll()
			self:getWindow():getHotkeySignal(0x70):add(function() self.ingame_editor:enable() end)

			self:getWindow():getHotkeySignal(0x72):removeAll()
			self:getWindow():getHotkeySignal(0x72):add(function() self.ingame_editor:disable() end)
		end
	end

	self:loadPlayField(level_name)		-- Создаём игровое поле

	-- Обновляю состояние дневника.
	if self.playfield:isNotebookActive() then
		self:getPanel():getControlsPanel():onNoteAdded()
	end

	if isCheatMode() then
		self:getWindow():getHotkeySignal( Hotkey_Ctrl(ERSK_K) ):add( completeSE )
		self:getWindow():getHotkeySignal( Hotkey_Ctrl(ERSK_L) ):add( completeCE )
	end
end
-------------------------------------------------------------------------------

function GameSceneBase:loadPlayField(levelName)
	--assert(not self.oldPlayfield)

	path_manager:startLevel(levelName)
	self.name = levelName

	if self:getTutorial() then
		self:getTutorial():hideTip()
	end

	if self.playfield then
		if self.onPFLoadedConnector then
			self.onPFLoadedConnector:remove()
			self.onPFLoadedConnector = nil
		end
		
		self:destroyOldPlayfield()

		self.oldPlayfield = self.playfield
		self.oldPlayfield:getMainEntity():setRespondable( false )
		self.oldPlayfield:getMainEntity():setActive		( false )
		self.oldPlayfield:getMainEntity():attachTo		( getGameRoot() )

		self:getFadeObject():show()
		self:getFadeObject():destroyEffectors()
		self:getFadeObject():ChangeOpacity(nil, 1, PLAYFIELD_FADE_IN_TIME)
		self:getFadeObject():setOrder( self:getInventory():getMainEntity():getOrder() - 0.1 )
	else
		self:getFadeObject():setRespondable(true)
		self:getFadeObject():setMaxOrder()
	end

	self.startPlayfieldTime = os.clock()

	local className = getLevelClassName(levelName)
	dofile(levels[className].path)

	self.playfield = Helpers.CallFunctionByName(className, self, levelName)
	self.playfield:onSceneStart()
	self.playfield:getMainEntity():setVisible( false )
	self.playfield:getMainEntity():setActive ( false )

	if not TEMP_USE_OLD_INVENTORY then
		local screen = self:getToolFoundScreen()
		if screen then
			screen:setAffectedPlayfield( self:getPlayField() )
		end
	end

	self:getMainEntity():setRespondable(false)

	local notification = getLastResourceLoadedNotification() -- self.playfield.lastResourceLoadedNotification
	if notification then
		self.onPFLoadedConnector = notification:add( bind(self.onPlayfieldLoaded, self) )
	else
		self:onPlayfieldLoaded()
	end

   self.music_manager:changeMusic()
   self.music_manager:changeAmbient()
end
-------------------------------------------------------------------------------

function GameSceneBase:onPlayfieldLoaded()
	self.onPFLoadedConnector = nil
	self:destroyOldPlayfield()

	self.playfield:getMainEntity():setActive ( true )
	self.playfield:getMainEntity():setVisible( true )

	self:getFadeObject():destroyEffectors()
	local eff = self:getFadeObject():ChangeOpacity(1, 0, PLAYFIELD_FADE_OUT_TIME)

	eff.onFinish:add(function()
						self:getMainEntity():setRespondable(true)
						self:getFadeObject():hide()
					 end)

	Console:pushText(string.format('Time loading %s: %.3f', self.playfield.name, os.clock() - self.startPlayfieldTime))
	self.startPlayfieldTime = nil
end
-------------------------------------------------------------------------------

function GameSceneBase:destroyOldPlayfield()
	if self.oldPlayfield then
		if self.ingame_editor then
			self.ingame_editor:on_scene_destroy()
		end

		self.oldPlayfield:destroy()
		self.oldPlayfield = nil

		collectgarbage('collect')
		collectgarbage('collect')
	end
end
-------------------------------------------------------------------------------

function GameSceneBase:createFadeObject()
	BaseScreen.createFadeObject(self)
	
	self:getFadeObject():setColor	( Colorf(0, 0, 0, 1) )
end
-------------------------------------------------------------------------------

function GameSceneBase:show(time)
	self.main_entity:setOpacity(1)
	self.main_entity:destroyEffectors()
	
	self:_activateMe()
end
-------------------------------------------------------------------------------

function GameSceneBase:hide(time)
	self.main_entity:setOpacity(0)
	self.main_entity:destroyEffectors()
	self.main_entity:setVisible(true)
	self.main_entity:setActive(true)
end
-------------------------------------------------------------------------------

function GameSceneBase:destroyScreen()
	BaseScreen.destroyScreen(self)
	GetScoreManager():setPopupTextsParent(nil)

	if self.isVolumeTurnDownMusic then
		self:volumeRestoreMusic()
	end
end
-------------------------------------------------------------------------------

function GameSceneBase:volumeTurnDownMusic()
	self.music_manager:volumeTurnDownMusic()
	self.isVolumeTurnDownMusic = true
end
-------------------------------------------------------------------------------

function GameSceneBase:volumeRestoreMusic()
	self.music_manager:volumeRestoreMusic()
	self.isVolumeTurnDownMusic = nil
end
-------------------------------------------------------------------------------

function GameSceneBase:volumeTurnDownInternoise()
	self.music_manager:volumeTurnDownInternoise()
	self.isVolumeTurnDownInternoise = true
end
-------------------------------------------------------------------------------

function GameSceneBase:volumeRestoreInternoise()
	self.music_manager:volumeRestoreInternoise()
	self.isVolumeTurnDownInternoise = false
end
-------------------------------------------------------------------------------

function GameSceneBase:setToolFoundScreen(screen)
	if TEMP_USE_OLD_INVENTORY then
		self.playfield.tool_found_window = screen
	else
		self.toolFoundScreen = screen
	end
end
-------------------------------------------------------------------------------

function GameSceneBase:getToolFoundScreen()
	if TEMP_USE_OLD_INVENTORY then
		return self.playfield.tool_found_window
	else
		return self.toolFoundScreen
	end
end
-------------------------------------------------------------------------------

function GameSceneBase:getTutorial()
	return self.tutorial
end
-------------------------------------------------------------------------------

function GameSceneBase:getPlayField()
	return self.playfield
end
-------------------------------------------------------------------------------

function GameSceneBase:getInventory()
	return self.panel:getInventory()
end
-------------------------------------------------------------------------------

function GameSceneBase:getHOPanel()
	return self.panel:getHOPanel()
end
-------------------------------------------------------------------------------

function GameSceneBase:getPanel()
	return self.panel
end
-------------------------------------------------------------------------------

function GameSceneBase:getAchievmentsManager()
	return self.achievments_manager
end
-------------------------------------------------------------------------------

function GameSceneBase:getMusicManager()
	return self.music_manager
end
-------------------------------------------------------------------------------

function GameSceneBase:getIngameEditor()
	return self.ingame_editor
end
-------------------------------------------------------------------------------

function GameSceneBase:getEntityNamed(name)
	local entity = self.window:getEntityNamed(name)
	assert(entity)
	return entity
end
-------------------------------------------------------------------------------

function GameSceneBase:hasEntityNamed(name)
	local entity = self.window:getEntityNamed(name)
	return (entity ~= nil)
end
-------------------------------------------------------------------------------

function GameSceneBase:getMaxOrder()
	local childs = self.window:getChilds()
	local max_order = 0
	for i = 0, childs:size() - 1 do
		local child_order = childs:get(i):getOrder()
		if child_order > max_order then
			max_order = child_order
		end
	end
	return max_order
end
-------------------------------------------------------------------------------

function GameSceneBase:getAutoPlayer()
    return self.autoplayer
end
-------------------------------------------------------------------------------
function GameSceneBase:playAmbientSound()
end
-------------------------------------------------------------------------------


-- Перегружается --------------------------------------------------------------
function GameSceneBase:registerAchievments()
end
-------------------------------------------------------------------------------


-------------------------------------------------------------------------------
-- Перегрузка этих двух методов для GameSceneBase. чтоб музыка не останавливалась, а постоянно обновлялалась. 
function GameSceneBase:stop()
	self.playfield:getMainEntity():setRespondable(false)
	self.playfield:getMainEntity():setActive(false)
	
	self.panel:getMainEntity():setRespondable(false)
	self.panel:getMainEntity():setActive(false)
end
-------------------------------------------------------------------------------

function GameSceneBase:go()
	if not self.playfield:getMouseBlocker():isHold() then
		self.playfield:getMainEntity():setRespondable(true)
	end
	self.playfield:getMainEntity():setActive(true)
	
	self.panel:getMainEntity():setRespondable(true)
	self.panel:getMainEntity():setActive(true)
end
-------------------------------------------------------------------------------