function OnSceneLoad(screen)
	parent_screen = "data/Scenes/36_Hell_System_Main.lua";
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/36_Piano_MG.lua', 'quest', parent_screen)
    
    LinkScene('area_exit', 'CursorDoorDown', parent_screen, false)
    
    LoadSound('36_A_Note', 'data/Sound/36_Hell_System/piano/A.ogg')
    LoadSound('36_B_Note', 'data/Sound/36_Hell_System/piano/B.ogg')
    LoadSound('36_C_Note', 'data/Sound/36_Hell_System/piano/C.ogg')
    LoadSound('36_D_Note', 'data/Sound/36_Hell_System/piano/D.ogg')
    LoadSound('36_E_Note', 'data/Sound/36_Hell_System/piano/E.ogg')
    LoadSound('36_F_Note', 'data/Sound/36_Hell_System/piano/F.ogg')
    LoadSound('36_G_Note', 'data/Sound/36_Hell_System/piano/G.ogg')
    
    LoadSound('36_Put_notes', 'data/Sound/36_Hell_System/piano/Put_notes.ogg')
    
    play_field.pianoNotes = {};
    play_field.pianoNotes[1] = MakeSoundEntity('36_C_Note');
    play_field.pianoNotes[2] = MakeSoundEntity('36_D_Note');
    play_field.pianoNotes[3] = MakeSoundEntity('36_E_Note');
    play_field.pianoNotes[4] = MakeSoundEntity('36_F_Note');
    play_field.pianoNotes[5] = MakeSoundEntity('36_G_Note');
    play_field.pianoNotes[6] = MakeSoundEntity('36_A_Note');
    play_field.pianoNotes[7] = MakeSoundEntity('36_B_Note');
    
    game_screen = screen;
	
	consecution = {6, 3, 5, 2, 4, 1, 3}
    current_step = 1;
    
    NOTES_AMOUNT = 7;
    
    Objects = {};
    for i = 1, NOTES_AMOUNT do
		Objects[i] = {}
		Objects[i].Index = i;
		
		Objects[i].Entity = screen:GetObject('note_'..i);
		Objects[i].Entity:SetOpacity(0.0);
		
		Objects[i].Target = screen:GetObject('target_'..i);
    end
    
    game_screen:GetObject('Layer_Notes'):SetVisible(false);
    
    for i = 1, 7 do
		game_screen:GetObject('red_light_'..i):SetVisible(false);
		game_screen:GetObject('green_light_'..i):SetVisible(false);
    end
    
	local on_mask_apply = function(tool_name, object_name)
		game_state.level_type = 'mini_game';
		play_field:GetLevelScreen():GetPanel():SetMode(PM_MINIGAME);
		game_screen:GetObject('area_note'):SetVisible(false);
		game_screen:GetObject('Layer_Notes'):SetVisible(true);
		game_screen:GetObject('Layer_Notes'):ChangeOpacity(0.0, 1.0, 0.4);
		ShowMonolog('C_36_PIANO_MG', 'C_36_PIANO_MG');
		
		MakeSoundEntity('36_Put_notes'):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);
		game_screen:GetObject('area_note'):SetVisible(false);
		game_screen:GetObject('Layer_Notes'):SetVisible(true);
		ShowMonolog('C_36_PIANO_MG', 'C_36_PIANO_MG');
	end
	RegisterToolApplyInfo('03_paper', 'area_note', nil, nil, on_mask_apply, on_load_when_mask_applied)
	
	if not HasGameState('ToolApplied', '03_paper')
	then
	    ShowMonolog('C_36_PIANO_MG_NO_NOTES', 'C_36_PIANO_MG_NO_NOTES');
	end
    
end

function SkipMiniGame()
	FinishMiniGame();
end

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

function OnUpdate(dt)
    DefaultOnUpdate(dt)
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 GetObjectByTarget(target)
	for i = 1, NOTES_AMOUNT do
		if(target:GetName() == Objects[i].Target:GetName()) then
			return Objects[i];
		end
	end
	
	return nil;
end

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

	if(HasGameState('ToolApplied', '03_paper')) then
		local Obj = GetObjectByTarget(object);
		
		if(Obj ~= nil) then
			Obj.Entity:ChangeOpacity(0.0, 1.0, 0.2);
			TimerDoString('Objects['..Obj.Index..'].Entity:ChangeOpacity(1.0, 0.0, 0.2);', 0.3, game_screen:GetTimer())
			
			play_field.pianoNotes[Obj.Index]:StopSound();
			play_field.pianoNotes[Obj.Index]:Play(false);
			
			if(consecution[current_step] == Obj.Index) then
				current_step = current_step + 1;
				game_screen:GetObject('green_light_'..Obj.Index):SetXY(game_screen:GetObject('knopka_'..Obj.Index):GetX(), game_screen:GetObject('knopka_'..Obj.Index):GetY());
				game_screen:GetObject('green_light_'..Obj.Index):SetVisible(true);
				TimerDoString('game_screen:GetObject("green_light_'..Obj.Index..'"):SetVisible(false);', 0.2, game_screen:GetTimer())
				if(current_step == 8) then
					FinishMiniGame();
				end
			else
				current_step = 1;
				game_screen:GetObject('red_light_'..Obj.Index):SetXY(game_screen:GetObject('knopka_'..Obj.Index):GetX(), game_screen:GetObject('knopka_'..Obj.Index):GetY());
				game_screen:GetObject('red_light_'..Obj.Index):SetVisible(true);
				TimerDoString('game_screen:GetObject("red_light_'..Obj.Index..'"):SetVisible(false);', 0.2, game_screen:GetTimer())
			end
		end
	else
		ShowMonolog('C_36_PIANO_MG_NO_NOTES', 'C_36_PIANO_MG_NO_NOTES');
	end
	
    DefaultOnObjectMouseDownProcessed(is_wrong_click);
