function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/06_Masks_lock.lua', 'quest', 'data/Scenes/06_Hotel_Street_Evening_Main.lua')

    LinkScene('area', 'CursorDoorDown', 'data/Scenes/06_Hotel_Street_Evening_Main.lua', false)
    --screen:GetObject('area_Library'):SetVisible(false);

	
    game_screen = screen;
	
	if HasGameState('MiniGameCompleted', 'Masks_Lock') == true then
		MiniGame = true;
		StartLevel('data/Scenes/06_Hotel_Street_Evening_Main.lua');
	else
		GameActive = true;
	
		skip_mini_game = false;
		MASKS_AMOUNT = 16;
		Objects = {};

		-- play_field:IgnoreOnTestPoint('BG_Library_Minigame');
		--play_field:GetObject('BG_Library_Minigame'):SetVisible(false);
		
		for i = 1,MASKS_AMOUNT do
			Objects['compose_'..i] = {};
			Objects['compose_'..i].Object = play_field:GetObject('compose_'..i);
			Objects['compose_'..i].RotateFlag = 2; 
			Objects['compose_'..i].Object:GetAnimation():Stop();
			Objects['compose_'..i].Object:GetAnimation():SetFrame(10);
			play_field:GetObject("eye_"..i):SetOpacity(0.0);
			play_field:IgnoreOnTestPoint("eye_"..i);
		end
		
		if not HasGameState('ToolApplied', '07_mask') then
			Objects['compose_'..6].Object:SetVisible(false);
			TimerDoString("ShowMonolog('06_MiniGame', 'C_06_MINI_GAME1')", 0.5, play_field:GetTimer())
			GameActive = false;
		end

		---[[
		RegisterActiveObject('area_2',
							 function()
								ShowMonolog('06_MiniGame', 'C_06_MINI_GAME1')
							 end,
							 nil,
							 true)
		--]]

		local on_mask_apply = function(tool_name, object_name)
			game_state.level_type = 'mini_game';
			play_field:GetLevelScreen():GetPanel():SetMode(PM_MINIGAME);
			play_field:GetObject("Plashka"):SetVisible(false);
			--play_field:GetObject("area"):SetVisible(false);
			TimerDoString("ShowMonolog('06_MiniGame', 'C_06_MINI_GAME2')", 0.5, play_field:GetTimer())
			play_field:GetObject("area_2"):SetVisible(false);
			Objects['compose_6'].Object:SetVisible(true);
			Objects['compose_6'].Object:GetAnimation():SetFrame(1);
			Objects['compose_6'].Object:GetAnimation():SetMode(HGEANIM_FWD);
			Objects['compose_6'].RotateFlag = 2;
			TimerDoString("Objects['compose_6'].Object:GetAnimation():Play();", 0.4, play_field:GetTimer())
			TimerDoString("GameActive = true;", 0.8, play_field:GetTimer())
			MakeSoundEntity('06_mask_added'):Play(false);
		end
		local on_load_when_mask_applied = function(tool_name)
			game_state.level_type = 'mini_game';
			play_field:GetLevelScreen():GetPanel():SetMode(PM_MINIGAME);
			play_field:GetObject("Plashka"):SetVisible(false);
			--play_field:GetObject("area"):SetVisible(false);
			play_field:GetObject("area_2"):SetVisible(false);
			TimerDoString("ShowMonolog('06_MiniGame', 'C_06_MINI_GAME2')", 0.5, play_field:GetTimer())
			Objects['compose_'..6].Object:SetVisible(true);
			GameActive = true;
			UnregisterActiveObject('area_2')
		end
		RegisterToolApplyInfo('07_mask', 'area_2', nil, nil, on_mask_apply, on_load_when_mask_applied)
		--play_field:GetObject("area"):AttachTo(RopeSimulation);
		
		LoadSound("RollMask", "data/Sound/06_Masks_lock/Roll_mask.ogg");
		LoadSound("06_mask_added", "data/Sound/06_Masks_lock/Mask_added.ogg");
		LoadSound("06_eyes_light", "data/Sound/06_Masks_lock/Eyes_light.ogg");
	end
	
	screen:SetLayer(ADDITIONAL_LAYER);
end

function eyeEffector(Obj)
	local af = CreateAnimationEffector()--AnimationEffector()
    af:AttachTo(Obj)
    af:SetKey(_TRACK_O, 0.0, 0.0)
    af:SetKey(_TRACK_O, 1.0, 1.0)
    af:SetKey(_TRACK_O, 2.0, 0.0)
    af:SetKey(_TRACK_O, 3.0, 1.0)
    af:Play()
end

