
function OnSceneLoad(screen)
    
    play_field.game_state = game_state
	
    game_state = {}    
    LoadData(screen)        
    game_state.custom_cursors = {}
    game_state.locked_objects = {}
    game_state.active_objects = {}
    --level:AddTaskSafe(CreateTask('', '', ''))
    --zone = CreateZoomZone('')
    game_state.locker = ReferenceCounter()
    game_state.active_zone = nil    
    game_state.game_skip = false;
    game_state.screen = screen;
    game_state.timer = screen:GetTimer()
    game_state.right_rotated = nil;
    
    game_state.HELP_STRING = "HELP_MINIGAME_02";
    AddMiniGamesGUI(screen);
	
	game_state.locked_objects['bg1'] = true;
	game_state.locked_objects['border'] = true;
	
	z = screen:GetObject('border'):GetZ() + 1;
	
	game_state.elements_to_finish = 0
	
	--local pos1 = Vector(916, 94)
	local pos2 = Vector(916, 663)
	local pos3 = Vector(111, 94) 
	local pos4 = Vector(111, 663)
	
	Objects = {};
	
	RegisterElement(screen:GetObject('_pazl_1'), pos2, 1)
	RegisterElement(screen:GetObject('_pazl_2'), pos2, 2)
	RegisterElement(screen:GetObject('_pazl_3'), pos2, 3)
	RegisterElement(screen:GetObject('_pazl_4'), pos2, 4)
	RegisterElement(screen:GetObject('_pazl_5'), pos2, 5)
	RegisterElement(screen:GetObject('_pazl_8'), pos2, 6)
	RegisterElement(screen:GetObject('_pazl_9'), pos2, 7)
	RegisterElement(screen:GetObject('_pazl_10'), pos2, 8)
	RegisterElement(screen:GetObject('_pazl_11'), pos2, 9)
	RegisterElement(screen:GetObject('_pazl_12'), pos3, 10)
	RegisterElement(screen:GetObject('_pazl_13'), pos3, 11)
	RegisterElement(screen:GetObject('_pazl_14'), pos3, 12)
	RegisterElement(screen:GetObject('_pazl_15'), pos3, 13)
	RegisterElement(screen:GetObject('_pazl_16'), pos3, 14)
	RegisterElement(screen:GetObject('_pazl_17'), pos3, 15)
	RegisterElement(screen:GetObject('_pazl_18'), pos3, 16)
	RegisterElement(screen:GetObject('_pazl_19'), pos3, 17)
	RegisterElement(screen:GetObject('_pazl_20'), pos4, 18)
	RegisterElement(screen:GetObject('_pazl_21'), pos4, 19)
	RegisterElement(screen:GetObject('_pazl_23'), pos4, 20)
	RegisterElement(screen:GetObject('_pazl_24'), pos4, 21)
	RegisterElement(screen:GetObject('_pazl_25'), pos4, 22)
	RegisterElement(screen:GetObject('_pazl_26'), pos4, 23)
	RegisterElement(screen:GetObject('_pazl_27'), pos4, 24)
	RegisterElement(screen:GetObject('_pazl_28'), pos4, 25)	
	
	game_state.active_object = nil
		
	InitChain3(screen)		
	AddSheriffEyeLogic(screen)
	
	Stat = 0;

	screen:SetLayer(2)
end

