function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/28_Runes_lock.lua', 'quest', 'data/Scenes/28_Master_Room_Main.lua')
    --LinkScene('area', 'CursorDoorDown', 'data/Scenes/27_Master_Hall_Main.lua', false)
	LinkScene('area_exit', 'CursorDoorDown', 'data/Scenes/28_Master_Room_Main.lua', false)

	game_screen = screen;
	mini_game_completed = false;
	skip_game = false;
	
	mimigame_state = "none"
	
	screen:GetObject('Light_1'):SetVisible(false);
	screen:GetObject('Light_2'):SetVisible(false);
	
	------------------------------------------------------------------------------------------
	field_size_x = 4;
	field_size_y = 5;
	
	game_field = {}
	for i = 1, field_size_x do
		game_field[i] = {}
		for j = 1,field_size_y do
			game_field[i][j] = 0;
		end
	end
	
	------------------------------------------------------------------------------------------
	SLOTS_AMOUNT_X = 4;
	SLOTS_AMOUNT_Y = 5;
	Slots = {}
	
	for i = 1, SLOTS_AMOUNT_X do
		Slots[i] = {}
		for j = 1,SLOTS_AMOUNT_Y do
			Slots[i][j] = {}
			
			if(j < SLOTS_AMOUNT_Y) then
				Slots[i][j].Pos = screen:GetObject('Runa_'..(j + (i - 1) * (SLOTS_AMOUNT_Y - 1))):GetPosition();
			end
		end
	end	
	
	Slots[1][5].Pos = Vector(359, 554);
	Slots[2][5].Pos = Vector(461, 554);
	Slots[3][5].Pos = Vector(562, 554);
	Slots[4][5].Pos = Vector(663, 554);
	------------------------------------------------------------------------------------------
	OBJECTS_AMOUNT = 16;
	
	Objects = {}
	for i = 1,OBJECTS_AMOUNT do
		Objects[i] = {}
		Objects[i].Entity = screen:GetObject('Runa_'..i);
		
		local rand_x = 0;
		local rand_y = 0;
		repeat
			rand_x = math.random(4);
			rand_y = math.random(4);
		until game_field[rand_x][rand_y] == 0
		
		Objects[i].index_x = rand_x;
		Objects[i].index_y = rand_y;
		Objects[i].Entity:SetXY(Slots[rand_x][rand_y].Pos.x, Slots[rand_x][rand_y].Pos.y);
		Objects[i].Pos = Objects[i].Entity:GetPosition();
		Objects[i].index = i;
		game_field[rand_x][rand_y] = 1;
	end
	-------------------------------------------------------------------------------------------
	for i = 1, field_size_x do
		for j = 1,field_size_y do
			game_field[i][j] = 0;
		end
	end
		
	LoadGame();
	for i = 1,OBJECTS_AMOUNT do
		Objects[i].Entity:SetXY(Slots[Objects[i].index_x][Objects[i].index_y].Pos.x, Slots[Objects[i].index_x][Objects[i].index_y].Pos.y);
		Objects[i].Pos = Objects[i].Entity:GetPosition();
		game_field[Objects[i].index_x][Objects[i].index_y] = 1;
	end
	game_screen:GetObject('fuse'):SetVisible(false);
	SaveGame();
	
	local on_apply = function(tool_name, object_name)
		game_state.level_type = 'mini_game';
		play_field:GetLevelScreen():GetPanel():SetMode(PM_MINIGAME);
		game_screen:GetObject('fuse'):SetVisible(true);
		game_screen:GetObject('fuse'):ChangeOpacity(0.0, 1.0, 0.3);	
		mimigame_state	= "started"
		MakeSoundEntity('28_Put_fuse'):Play(false);
	end
	local on_load_when_applied = function(tool_name)
		game_state.level_type = 'mini_game';
		play_field:GetLevelScreen():GetPanel():SetMode(PM_MINIGAME);
		game_screen:GetObject('fuse'):SetVisible(true);
		mimigame_state	= "started"
	end
	RegisterToolApplyInfo('27_Fuse', 'area', nil, nil, on_apply, on_load_when_applied)
	
	if HasGameState('ToolApplied', '27_Fuse') == false then
		ShowMonolog('C_28_RUNES_MG_2', 'C_28_RUNES_MG_2');
	else
		ShowMonolog('C_28_RUNES_MG', 'C_28_RUNES_MG');
	end
	
	LoadSound('28_Put_rune', 'data/Sound/28_Master_Room/Runes_lock/Put_rune.ogg')
	LoadSound('28_Lights_on', 'data/Sound/28_Master_Room/Runes_lock/Lights_on.ogg')
	LoadSound('28_Put_fuse', 'data/Sound/28_Master_Room/Runes_lock/Put_fuse.ogg')
	
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 LoadGame()
	for i = 1, OBJECTS_AMOUNT do
		for k = 1, field_size_x do
			for l = 1, field_size_y do
				if HasGameState('MINI_GAME_RUNES_LOCK_STATE_'..i, ''..k..'_'..l) == true then
					 Objects[i].index_x = k;
					 Objects[i].index_y = l;
				end
			end
		end
	end
