function OnSceneLoad(screen)
    LoadData(screen)
	DefaultOnSceneLoad('data/Scenes/29_picture.lua', 'mini_game', 'data/Scenes/29_Daughter_Room_Main.lua')
	game_screen = screen;
	LinkScene('area', 'CursorDoorDown', 'data/Scenes/29_Daughter_Room_Main.lua', false)
	
	mini_game_completed = false;
	mini_game_active = true;
------------------------------------------------------------------------------------------
--Игровое поле
------------------------------------------------------------------------------------------
	field_size_x = 5;
	field_size_y = 4;
	
	GameField = {}
	for i = 1, field_size_x do
		GameField[i] = {}
		for j = 1, field_size_y do
			GameField[i][j] = 0;
		end
	end
------------------------------------------------------------------------------------------
--елементы пазла
------------------------------------------------------------------------------------------
	SIZE_X = 130;
	SIZE_Y = 129;
	
	OBJECTS_AMOUNT = 9;
	Objects = {}
	
	for i = 1, OBJECTS_AMOUNT do
		Objects[i] = {}
		Objects[i].Entity = game_screen:GetObject("Mask_"..i);
		Objects[i].size_x = math.floor(Objects[i].Entity:GetAnimation():GetWidth() / 150) + 1;
		Objects[i].size_y = math.floor(Objects[i].Entity:GetAnimation():GetHeight() / 150) + 1;
		Objects[i].last_move = 0;
		Objects[i].index = i;
	end
	
	Objects[1].Pos = Vector(1, 1);
	Objects[2].Pos = Vector(2, 1);
	Objects[3].Pos = Vector(1, 3);
	Objects[4].Pos = Vector(4, 1);
	Objects[5].Pos = Vector(5, 1);
	Objects[6].Pos = Vector(3, 3);
	Objects[7].Pos = Vector(3, 4);
	Objects[8].Pos = Vector(4, 3);
	Objects[9].Pos = Vector(4, 4);
	
	left_corner_x = 200;
	left_corner_y = 110;
	
	--Разкоментировать для сохранения текущего статуса
	--LoadGame();
	
	for i = 1, OBJECTS_AMOUNT do
		for k = Objects[i].Pos.x,Objects[i].Pos.x + Objects[i].size_x - 1 do
			for l = Objects[i].Pos.y,Objects[i].Pos.y + Objects[i].size_y - 1 do
				GameField[k][l] = 1;
			end
		end
		Objects[i].Entity:SetXY((Objects[i].Pos.x  - 1) * SIZE_X + left_corner_x, (Objects[i].Pos.y - 1) * SIZE_Y + left_corner_y);
	end
	
	ShowMonolog('C_29_PICTURE_MG', 'C_29_PICTURE_MG');
	
	LoadSound('29_MovePictureIcon', 'data/Sound/29_Daughter_Room/MovePictureIcon.ogg');
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_PICTURE_STATE_'..i, ''..k..'_'..l) == true then
					 Objects[i].Pos.x = k;
					 Objects[i].Pos.y = l;
				end
			end
		end
	end
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 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 IsMove(obj)
	if(IsMovedLeft(obj) == true) then
		return;
	end
	if(IsMovedRight(obj) == true) then
		return;
	end
	if(IsMovedTop(obj) == true) then
		return;
	end
	if(IsMoveBottom(obj) == true) then
		return;
	end	
end

function IsMoveReverce(obj)
	if(IsMoveBottom(obj) == true) then
		return;
	end
	if(IsMovedTop(obj) == true) then
		return;
	end
	if(IsMovedRight(obj) == true) then
		return;
	end
	if(IsMovedLeft(obj) == true) then
		return;
	end	
end     

function IsMovedLeft(obj)
	if(GameField[obj.Pos.x - 1] ~= nil) then
		for j = obj.Pos.y, obj.Pos.y + obj.size_y - 1 do
			if(GameField[obj.Pos.x - 1][j] ~= 0) then
				return false;
			end
		end

		ElementMove(obj, 1);--едим влево
		return true;
	end
end

function IsMovedRight(obj)
	if(GameField[obj.Pos.x + 1] ~= nil) then
		for j = obj.Pos.y, obj.Pos.y + obj.size_y - 1 do
			if(GameField[obj.Pos.x + obj.size_x] == nil or GameField[obj.Pos.x + obj.size_x][j] ~= 0) then
				return false;
			end
		end
		
		ElementMove(obj, 2);--едим вправо
		return true;
	end
end

function IsMovedTop(obj)
	for i = obj.Pos.x, obj.Pos.x + obj.size_x - 1 do
		if(GameField[i][obj.Pos.y - 1] ~= 0) then
			return false;
		end
	end
	
	ElementMove(obj, 3);--едим вверх
	return true;
end


function IsMoveBottom(obj)
	for i = obj.Pos.x, obj.Pos.x + obj.size_x - 1 do
		if(GameField[i][obj.Pos.y + obj.size_y] ~= 0) then
			return false;
		end
	end
	
	ElementMove(obj, 4);--едим вниз
	return true;

end

