function OnSceneLoad(screen)

	--Колличество штекеров
	PIN_AMOUNT = 5;
	
	MiniGameScreen = screen;
	play_field.game_state = game_state
	
    game_state = {}    
    LoadData(screen)
    local pf = play_field
    local level = pf:GetLevelScreen()
    game_state.custom_cursors = {}
    game_state.locked_objects = {}
    game_state.active_objects = {}
    game_state.locker = ReferenceCounter()
    game_state.active_zone = nil
    game_state.inventory_zones = {}
	game_state.active_objects = {}
	game_state.screen = screen;
	game_state.timer = screen:GetTimer()
	game_state.game_skip = false;
	game_state.HELP_STRING = "HELP_MINIGAME_10";
	
	Objects = {};
	Field = {}
	
	for i = 1,5 do
		Field[i] = {false, false, false, false, false}
	end
	
	AddObject(screen, "shteker_1", 2, 5, Vector(3, 3));
	AddObject(screen, "shteker_2", 2, 4, Vector(2, 4));
	AddObject(screen, "shteker_3", 3, 3, Vector(5, 3));
	AddObject(screen, "shteker_4", 4, 2, Vector(4, 2));
	AddObject(screen, "shteker_5", 5, 1, Vector(1, 1));
	
	InitStartPos();
	AddMiniGamesGUI(screen);
	
	InitChain1(screen)
    AddSheriffEyeLogic(screen)
    
    OldMouseX = 0;
	OldMouseY = 0;
			
	screen:SetLayer(2);
end

function InitChain1(screen)
    local comics1 = CreateComics(screen,                                                                  
                                 'Sheriff',
								 'eyes')
                                     
    comics1.OnHidden = function()
		game_state.locker:Release()    
		OnCompletedGame();
    end	               
    
    game_state.finish_dialog = CreateChain('pixel',
                                    screen,
                                    comics1, GetString("COMICS_10_6"))
                                    
    comics1.OnShown = function()
        OnMinigameComicsShown(comics1)
    end
end

function SkipMiniGame()
	if(game_state.game_skip == false) then
		game_state.game_skip = true;
		for i = 1,PIN_AMOUNT do
			Objects[i].Obj:Move(58 + 111 * Objects[i].NeedPos.x - Objects[i].Obj:GetX(), 125 * Objects[i].NeedPos.y + 38 - Objects[i].Obj:GetY(), 0.3);
		end
		
		return 0.5;
	end
	
	return -1.0;
end

function AddObject(screen, ObjName, len, Index, NeedPos)
	Objects[Index] = {};
	Objects[Index].Obj = screen:GetObject(ObjName);
	Objects[Index].Len = len;
	Objects[Index].Pos = Vector(0, 0);
	Objects[Index].OldPos = Vector(0, 0);
	Objects[Index].NeedPos = NeedPos;
	Objects[Index].Obj:SetZ(100 + Index);
	Objects[Index].Index = Index;
end

function InitStartPos()
	for i = 1,PIN_AMOUNT do
		local X;
		local Y;
		local len = Objects[i].Len;
		
		repeat
			Flag = false;
			X = math.random(5);
			Y = math.random(6 - len);
		until IsFree(X, Y, len) == true
		Objects[i].Pos.x = X;
		Objects[i].Pos.y = Y;
		
		Objects[i].Obj:SetXY(58 + 111 * X, 125 * Y + 38);
		
		for j = 0,len - 1 do
			Field[X][Y + j] = true;
		end
	end
end


function IsFree(X, Y, len)
	for i = Y, Y + len - 1 do
		if(Field[X] == nil or Field[X][i] ~= false) then
			return false;
		end
	end
	
	return true;
end

function GetObject(object)
	for i = 1,PIN_AMOUNT do
		if(Objects[i].Obj:GetName() == object:GetName()) then
			return Objects[i];
		end
	end
	return nil;
end

function StartDrag(object)
	if(game_state.compite == true) then
		return;
	end
	
	local Obj = GetObject(object);
	
	if(Obj ~= nil) then
		DragObject = Obj;
		DragObject.OldPos = DragObject.Pos;
		for i = Obj.Pos.y, (Obj.Pos.y + Obj.Len - 1) do
			Field[Obj.Pos.x][i] = false;
		end
		
		dX = GetMouseX() - DragObject.Obj:GetX();
		dY = GetMouseY() - DragObject.Obj:GetY();
		
		DragObject.Obj:SetZ(200);
		
		OldMouseX = GetMouseX();
		OldMouseY = GetMouseY();
	end
end