end

function SaveGame()
	for i = 1, OBJECTS_AMOUNT do
		if HasGameState('MINI_GAME_RUNES_LOCK_STATE_'..i, ''..Objects[i].index_x..'_'..Objects[i].index_y) == false then
			AddGameState('MINI_GAME_RUNES_LOCK_STATE_'..i, ''..Objects[i].index_x..'_'..Objects[i].index_y);
		end
	end
end


function GetObjectByName(obj_name)
	for i = 1, OBJECTS_AMOUNT do
		if(Objects[i].Entity:GetName() == obj_name) then
			return Objects[i];
		end
	end
	
	return nil;
end

function StartDrag(obj)
	DragObject = obj;
	game_field[DragObject.index_x][DragObject.index_y] = 0;
	DragObject.Entity:SetZ(200);
	
	if HasGameState('MINI_GAME_RUNES_LOCK_STATE_'..DragObject.index, ''..DragObject.index_x..'_'..DragObject.index_y) == true then
		RemoveGameState('MINI_GAME_RUNES_LOCK_STATE_'..DragObject.index, ''..DragObject.index_x..'_'..DragObject.index_y);
	end
end

function UpdateDrag()
	if(DragObject ~= nil) then
		DragObject.Entity:SetXY(GetMousePos().x, GetMousePos().y);
	end
end

function StopDrag(doAbort)
	if(DragObject ~= nil) then
		local set_flag = false;
		for i = 1, SLOTS_AMOUNT_X do
			for j = 1,SLOTS_AMOUNT_Y do
				if (set_flag == false and
                    math.abs(DragObject.Entity:GetX() - Slots[i][j].Pos.x) < 55 and
                    math.abs(DragObject.Entity:GetY() - Slots[i][j].Pos.y) < 55 and
                    game_field[i][j] == 0) and not doAbort
                then
					set_flag = true;
					DragObject.index_x = i;
					DragObject.index_y = j;
				end
			end
		end
		
		game_field[DragObject.index_x][DragObject.index_y] = 1;
		DragObject.Entity:Move(Slots[DragObject.index_x][DragObject.index_y].Pos.x - DragObject.Entity:GetX(),
                               Slots[DragObject.index_x][DragObject.index_y].Pos.y - DragObject.Entity:GetY(),
                               0.2);
		DragObject.Entity:SetZ(100);
		AddGameState('MINI_GAME_RUNES_LOCK_STATE_'..DragObject.index, ''..DragObject.index_x..'_'..DragObject.index_y);
		MakeSoundEntity('28_Put_rune'):Play(false);
		DragObject = nil;
		
		if(IsCompleted()) then
			FinishGame();
		end
	end
end

function IsCompleted()
	if(Objects[3].index_x ~= 1 or Objects[3].index_y ~= 5) then
		return false;
	end
	if(Objects[5].index_x ~= 2 or Objects[5].index_y ~= 5) then
		return false;
	end
	if(Objects[11].index_x ~= 3 or Objects[11].index_y ~= 5) then
		return false;
	end
	if(Objects[6].index_x ~= 4 or Objects[6].index_y ~= 5) then
		return false;
	end
	return true;
end

