
function OnSceneLoad(screen)

	play_field.game_state = game_state
	
	game_state.locked_objects['Bg1_01'] = true
	
    game_state = {}    
    
    game_state.elements_to_finish = 0
    
    LoadData(screen)
    local pf = play_field
    local level = pf:GetLevelScreen()
    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.screen = screen;
    game_state.game_skip = false;
    game_state.HELP_STRING = "HELP_MINIGAME_03";
	
	game_state.polar_coords = {}
	game_state.polar_coords.radius = 0
	game_state.polar_coords.angle = 0
	
	RegisterElement(game_state.screen:GetObject('Ring01'))
	RegisterElement(game_state.screen:GetObject('Ring02'))
	RegisterElement(game_state.screen:GetObject('Ring03'))
	RegisterElement(game_state.screen:GetObject('Ring04'))
	RegisterElement(game_state.screen:GetObject('Ring05'))
	
	game_state.timer = game_state.screen:GetTimer()
	AddMiniGamesGUI(screen);
		
	InitChain1(screen)	
	AddSheriffEyeLogic(screen)
		
	game_state.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_3_3"))
                                    
    comics1.OnShown = function()
        OnMinigameComicsShown(comics1)
    end
end

function OnScreenShown()
end

function RegisterElement(object)
    local element_data = {}
	if not game_state.elements then
        game_state.elements = {}
    end
	element_data.name = object:GetName()
	object:SetAngle(math.random(M_TWO_PI / 360 * 10, M_TWO_PI - M_TWO_PI / 360 * 10))	
	element_data.pos = {}
	element_data.pos.x = object:GetX()
	element_data.pos.y = object:GetY()
	table.insert(game_state.elements, element_data)
    game_state.elements_to_finish = game_state.elements_to_finish + 1
end

function OnObjectRightMouseDown(object)
end

function OnAllPiecesReachedPositions()
    ShowDoneButton()
end

function OnObjectRightMouseUp(object)
end

function OnSceneFree(screen)
    FreeData(screen)
end

function OnUpdate(dt)
	if game_state.active_object then
	    local old_angle = game_state.polar_coords.angle
	
	    local v = Vector(GetMouseX() - 512, GetMouseY() - 384)
	
	    game_state.polar_coords.radius = v:Length()
											           
	    game_state.polar_coords.angle = v:Angle(nil)
	    
	    --local obj_angle = game_state.active_object:GetAngle()
	    --local new_angle = obj_angle + game_state.polar_coords.angle - old_angle
        game_state.active_object:SetAngle(game_state.active_object.start_angle + v:Angle(nil) - game_state.active_object.start_dangle)
    end
end

function OnExit()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function OnObjectMouseDown(object)
	DefaultOnObjectMouseDown(object)
		
    local object_name = object:GetName()
	if game_state.locked_objects[object_name] then
        return
    end
    
    if game_state.elements and game_state.game_skip == false then
	    for i, v in ipairs(game_state.elements) do        
		    if object_name == v.name then
    		    game_state.active_object = game_state.screen:GetObject(v.name)
			    local v = Vector(GetMouseX() - 512, GetMouseY() - 384)
    		    game_state.active_object.start_angle = game_state.active_object:GetAngle()
				game_state.active_object.start_dangle = v:Angle(nil)
    		    break
		    end
        end
    end    
end

function OnObjectMouseUp(object)    
    if not game_state.active_object then
        return
    end
    for i, v in ipairs(game_state.elements) do
		if game_state.active_object:GetName() == v.name then
		    game_state.active_object:SetAngle(fix_angle(game_state.active_object:GetAngle()))
			if math.abs(M_TWO_PI - game_state.active_object:GetAngle()) <= M_PI / 180 * 5 or
			   math.abs(game_state.active_object:GetAngle()) <= M_PI / 180 * 5 then
			
				game_state.elements_to_finish = game_state.elements_to_finish - 1
				game_state.active_object:SetAngle(0)
				if game_state.elements_to_finish == 0  and game_state.game_skip == false then
				    ShowDoneButton();
				end
				game_state.locked_objects[game_state.active_object:GetName()] = true;				
                break
			end
		end
	end
	game_state.active_object = nil
end

function SkipMiniGame()
	if(game_state.game_skip == false) then
		game_state.game_skip = true
		local gathering_time = 1    
		for i, v in ipairs(game_state.elements) do
			local obj = game_state.screen:GetObject(v.name)
			obj:SetZ(obj:GetZ() + 100)
			
			local af = CreateAnimationEffector()        
			af:AttachTo(obj)
			af:SetMode(_ANIM_FWD)
			
			af:SetKey(_TRACK_X, 0.0, obj:GetX())
			af:SetKey(_TRACK_X, gathering_time, v.pos.x)
			af:SetKey(_TRACK_Y, 0.0, obj:GetY())        
			af:SetKey(_TRACK_Y, gathering_time, v.pos.y)        
			af:SetKey(_TRACK_A, 0.0, obj:GetAngle())
			af:SetKey(_TRACK_A, gathering_time, 0)
			af:Play()        
		end
		TimerDo(game_state.timer,
				gathering_time,
				OnAllPiecesReachedPositions)
			
		return 1.0;
	end
	
	return -1.0;
end

function OnObjectWillBeFound(object)
end

function OnToolFound(tool)
end

function OnToolApply(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/3_Circles/Bg1_01.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Bg1_01',anim,512,384,90,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/3_Circles/Ring01.png',0,0,628,628,1,0)
	anim:SetHotSpot(314,314)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Ring01',anim,512,384,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/3_Circles/Ring02.png',0,0,528,528,1,0)
	anim:SetHotSpot(264,264)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Ring02',anim,512,384,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/3_Circles/Ring03.png',0,0,428,428,1,0)
	anim:SetHotSpot(214,214)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Ring03',anim,512,384,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/3_Circles/Ring04.png',0,0,328,328,1,0)
	anim:SetHotSpot(164,164)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Ring04',anim,512,384,103,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/3_Circles/Ring05.png',0,0,226,226,1,0)
	anim:SetHotSpot(113,113)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Ring05',anim,512,384,104,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/3_Circles/Ring06.png',0,0,126,126,1,0)
	anim:SetHotSpot(63,63)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Ring06',anim,512,384,105,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,378,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,369,212,500,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