function PlayIconMoveSound()

    if not play_field.moveIconSound
    then
        play_field.moveIconSound = MakeSoundEntity('29_MovePictureIcon');
    end

    play_field.moveIconSound:StopSound();
    play_field.moveIconSound:Play(false);

end

function ElementMove(obj, index)
	if(index == obj.last_move)then
		obj.last_move = 0;
		IsMoveReverce(obj);
		return;
	end
	
	PlayIconMoveSound()
	
	mini_game_active = false;
	TimerDoString('mini_game_active = true', 0.25, game_screen:GetTimer())
	
	if HasGameState('MINI_GAME_PICTURE_STATE_'..obj.index, ''..obj.Pos.x..'_'..obj.Pos.y) == true then
		RemoveGameState('MINI_GAME_PICTURE_STATE_'..obj.index, ''..obj.Pos.x..'_'..obj.Pos.y);
	end
		
	for k = obj.Pos.x,obj.Pos.x + obj.size_x - 1 do
		for l = obj.Pos.y,obj.Pos.y + obj.size_y - 1 do
			GameField[k][l] = 0;
		end
	end
	
	if(index == 1) then
		obj.Entity:Move(-SIZE_X, 0.0, 0.2);
		obj.Pos.x = obj.Pos.x - 1;
		obj.last_move = 2;
	end
	if(index == 2) then
		obj.Entity:Move(SIZE_X, 0.0, 0.2);
		obj.Pos.x = obj.Pos.x + 1;
		obj.last_move = 1;
	end
	if(index == 3) then
		obj.Entity:Move(0.0, -SIZE_Y, 0.2);
		obj.Pos.y = obj.Pos.y - 1;
		obj.last_move = 4;
	end
	if(index == 4) then
		obj.Entity:Move(0.0, SIZE_Y, 0.2);
		obj.Pos.y = obj.Pos.y + 1;
		obj.last_move = 3;
	end
	
	for k = obj.Pos.x,obj.Pos.x + obj.size_x - 1 do
		for l = obj.Pos.y,obj.Pos.y + obj.size_y - 1 do
			GameField[k][l] = 1;
		end
	end
	
	AddGameState('MINI_GAME_PICTURE_STATE_'..obj.index, ''..obj.Pos.x..'_'..obj.Pos.y);
	
	if(IsCompleted()) then
		FinishGame();
	end
end

function IsCompleted()
	if(Objects[3].Pos.x == 4 and Objects[3].Pos.y == 1) then
		return true;
	end
end

function FinishGame()
	if HasGameState('MiniGameCompleted', '29_Picture') == false then
		AddGameState('MiniGameCompleted', '29_Picture');
	end
				
	if(mini_game_completed == false) then
		TimerDoString("StartLevel('data/Scenes/29_Daughter_Room_Main.lua');", 1.0, game_screen:GetTimer())
		mini_game_completed = true;
		mini_game = true;
	end
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(obj ~= nil and mini_game_completed == false and mini_game_active == true) then
		IsMove(obj);
	end
	
    DefaultOnObjectMouseDownProcessed(is_wrong_click);
end

function SkipMiniGame()
	if HasGameState('MiniGameCompleted', '29_Picture') == false then
		AddGameState('MiniGameCompleted', '29_Picture');
	end
				
	if(mini_game_completed == false) then
		TimerDoString("StartLevel('data/Scenes/29_Daughter_Room_Main.lua');", 0.5, game_screen:GetTimer())
		mini_game_completed = true;
		mini_game = true;
	end
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/Picture/BG_Picture.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_Picture',anim,512,384,90,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,711,150,0,90,15)
	local anim = CreateAnimation('data/Texture/Minigames/Picture/Mask_1.png',0,0,140,270,1,0)
	anim:SetHotSpot(0,0)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Mask_1',anim,204,139,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Picture/Mask_2.png',0,0,140,270,1,0)
	anim:SetHotSpot(0,0)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Mask_2',anim,334,141,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Picture/Mask_3.png',0,0,270,270,1,0)
	anim:SetHotSpot(0,0)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Mask_3',anim,204,397,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Picture/Mask_4.png',0,0,140,270,1,0)
	anim:SetHotSpot(0,0)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Mask_4',anim,590,138,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Picture/Mask_5.png',0,0,140,270,1,0)
	anim:SetHotSpot(0,0)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Mask_5',anim,720,136,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Picture/Mask_6.png',0,0,140,140,1,0)
	anim:SetHotSpot(0,0)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Mask_6',anim,459,397,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Picture/Mask_7.png',0,0,140,140,1,0)
	anim:SetHotSpot(0,0)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Mask_7',anim,461,526,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Picture/Mask_8.png',0,0,270,140,1,0)
	anim:SetHotSpot(0,0)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Mask_8',anim,590,398,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Picture/Mask_9.png',0,0,270,140,1,0)
	anim:SetHotSpot(0,0)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Mask_9',anim,591,526,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Picture/Layer_Cristall.PNG',0,0,109,97,1,0)
	anim:SetHotSpot(54,48)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Cristall',anim,846,139,100,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