function FinishGame()
	if HasGameState('MiniGameCompleted', '28_Runes_lock') == false then
		AddGameState('MiniGameCompleted', '28_Runes_lock');
	end
	
	game_screen:GetObject('Light_1'):SetVisible(true);
	game_screen:GetObject('Light_2'):SetVisible(true);
	eyeEffector(game_screen:GetObject('Light_1'));
	eyeEffector(game_screen:GetObject('Light_2'));
	
	mini_game_completed = true;
	mini_game = true;
	
	MakeSoundEntity('28_Lights_on'):Play(false);
	
	TimerDoString("StartLevel('data/Scenes/28_Master_Room_Main.lua');", 1.5, game_screen:GetTimer())
end

function SkipMiniGame()
	if(skip_game == false) then
		FinishGame();
		skip_game = true;
	end
end
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function OnSceneFree(screen)
    DefaultOnSceneFree()
    FreeData(screen)
end

function OnUpdate(dt)
    DefaultOnUpdate(dt)
    UpdateDrag();
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
	
	local obj = GetObjectByName(obj_name);
	
	if(mimigame_state == "started") then
		if(obj ~= nil and mini_game_completed == false) then
			StartDrag(obj);
		end
	else
		ShowMonolog('C_28_RUNES_MG_2', 'C_28_RUNES_MG_2');
	end
	
    DefaultOnObjectMouseDownProcessed(is_wrong_click);
end

function OnObjectMouseUp(object)
    DefaultOnObjectMouseUp(object)
    StopDrag();
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/RunesLock/BG_Door_to_Daughter.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_Door_to_Daughter',anim,512,384,90,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/RunesLock/Light.png',0,0,68,164,1,0)
	anim:SetHotSpot(34,82)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Light_1',anim,192,401,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/RunesLock/Light.png',0,0,68,164,1,0)
	anim:SetHotSpot(34,82)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Light_2',anim,826,403,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/RunesLock/Runa_1.png',0,0,98,98,1,0)
	anim:SetHotSpot(49,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Runa_1',anim,361,89,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/RunesLock/Runa_2.png',0,0,98,98,1,0)
	anim:SetHotSpot(49,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Runa_2',anim,462,89,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/RunesLock/Runa_3.png',0,0,98,98,1,0)
	anim:SetHotSpot(49,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Runa_3',anim,562,89,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/RunesLock/Runa_4.png',0,0,98,98,1,0)
	anim:SetHotSpot(49,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Runa_4',anim,662,90,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/RunesLock/Runa_5.png',0,0,98,98,1,0)
	anim:SetHotSpot(49,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Runa_5',anim,362,189,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/RunesLock/Runa_6.png',0,0,98,98,1,0)
	anim:SetHotSpot(49,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Runa_6',anim,462,190,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/RunesLock/Runa_7.png',0,0,98,98,1,0)
	anim:SetHotSpot(49,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Runa_7',anim,562,191,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/RunesLock/Runa_8.png',0,0,98,98,1,0)
	anim:SetHotSpot(49,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Runa_8',anim,662,191,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/RunesLock/Runa_9.png',0,0,98,98,1,0)
	anim:SetHotSpot(49,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Runa_9',anim,361,289,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/RunesLock/Runa_10.png',0,0,98,98,1,0)
	anim:SetHotSpot(49,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Runa_10',anim,463,291,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/RunesLock/Runa_11.png',0,0,98,98,1,0)
	anim:SetHotSpot(49,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Runa_11',anim,563,289,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/RunesLock/Runa_12.png',0,0,98,98,1,0)
	anim:SetHotSpot(49,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Runa_12',anim,662,290,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/RunesLock/Runa_13.png',0,0,98,98,1,0)
	anim:SetHotSpot(49,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Runa_13',anim,361,392,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/RunesLock/Runa_14.png',0,0,98,98,1,0)
	anim:SetHotSpot(49,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Runa_14',anim,462,391,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/RunesLock/Runa_15.png',0,0,98,98,1,0)
	anim:SetHotSpot(49,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Runa_15',anim,563,390,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/RunesLock/Runa_16.png',0,0,97,97,1,0)
	anim:SetHotSpot(48,48)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Runa_16',anim,662,388,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/RunesLock/01.png',0,0,69,247,1,0)
	anim:SetHotSpot(34,123)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('fuse',anim,193,380,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('area',anim,198,397,100,0,20,35)
	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,486,713,350,0,90,15)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