function RotateObj(objectName)
	local obj = Objects[objectName];
	
	if(obj ~= nil) then
		if(Objects[objectName].RotateFlag == 1) then
			Objects[objectName].RotateFlag = 2;
			obj.Object:GetAnimation():SetMode(HGEANIM_FWD);
		else
			Objects[objectName].RotateFlag = 1;
			obj.Object:GetAnimation():SetMode(HGEANIM_REV);
		end
		obj.Object:GetAnimation():Play();
	end
	
	TimerDoString("CompliteGame()", 1.0, play_field:GetTimer());
end

function GetObjectIndex(object)
	for i = 1,MASKS_AMOUNT do
		if(object:GetName() == 'compose_'..i) then
			return i;
		end
	end
	
	return 0;
end

function IsComplite()
	for i = 1,MASKS_AMOUNT do
		if(Objects['compose_'..i].RotateFlag == 2) then
			return false;
		end
	end
	
	return true;
end

function CompliteGame()
	if(IsComplite()) then
		if HasGameState('MiniGameCompleted', 'Masks_Lock') == false then
			AddGameState('MiniGameCompleted', 'Masks_Lock');
			TimerDoString("FinishGame()", 0.4, game_screen:GetTimer())
		end
	end
end

function FinishGame()
	for i = 1,MASKS_AMOUNT do
		eyeEffector(play_field:GetObject("eye_"..i));
	end
	MiniGame = true;
	SetGameState('LastVisitedScene', 'data/Scenes/06_Hotel_Street_Evening_Main.lua')
	TimerDoString("StartLevel('data/Scenes/06_Hotel_Street_Evening_Main.lua');", 3.5, game_screen:GetTimer())
	MakeSoundEntity('06_eyes_light'):Play(false);
end

function OnSceneFree(screen)
    DefaultOnSceneFree()
    FreeData(screen)
end

function OnUpdate(dt)
    DefaultOnUpdate(dt)
   -- local Pos = RopeSimulation:GetPosition();
    --game_screen:GetObject("area"):SetScrXY(Pos.x, Pos.y);
end

function OnAllItemsFound()
    DefaultOnAllItemsFound()
end

function OnLose()
    DefaultOnLose()
end

function OnExit()
    DefaultOnExit()
end

function OnClose()
    DefaultOnClose()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function OnObjectMouseDown(object)
    local is_wrong_click = not DefaultOnObjectMouseDown(object)
    local obj_name = object:GetName()
    local pf = play_field

	if(Objects[obj_name] ~= nil and IsComplite() == false and skip_mini_game == false and GameActive == true) then
		local Index = GetObjectIndex(object);
		RotateObj('compose_'..Index);
		if((Index - 1) % 4 > 0) then
			RotateObj('compose_'..(Index - 1));
		end
		if((Index - 1) % 4 < 3) then
			RotateObj('compose_'..(Index + 1));
		end
		if(Index > 4) then
			RotateObj('compose_'..(Index - 4));
		end
		if(Index < MASKS_AMOUNT - 3) then
			RotateObj('compose_'..(Index + 4));
		end
		
		PlaySound("RollMask");
	end
	
    DefaultOnObjectMouseDownProcessed(is_wrong_click);
end

function SkipMiniGame()
	Objects['compose_'..6].Object:SetVisible(true);
	play_field:GetObject("Plashka"):SetVisible(false);
	play_field:GetObject("area"):SetVisible(false);
	
	if(skip_mini_game == false) then
		for i = 1,MASKS_AMOUNT do
			local obj = Objects['compose_'..i];
			if(obj.RotateFlag == 2) then
				obj.RotateFlag = 1;
				obj.Object:GetAnimation():SetMode(HGEANIM_REV);
				obj.Object:GetAnimation():Play();
			end
		end
		skip_mini_game = true;
		
		AddGameState('MiniGameCompleted', 'Masks_Lock');
		TimerDoString("FinishGame()", 1.4, game_screen:GetTimer())
	end
end

function OnObjectMouseUp(object)
	--RopeSimulation:SetActive(false);
    DefaultOnObjectMouseUp(object)
end

function OnObjectFound(object)
    DefaultOnObjectFound(object)
end

function OnToolFound(tool)
    DefaultOnToolFound(tool)
end

function OnToolApply(tool, object)
    DefaultOnToolApply(tool, object)
end

function OnObjectReachPanel(object)
    DefaultOnObjectReachPanel(object)
end

function OnButtonPress(title)
    DefaultOnButtonPress(title)
end

function OnMouseMove(previous_pos, current_pos)
	--RopeSimulation:SetXY(current_pos.x, current_pos.y);
    DefaultOnMouseMove(previous_pos, current_pos)
end

function OnSetTool(tool)
    DefaultOnSetTool(tool)
end

function LevelLoaded(level)
end

function OnSceneDestroy(playfield)
    DefaultOnSceneDestroy(playfield)