function IsComplite()
	local Counter = 0;
	
	for i = 1,PIN_AMOUNT do
		if(Objects[i].NeedPos.x ~= Objects[i].Pos.x or Objects[i].NeedPos.y ~= Objects[i].Pos.y) then
			if(Objects[i].Obj:GetName() == 'shteker_2' and Objects[5].NeedPos.x == Objects[i].Pos.x and Objects[5].NeedPos.y == Objects[i].Pos.y) then
				Counter = Counter + 1;
			else
				if(Objects[i].Obj:GetName() == 'shteker_1' and Objects[4].NeedPos.x == Objects[i].Pos.x and Objects[4].NeedPos.y == Objects[i].Pos.y) then
					Counter = Counter + 1;
				end
			end
		else
			Counter = Counter + 1;
		end
	end
	
	if(Counter == 5) then
		return true;
	else
		return false
	end
end

function UpdateDrag()
	if(DragObject ~= nil) then
		DragObject.Obj:SetXY(GetMouseX() - dX, GetMouseY() - dY);
	end
end

function StopDrag()
	if(DragObject ~= nil) then
		local X = math.floor((DragObject.Obj:GetX() - 2.5) / 111);
		local Y = math.floor((DragObject.Obj:GetY() + 25) / 125);
		
		if(IsFree(X, Y, DragObject.Len) == true) then
			DragObject.Obj:SetXY(58 + 111 * X, 125 * Y + 38);
			DragObject.Pos.x = X;
			DragObject.Pos.y = Y;
		else
			DragObject.Obj:SetXY(58 + 111 * DragObject.OldPos.x, 125 * DragObject.OldPos.y + 38);
			DragObject.Pos = DragObject.OldPos;
		end
		
		for j = DragObject.Pos.y, (DragObject.Pos.y + DragObject.Len - 1) do
			Field[DragObject.Pos.x][j] = true;
		end
		
		DragObject.Obj:SetZ(100 + DragObject.Index);
		DragObject = nil;
		
		if(OldMouseX ~= GetMouseX() or OldMouseY ~= GetMouseY()) then
			PlaySound("ToolSet");
			OldMouseX = 0;
			OldMouseY = 0;
		end
		
		if(IsComplite()) then
			game_state.compite = true;
			ShowDoneButton();
		end
	end
end

function OnSceneFree(screen)
    FreeData(screen)
end

function OnUpdate(dt)
	UpdateDrag();
end

function OnScreenShown()
end

function OnExit()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

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

    is_wrong_click = DefaultOnObjectMouseDown(object)
	
	StartDrag(object);
	
    if is_wrong_click then
        --pf:ShowWrongObjectEffect()
    end    
end

function OnObjectMouseUp(object)
	StopDrag();
end

function OnObjectFound(object)
end

function OnToolFound(tool)
end

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

function OnObjectReachPanel(object)
end

function OnButtonPress(title)
    DefaultOnButtonPress(title)
    OnMiniGameButtonPress(title);
end

function OnMouseMove(previous_pos, current_pos)
end

function OnSetTool(tool)
end

function CanBeHinted(obj)
    if locked_object[obj] ~= true then
        return true
    else
        return false
    end
end

function OnLostFocus()
end

function OnGainFocus()
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/10_ATS/ATS_diffuze_bak.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('ATS_diffuze_bak',anim,511,383,97,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/10_ATS/shteker_2.png',0,0,118,260,1,0)
	anim:SetHotSpot(13,56)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('shteker_2',anim,279,536,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/10_ATS/shteker_3.png',0,0,123,366,1,0)
	anim:SetHotSpot(14,37)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('shteker_3',anim,615,413,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/10_ATS/shteker_4.png',0,0,157,512,1,0)
	anim:SetHotSpot(49,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('shteker_4',anim,503,289,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/10_ATS/shteker_5.png',0,0,170,607,1,0)
	anim:SetHotSpot(13,39)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('shteker_5',anim,168,164,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/10_ATS/shteker_2.png',0,0,118,260,1,0)
	anim:SetHotSpot(13,56)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('shteker_1',anim,391,414,104,0,1,1)
	local anim = CreateAnimation('data/Texture/Comics/Sheriff.dds',0,0,530,636,1,0)
	anim:SetHotSpot(266,256)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Sheriff',anim,505,390,400,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('pixel',anim,512,384,501,0,128,96)
	local anim = CreateAnimation('data/Texture/Comics/eyes.png',0,0,79,33,1,0)
	anim:SetHotSpot(39,16)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('eyes',anim,499,212,500,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