end

function FinishMiniGame()
	if HasGameState('MiniGameCompleted', '36_Piano_MG') == false then
		AddGameState('MiniGameCompleted', '36_Piano_MG');
	end
	TimerDoString('StartLevel("data/Scenes/36_Hell_System_Main.lua");', 0.5, game_screen:GetTimer())
end

function OnObjectMouseUp(object)
    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)
    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/Piano/BG_notes.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_notes',anim,512,384,90,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/Layer_Notes.PNG',0,0,444,316,1,0)
	anim:SetHotSpot(222,158)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Notes',anim,513,217,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/A.jpg',0,0,225,245,1,0)
	anim:SetHotSpot(112,122)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('note_6',anim,671,546,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/C.jpg',0,0,225,245,1,0)
	anim:SetHotSpot(112,122)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('note_1',anim,271,546,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/D.jpg',0,0,225,245,1,0)
	anim:SetHotSpot(112,122)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('note_2',anim,331,546,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/E.jpg',0,0,225,245,1,0)
	anim:SetHotSpot(112,122)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('note_3',anim,431,546,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/F.jpg',0,0,225,245,1,0)
	anim:SetHotSpot(112,122)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('note_4',anim,511,546,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/G.jpg',0,0,225,245,1,0)
	anim:SetHotSpot(112,122)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('note_5',anim,611,546,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/H.jpg',0,0,225,245,1,0)
	anim:SetHotSpot(112,122)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('note_7',anim,741,546,100,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('target_1',anim,270,539,100,20,10,26)
	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('target_2',anim,351,548,100,20,10,26)
	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('target_3',anim,429,546,100,10,10,26)
	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('target_4',anim,505,541,100,0,10,26)
	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('target_5',anim,587,549,100,-15,10,26)
	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('target_6',anim,667,544,100,-20,10,26)
	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('target_7',anim,754,541,100,-20,10,26)
	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_note',anim,513,217,100,0,70,50)
	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_exit',anim,544,748,100,0,100,20)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/planka.png',0,0,495,60,1,0)
	anim:SetHotSpot(247,30)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('planka',anim,504,434,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/knopka.png',0,0,46,49,1,0)
	anim:SetHotSpot(23,24)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('knopka_1',anim,319,433,103,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/knopka.png',0,0,46,49,1,0)
	anim:SetHotSpot(23,24)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('knopka_2',anim,391,433,103,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/knopka.png',0,0,46,49,1,0)
	anim:SetHotSpot(23,24)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('knopka_3',anim,454,434,103,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/knopka.png',0,0,46,49,1,0)
	anim:SetHotSpot(23,24)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('knopka_4',anim,507,434,103,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/knopka.png',0,0,46,49,1,0)
	anim:SetHotSpot(23,24)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('knopka_5',anim,567,434,103,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/knopka.png',0,0,46,49,1,0)
	anim:SetHotSpot(23,24)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('knopka_6',anim,628,433,103,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/knopka.png',0,0,46,49,1,0)
	anim:SetHotSpot(23,24)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('knopka_7',anim,693,434,103,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/red_light.png',0,0,46,45,1,0)
	anim:SetHotSpot(23,22)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('red_light_1',anim,968,174,104,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/green_light.png',0,0,41,37,1,0)
	anim:SetHotSpot(20,18)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('green_light_1',anim,969,204,104,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/red_light.png',0,0,46,45,1,0)
	anim:SetHotSpot(23,22)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('red_light_2',anim,968,174,104,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/green_light.png',0,0,41,37,1,0)
	anim:SetHotSpot(20,18)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('green_light_2',anim,969,204,104,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/red_light.png',0,0,46,45,1,0)
	anim:SetHotSpot(23,22)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('red_light_3',anim,968,174,104,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/green_light.png',0,0,41,37,1,0)
	anim:SetHotSpot(20,18)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('green_light_3',anim,969,204,104,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/red_light.png',0,0,46,45,1,0)
	anim:SetHotSpot(23,22)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('red_light_4',anim,968,174,104,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/green_light.png',0,0,41,37,1,0)
	anim:SetHotSpot(20,18)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('green_light_4',anim,969,204,104,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/red_light.png',0,0,46,45,1,0)
	anim:SetHotSpot(23,22)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('red_light_5',anim,968,174,104,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/green_light.png',0,0,41,37,1,0)
	anim:SetHotSpot(20,18)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('green_light_5',anim,969,204,104,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/red_light.png',0,0,46,45,1,0)
	anim:SetHotSpot(23,22)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('red_light_6',anim,968,174,104,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/green_light.png',0,0,41,37,1,0)
	anim:SetHotSpot(20,18)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('green_light_6',anim,969,204,104,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/red_light.png',0,0,46,45,1,0)
	anim:SetHotSpot(23,22)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('red_light_7',anim,968,174,104,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Piano/green_light.png',0,0,41,37,1,0)
	anim:SetHotSpot(20,18)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('green_light_7',anim,969,204,104,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