end

--------------------------------------------------------------------------------

-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function LoadData(screen)
	local anim = CreateAnimation('data/Texture/Minigames/Library/BG_Library_Minigame.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_Library_Minigame',anim,512,384,149,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/compose.png',0,0,95,95,11,20)
	anim:SetHotSpot(47,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('compose_1',anim,347,188,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/compose.png',0,0,95,95,11,20)
	anim:SetHotSpot(47,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('compose_2',anim,456,187,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/compose.png',0,0,95,95,11,20)
	anim:SetHotSpot(47,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('compose_3',anim,565,187,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/compose.png',0,0,95,95,11,20)
	anim:SetHotSpot(47,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('compose_4',anim,675,189,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/compose.png',0,0,95,95,11,20)
	anim:SetHotSpot(47,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('compose_5',anim,347,296,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/compose.png',0,0,95,95,11,20)
	anim:SetHotSpot(47,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('compose_6',anim,456,296,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/compose.png',0,0,95,95,11,20)
	anim:SetHotSpot(47,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('compose_7',anim,566,296,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/compose.png',0,0,95,95,11,20)
	anim:SetHotSpot(47,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('compose_8',anim,676,297,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/compose.png',0,0,95,95,11,20)
	anim:SetHotSpot(47,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('compose_9',anim,348,405,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/compose.png',0,0,95,95,11,20)
	anim:SetHotSpot(47,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('compose_10',anim,456,405,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/compose.png',0,0,95,95,11,20)
	anim:SetHotSpot(47,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('compose_11',anim,565,405,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/compose.png',0,0,95,95,11,20)
	anim:SetHotSpot(47,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('compose_12',anim,675,406,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/compose.png',0,0,95,95,11,20)
	anim:SetHotSpot(47,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('compose_13',anim,348,514,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/compose.png',0,0,95,95,11,20)
	anim:SetHotSpot(47,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('compose_14',anim,456,515,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/compose.png',0,0,95,95,11,20)
	anim:SetHotSpot(47,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('compose_15',anim,566,515,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/compose.png',0,0,95,95,11,20)
	anim:SetHotSpot(47,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('compose_16',anim,676,514,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/Layer_Eyes.png',0,0,110,69,1,0)
	anim:SetHotSpot(55,34)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('eye_1',anim,344,181,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/Layer_Eyes.png',0,0,110,69,1,0)
	anim:SetHotSpot(55,34)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('eye_2',anim,455,182,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/Layer_Eyes.png',0,0,110,69,1,0)
	anim:SetHotSpot(55,34)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('eye_3',anim,562,180,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/Layer_Eyes.png',0,0,110,69,1,0)
	anim:SetHotSpot(55,34)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('eye_4',anim,673,183,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/Layer_Eyes.png',0,0,110,69,1,0)
	anim:SetHotSpot(55,34)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('eye_5',anim,344,289,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/Layer_Eyes.png',0,0,110,69,1,0)
	anim:SetHotSpot(55,34)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('eye_6',anim,456,292,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/Layer_Eyes.png',0,0,110,69,1,0)
	anim:SetHotSpot(55,34)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('eye_7',anim,564,291,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/Layer_Eyes.png',0,0,110,69,1,0)
	anim:SetHotSpot(55,34)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('eye_8',anim,675,291,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/Layer_Eyes.png',0,0,110,69,1,0)
	anim:SetHotSpot(55,34)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('eye_9',anim,347,399,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/Layer_Eyes.png',0,0,110,69,1,0)
	anim:SetHotSpot(55,34)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('eye_10',anim,454,399,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/Layer_Eyes.png',0,0,110,69,1,0)
	anim:SetHotSpot(55,34)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('eye_11',anim,563,400,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/Layer_Eyes.png',0,0,110,69,1,0)
	anim:SetHotSpot(55,34)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('eye_12',anim,672,402,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/Layer_Eyes.png',0,0,110,69,1,0)
	anim:SetHotSpot(55,34)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('eye_13',anim,345,508,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/Layer_Eyes.png',0,0,110,69,1,0)
	anim:SetHotSpot(55,34)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('eye_14',anim,453,509,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/Layer_Eyes.png',0,0,110,69,1,0)
	anim:SetHotSpot(55,34)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('eye_15',anim,564,511,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Library/Layer_Eyes.png',0,0,110,69,1,0)
	anim:SetHotSpot(55,34)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('eye_16',anim,672,509,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area',anim,486,713,150,0,90,15)
	local anim = CreateAnimation('data/Texture/Minigames/Library/Plashka.png',0,0,105,105,1,0)
	anim:SetHotSpot(52,52)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Plashka',anim,456,296,150,0,0.92,0.92)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(3,3)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_2',anim,458,296,200,0,60,60)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