function InitChain3(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_2_3"))
                                    
    comics1.OnShown = function()
        OnMinigameComicsShown(comics1)
    end
end

function OnScreenShown()
end

function IsComplited()
	for i = 1, 25 do
		if(Objects[i].Active == true) then
			return false;
		end
	end
	
	return true;
end

function RegisterElement(object, position, index)
	local name = object:GetName();
	
	Objects[index] = {}
	Objects[index].Obj = object;
	Objects[index].NeedPos = Vector(object:GetX(), object:GetY())
	if(index ~= 3 and index ~= 4 and index ~= 21 and index ~= 24 and index ~= 17) then
		Objects[index].Obj:SetX(position.x);
		Objects[index].Obj:SetY(position.y);
		Objects[index].Obj:SetAngle(math.random(0, 3) * M_PI_OVER_2);
		Objects[index].Active = true;
		Objects[index].Locked = false;
		Objects[index].Obj:SetZ(z);
	else
		Objects[index].Active = false;
		Objects[index].Locked = true;
	end
	
	Objects[index].Index = index;
 	z = z + 1;
end

function OnSceneFree(screen)
    FreeData(screen)
end

function OnUpdate(dt)
	CheckPuzzlePiecePos(DragObject)
	
    if DragObject then
        DragObject.Obj:SetXY(GetMouseX(), GetMouseY())
    else
		 if((oldMouseX ~= GetMouseX() or oldMouseY ~= GetMouseY()) and RotateObj ~= nil) then
			RotateObj = nil;
		end
    end
    oldMouseX = GetMouseX()
    oldMouseY = GetMouseY()
end

function OnExit()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function GetObjectByName(name)
	for i = 1, 25 do
		if(name == Objects[i].Obj:GetName()) then
			return Objects[i];
		end
	end
	
	return nil;
end 

function OnObjectMouseDown(object) 
	DefaultOnObjectMouseDown(object)
   
	if game_state.game_skip == true then
        return;
    end
    
    local object_name = object:GetName()
    local is_wrong_click = true
    local pf = game_state.screen
    
    if game_state.locked_objects[object_name] then
        return
    end
      
	Object = GetObjectByName(object_name);
	
	if Object ~= nil and Object.Active == true and Object.Locked == false then
		DragObject = Object;
		DragObject.Obj:SetZ(DragObject.Obj:GetZ() + 100)
	end
	
    --is_wrong_click = false;
    return is_wrong_click;
end

function OnObjectMouseUp(object)
    if DragObject then
		if(DragObject.Active == true and DragObject.Locked == false) then
			local Object = GetObjectByName(DragObject.Obj:GetName());
			--Object.Locked = true;
			--GTimerDoString('Objects['..Object.Index..'].Locked = false', 0.4);
			local found = false;
			found = CheckElementPos(DragObject.Obj:GetName());
			if not found then
				DragObject.Obj:SetZ(DragObject.Obj:GetZ() - 100);			
			end
		end

		DragObject = nil
	end
end

function OnAllPuzzlePiecesPlaced()
end

function OnObjectRightMouseDown(object)
    if game_state.game_skip == true then
        return;
    end
    
    local Object = GetObjectByName(object:GetName());
    
	if(RotateObj == nil and DragObject == nil) then
		if(Object == nil) then
			return;
		end
		RotateObj = Object;
		oldMouseX = GetMouseX();
		oldMouseY = GetMouseY();
	else
		if(DragObject) then
			Object = DragObject;
		else
			Object = RotateObj;
		end
	end
	
	if(Object ~= nil and (Object.Active == false or Object.Locked == true)) then
		return;
    end
    
	Object.Obj:SetXY(GetMouseX(), GetMouseY());
     
    local af = CreateAnimationEffector()
    
    af:AttachTo(Object.Obj)
    af:SetMode(_ANIM_FWD)
    af:SetKey(_TRACK_A, 0.0, Object.Obj:GetAngle())
    af:SetKey(_TRACK_A, 0.15, Object.Obj:GetAngle() + M_PI_OVER_2)
    af:Play()	
	
	Object.Locked = true;
	GTimerDoString('Objects['..Object.Index..'].Locked = false', 0.153);
	GTimerDoString('CorrectAngle("'..Object.Obj:GetName()..'")', 0.151);
	if(DragObject == nil or Object.Obj:GetName() ~= DragObject.Obj:GetName()) then
		GTimerDoString('CheckElementPos("'..Object.Obj:GetName()..'")', 0.152);
	end
end

game_state.active_object = nil;


--хака для исправления недоповорота на малых FPS
function CorrectAngle(element_name)
	if(element_name ~= nil) then
		local object = game_state.screen:GetObject(element_name)
		local angle = object:GetAngle();
		object:SetAngle(fix_angle(object:GetAngle()))
		object:SetAngle(_correct(angle, 0, 					M_PI_OVER_2));
		object:SetAngle(_correct(angle, M_PI_OVER_2, 		M_PI));
		object:SetAngle(_correct(angle, M_PI, 				M_PI + M_PI_OVER_2));
		object:SetAngle(_correct(angle, M_PI + M_PI_OVER_2, M_TWO_PI));
		object:SetAngle(fix_angle(object:GetAngle()))
	end
end

function _correct(angle, value1, value2)
	if (angle > value1 and angle < value2) then
		return value2;
	end
	
	return angle;
end 

function CheckElementPos(element_name)
    local object = GetObjectByName(element_name);
    
	if object ~= nil then
		object.Obj:SetAngle(fix_angle(object.Obj:GetAngle()))
		CorrectAngle(element_name);
		
		if math.abs(object.NeedPos.x - object.Obj:GetX()) < 30 and
				math.abs(object.NeedPos.y - object.Obj:GetY()) < 30 and object.Obj:GetAngle() == 0.0 then
			  
			object.Obj:SetX(object.NeedPos.x);
			object.Obj:SetY(object.NeedPos.y);
			object.Obj:SetZ(game_state.screen:GetObject('border'):GetZ() - object.Index);
			object.Active = false;
			
			if IsComplited() and  game_state.game_skip == false then
				ShowDoneButton()
				OnAllPuzzlePiecesPlaced()            
			end
			
			return true
		end
	end
	
	return false;
end

function OnObjectRightMouseUp(object)
end

function OnObjectWillBeFound(object)
end

function OnToolFound(tool)
end

function OnToolApply(tool, object)
end

function SkipMiniGame()
	if(game_state.game_skip == false) then
		game_state.game_skip = true
		
		for i = 1,25 do
			local obj = Objects[i].Obj
			obj:SetZ(obj:GetZ() + 100.0)
			local af = CreateAnimationEffector()        
			af:AttachTo(obj)
			af:SetMode(_ANIM_FWD)
			
			af:SetKey(_TRACK_X, 0.0, obj:GetX())
			af:SetKey(_TRACK_X, 1.0, Objects[i].NeedPos.x)
			af:SetKey(_TRACK_Y, 0.0, obj:GetY())        
			af:SetKey(_TRACK_Y, 1.0, Objects[i].NeedPos.y)        
			af:SetKey(_TRACK_A, 0.0, obj:GetAngle())
			af:SetKey(_TRACK_A, 1.0, 0)
			af:Play()        
		end
		
		TimerDo(game_state.timer, 1, OnAllPiecesReachedPositions);
		
		return 1.0;
	end
	
	return -1.0;
end

function OnObjectReachPanel(object)
end

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

function OnAllPiecesReachedPositions()
	for i = 1, 25 do
        Objects[i].Obj:SetZ(game_state.screen:GetObject('border'):GetZ() - 1);
        CheckElementPos(Objects[i].Obj:GetName());
    end
    OnAllPuzzlePiecesPlaced()
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/2_Puzzle/border.png',0,0,753,753,1,0)
	anim:SetHotSpot(376,376)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('border',anim,518,387,320,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/bg.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bg1',anim,512,384,190,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/pazl_28.png',0,0,77,279,1,0)
	anim:SetHotSpot(38,139)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('_pazl_28',anim,800,287,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/pazl_23.png',0,0,79,192,1,0)
	anim:SetHotSpot(39,96)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('_pazl_23',anim,220,246,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/pazl_24.png',0,0,254,438,1,0)
	anim:SetHotSpot(127,219)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('_pazl_24',anim,429,248,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/pazl_26.png',0,0,49,119,1,0)
	anim:SetHotSpot(24,59)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('_pazl_26',anim,696,256,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/pazl_27.png',0,0,103,272,1,0)
	anim:SetHotSpot(51,136)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('_pazl_27',anim,725,239,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/pazl_25.png',0,0,201,412,1,0)
	anim:SetHotSpot(100,206)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('_pazl_25',anim,627,254,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/pazl_1.png',0,0,209,46,1,0)
	anim:SetHotSpot(104,23)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('_pazl_1',anim,563,718,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/pazl_2.png',0,0,151,190,1,0)
	anim:SetHotSpot(75,95)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('_pazl_2',anim,554,126,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/pazl_3.png',0,0,90,179,1,0)
	anim:SetHotSpot(45,89)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('_pazl_3',anim,437,152,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/pazl_4.png',0,0,88,130,1,0)
	anim:SetHotSpot(44,65)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('_pazl_4',anim,386,250,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/pazl_5.png',0,0,227,94,1,0)
	anim:SetHotSpot(113,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('_pazl_5',anim,288,500,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/pazl_8.png',0,0,174,183,1,0)
	anim:SetHotSpot(87,91)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('_pazl_8',anim,661,192,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/pazl_9.png',0,0,84,201,1,0)
	anim:SetHotSpot(42,100)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('_pazl_9',anim,828,325,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/pazl_10.png',0,0,89,209,1,0)
	anim:SetHotSpot(44,104)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('_pazl_10',anim,205,363,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/pazl_11.png',0,0,73,285,1,0)
	anim:SetHotSpot(36,142)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('_pazl_11',anim,763,292,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/pazl_12.png',0,0,68,347,1,0)
	anim:SetHotSpot(34,173)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('_pazl_12',anim,294,279,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/pazl_13.png',0,0,70,328,1,0)
	anim:SetHotSpot(35,164)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('_pazl_13',anim,260,294,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/pazl_14.png',0,0,88,221,1,0)
	anim:SetHotSpot(44,110)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('_pazl_14',anim,717,329,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/pazl_15.png',0,0,185,120,1,0)
	anim:SetHotSpot(92,60)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('_pazl_15',anim,594,645,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/pazl_16.png',0,0,330,81,1,0)
	anim:SetHotSpot(165,40)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('_pazl_16',anim,418,507,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/pazl_17.png',0,0,328,64,1,0)
	anim:SetHotSpot(164,32)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('_pazl_17',anim,704,452,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/pazl_18.png',0,0,137,118,1,0)
	anim:SetHotSpot(68,59)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('_pazl_18',anim,259,592,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/pazl_19.png',0,0,603,167,1,0)
	anim:SetHotSpot(301,83)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('_pazl_19',anim,562,554,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/pazl_20.png',0,0,132,199,1,0)
	anim:SetHotSpot(66,99)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('_pazl_20',anim,729,601,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/2_Puzzle/pazl_21.png',0,0,259,175,1,0)
	anim:SetHotSpot(129,87)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('_pazl_21',anim,389,649,200,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,756,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,750,212,500,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
