function OnSceneLoad(screen)
    LoadData(screen)
    parent_screen_name = "data/Scenes/36_Hell_System_Main.lua"
	DefaultOnSceneLoad('data/Scenes/32_Hell_System.lua', 'mini_game', parent_screen_name);
	
	_states = {}
	_states.screen = screen;
	_states.skip = false;
	_states.complited = false;
	
	OBJECTS_AMOUNT = 4;
	
	Objects = {}
	for i = 1, 4 do
		Objects[i] = {}
		Objects[i].Entity = _states.screen:GetObject('ring_'..i);
		Objects[i].Entity:SetAngle(math.random(360) * M_PI / 180);
		Objects[i].StartAngle = 0;
		Objects[i].StartDAngel = 0;
		Objects[i].Active = true;
		Objects[i].Index = i;
	end
	
	LoadGame();
	
	LoadSound('36_Rotate_circle', 'data/Sound/36_Hell_System/CircleLock/Rotate_circle.ogg')
	play_field.rotateCircleSound = MakeSoundEntity('36_Rotate_circle');
end

function OnSceneFree(screen)
    DefaultOnSceneFree()
    FreeData(screen)
end

------------------------------------------------------------------------------------------------------------------------------------------------------------

function LoadGame()
	for i = 1, OBJECTS_AMOUNT do
		if HasGameState('MINI_GAME_36_STATE', 'state_'..i) == true then
			Objects[i].Active = false;
			Objects[i].Entity:SetAngle(0);
		end
	end
end

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

function StartDrag(obj)
	DragObject = obj;
	DragObject.StartAngle = DragObject.Entity:GetAngle();
	DragObject.StartDAngle = Vector(MouseHolder:GetX() - 512, MouseHolder:GetY() - 384):Angle(nil);
	play_field.rotateCircleSound:Play(false);
end

function UpdateDrag()
	if(DragObject ~= nil) then    
	    DragObject.Entity:SetAngle(DragObject.StartAngle + Vector(MouseHolder:GetX() - 512, MouseHolder:GetY() - 384):Angle(nil) - DragObject.StartDAngle);
	    if(DragObject.Entity:GetAngle() < 0) then
			DragObject.Entity:SetAngle(DragObject.Entity:GetAngle() + M_TWO_PI);
	    end
	    
	    if(DragObject.Entity:GetAngle() > M_TWO_PI) then
			DragObject.Entity:SetAngle(DragObject.Entity:GetAngle() - M_TWO_PI);
	    end
	    
	    --[[
	    if not play_field.rotateCircleSound:IsPlaying()
        then
            play_field.rotateCircleSound:Play(false);
        end
        --]]
    else
        --[[
        if play_field.rotateCircleSound:IsPlaying()
        then
            play_field.rotateCircleSound:StopSound();
        end
        --]]
	end
	
end

function StopDrag()
	if(DragObject ~= nil) then
		if (math.abs(M_TWO_PI - DragObject.Entity:GetAngle()) <= M_PI / 180 * 5 or math.abs(DragObject.Entity:GetAngle()) <= M_PI / 180 * 5) then
			DragObject.Entity:SetAngle(0);
			DragObject.Active = false;
			
			if HasGameState('MINI_GAME_36_STATE', 'state_'..DragObject.Index) == false then
				AddGameState('MINI_GAME_36_STATE', 'state_'..DragObject.Index)
			end
			
			if(InCompleted() == true) then
				FinishMiniGame();
			end
		end
		DragObject = nil;
	end
end

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

function FinishMiniGame()
	if HasGameState('MiniGameCompleted', 'Hell_System_MG') == false then
		AddGameState('MiniGameCompleted', 'Hell_System_MG');
	end
	
	_states.complited = true;
	mini_game_36 = true;
	
	TimerDoString("StartLevel('"..parent_screen_name.."');", 0.5, _states.screen:GetTimer())
end

function SkipMiniGame()
	if(_states.skip == false and _states.complited == false) then
		_states.skip = true;
		
		--[[
		for i = 1, OBJECT_AMOUNT do
			Objects[i].Entity:Rotate(-Objects[i].Entity:GetAngle(), 0.2);
		end]]
		
		FinishMiniGame();
	end
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

	if(_states.skip == false and _states.complited == false) then
		local obj = GetObjectByName(obj_name);
		
		if(obj ~= nil and obj.Active == true) then
			StartDrag(obj);
		end
	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/Hell_System/BG.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG',anim,512,384,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Hell_System/cyrkle_1.png',0,0,549,549,1,0)
	anim:SetHotSpot(274,274)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('ring_1',anim,513,382,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Hell_System/cyrkle_2.png',0,0,431,431,1,0)
	anim:SetHotSpot(215,215)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('ring_2',anim,513,383,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Hell_System/cyrkle_3.png',0,0,321,321,1,0)
	anim:SetHotSpot(160,160)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('ring_3',anim,513,383,103,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Hell_System/cyrkle_4.png',0,0,188,188,1,0)
	anim:SetHotSpot(94,94)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('ring_4',anim,515,384,104,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
