local zone_well

function add_fibrillation_effector_door_light(obj, delay)
	local duration = 7.0

	eff= CreateAnimationEffector()
	eff:AttachTo(obj)
	eff:SetMode(_ANIM_LOOP)

    local dopacity = -0.

	eff:SetKey(_TRACK_O, 0.0 + delay, 1.0 + dopacity)
	eff:SetKey(_TRACK_O, duration * 1.0/4.0 + delay, 0.75 + dopacity)
	eff:SetKey(_TRACK_O, duration * 2.0/4.0 + delay, 1.0 + dopacity)
	eff:SetKey(_TRACK_O, duration * 3.0/4.0 + delay, 0.59 + dopacity)
	eff:SetKey(_TRACK_O, duration * 4.0/4.0 + delay, 1.0 + dopacity)
	eff:Play()
end

function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/30_Road_Fork_Main.lua', 'quest')

	LinkScene('area_28', 'CursorDoorRightUp','data/Scenes/28_Master_Room_Main.lua')
	LinkScene('area_32', 'CursorDoorLeftUp','data/Scenes/32_Lift_Main.lua')
	
	LoadSound('30_Attaching_rope_ladder', 'data/Sound/30_Road fork/Attaching_rope_ladder.ogg')
	LoadSound('30_Torch_burning', 'data/Sound/30_Road fork/Torch_burning.ogg')

	zone_well = CreateZoomZone('area_well',
							   'CloseZoneWell',
							   'BG_Drraw_well', true,
							   'Layer_Ladder', false,
							   'area_ladder', true)
	play_field:GetObject('area_ladder'):SetMaxOpacity(0.001)

	local eff = CreateAnimationEffector()
	eff:AttachTo(play_field:GetObject('Layer_Chain_L'))
	eff:SetMode(_ANIM_LOOP)
	eff:SetKey(_TRACK_A, 0.0, -0.01)
	eff:SetKey(_TRACK_A, 4.0, 0.01)
	eff:SetKey(_TRACK_A, 8.0, -0.01)
	eff:Play()

	local eff = CreateAnimationEffector()
	eff:AttachTo(play_field:GetObject('Layer_Chain_R'))
	eff:SetMode(_ANIM_LOOP)
	eff:SetKey(_TRACK_A, 0.0, -0.01)
	eff:SetKey(_TRACK_A, 3.0, 0.01)
	eff:SetKey(_TRACK_A, 6.0, -0.01)
	eff:Play()

	add_fibrillation_effector_door_light(play_field:GetObject('Light_Entrance'), 0.)

	RegisterMonologObject('area_ladder', 'C_30_DEEP_WELL')
	RegisterMonologObject('area_dialog', 'C_30_SHADOW', true)

	local on_ladder_apply = function()
		zone_well.SetObjectVisible('Layer_Ladder', true)
		play_field:GetObject('Layer_Ladder'):ChangeOpacity(0., 1., 0.4)
		UnregisterActiveObject('area_ladder')
		LinkScene('area_ladder', 'CursorDoorDown','data/Scenes/31_Draw_Well_Main.lua')
		MakeSoundEntity('30_Attaching_rope_ladder'):Play(false);
	end
	local on_load_when_ladder_applied = function()
		zone_well.SetObjectVisible('Layer_Ladder', true)
		UnregisterActiveObject('area_ladder')
		LinkScene('area_ladder', 'CursorDoorDown','data/Scenes/31_Draw_Well_Main.lua')
	end
	RegisterToolApplyInfo('29_rope_ladder', 'area_ladder', nil, nil, on_ladder_apply, on_load_when_ladder_applied)

	if HasGameState('ToolFound', '31_bulb_full') then
		game_state.ignore_on_highlight['area_ladder'] = true
		game_state.ignore_on_highlight['area_well'] = true
	end

	for i=1, 7
    do
	    local eff = MegoLightEffector();
	
	    eff:AttachTo(play_field:GetObject('FireRadialGlow'..i));
	    eff:PlayAt(math.random());
	end
	
	MakeSoundEntity('30_Torch_burning'):Play(true);
	
	AddRatsComposition()
end

function AddRatsComposition()

    createComposition17_Theatre_Entrance_rat = function()
    
        local composition17_Theatre_Entrance_rat = CreateCompositionSequence();
        local compositionDuration = 2.16666666666667;
        composition17_Theatre_Entrance_rat:SetDuration(compositionDuration);
    
    
        -- Layer Rat_eating
        local element = BuildRatEatingComposition();
        element:SetName("Rat_eating");
        composition17_Theatre_Entrance_rat:AddElement(element);
        element:SetHotSpot(512, 384);
        element:SetParentElement(composition17_Theatre_Entrance_rat);
        element:SetXY(199.02880859375, 610.112228393555);
    
        element:SetScaleX(0.19143884892086);
        element:SetScaleY(0.22597122302158);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 3.5);
    
    
        effector:Play();
    
    
        -- Layer Layer_2
        local element = CreateCompositionElement("data/Texture/Scenes/30_Road_Fork/Zone_Main/Layers/mask_30_1.png");
        element:SetName("Layer_2");
        composition17_Theatre_Entrance_rat:AddElement(element);
        element:SetHotSpot(135, 87.5);
        element:SetParentElement(composition17_Theatre_Entrance_rat);
        element:SetXY(123.02734375, 540.020141601563);
    
        element:SetScaleX(1);
        element:SetScaleY(1);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 60);
    
    
        effector:Play();
    
    
        -- Layer Layer_3
        local element = CreateCompositionElement("data/Texture/Scenes/30_Road_Fork/Zone_Main/Layers/mask_30_2.png");
        element:SetName("Layer_3");
        composition17_Theatre_Entrance_rat:AddElement(element);
        element:SetHotSpot(89.5, 103.5);
        element:SetParentElement(composition17_Theatre_Entrance_rat);
        element:SetXY(345, 614);
    
        element:SetScaleX(1);
        element:SetScaleY(1);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 60);
    
    
        effector:Play();
    
    
        composition17_Theatre_Entrance_rat:Play();
    
        return composition17_Theatre_Entrance_rat;
    end
    
    local createCompositionRatRunning1 = function()
    
        local compositionRatRunning1 = CreateCompositionSequence();
        local compositionDuration = 3.5;
        compositionRatRunning1:SetDuration(compositionDuration);
        
        -- Layer Rat_running
        local element = BuildRatRunningComposition();
        element:SetName("Rat_running");
        compositionRatRunning1:AddElement(element);
        element:SetHotSpot(512, 384);
        element:SetParentElement(compositionRatRunning1);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0.625, 5.625);
    
        effector:SetKey(_TRACK_X, 0.625, 395.896377563477);
        effector:SetKey(_TRACK_Y, 0.625, 575.916549682617);
        effector:SetKey(_TRACK_X, 1.04166666666667, 317.662401753038);
        effector:SetKey(_TRACK_Y, 1.04166666666667, 525.637191500941);
        effector:SetKey(_TRACK_X, 1.5916748046875, 203.976974487305);
        effector:SetKey(_TRACK_Y, 1.5916748046875, 516.854675292969);
        effector:SetKey(_TRACK_SX, 0.625, 0.19);
        effector:SetKey(_TRACK_SY, 0.625, 0.19);
        effector:SetKey(_TRACK_SX, 1.58333333333333, 0.11);
        effector:SetKey(_TRACK_SY, 1.58333333333333, 0.11);
        effector:SetKey(_TRACK_A, 0.625, 0.4931506050842);
        effector:SetKey(_TRACK_A, 1.04166666666667, 0.4931506050842);
        effector:SetKey(_TRACK_A, 1.25, 0.187012799173);
    
        effector:Play();
        
        --compositionRatRunning1:Play();
        
        return compositionRatRunning1;
    end
    
    local createCompositionRatRunning2 = function()
    
        local compositionRatRunning2 = CreateCompositionSequence();
        local compositionDuration = 2.5;
        compositionRatRunning2:SetDuration(compositionDuration);
        
        -- Layer Rat_running
        local element = BuildRatRunningComposition();
        element:SetName("Rat_running_2");
        compositionRatRunning2:AddElement(element);
        element:SetHotSpot(512, 384);
        element:SetParentElement(compositionRatRunning2);
        element:SetAngle(0.29593767482492);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 5);
    
        effector:SetKey(_TRACK_X, 0, 168.258972167969);
        effector:SetKey(_TRACK_Y, 0, 486.408630371094);
        effector:SetKey(_TRACK_X, 0.33333333333333, 214.848697383867);
        effector:SetKey(_TRACK_Y, 0.33333333333333, 518.672800417476);
        effector:SetKey(_TRACK_X, 1.30000813802083, 311.165466308594);
        effector:SetKey(_TRACK_Y, 1.30000813802083, 625.148193359375);
        effector:SetKey(_TRACK_SX, 0, -0.11);
        effector:SetKey(_TRACK_SY, 0, 0.11);
        effector:SetKey(_TRACK_SX, 1.29166666666667, -0.33);
        effector:SetKey(_TRACK_SY, 1.29166666666667, 0.33);
    
        effector:Play();
        
        --compositionRatRunning2:Play();
        
        return compositionRatRunning2;
    end
    
    local composes = {};
    composes.ratRunning1 = createCompositionRatRunning1();
    --composes.ratRunning1:SetCyclic(true);
    composes.ratRunning1:SetZ(43);
    --composes.ratRunning1:SetSpeed(0.8);
    composes.ratRunning1:AttachTo(play_field);
    composes.ratRunning1:SetDestroysOnEnd(false);
    composes.ratRunning1:SetEndAction("RatRunningEnded");
    RatRunningEnded(composes.ratRunning1);
    
    composes.ratRunning2 = createCompositionRatRunning2();
    --composes.ratRunning2:SetCyclic(true);
    composes.ratRunning2:SetZ(44);
    --composes.ratRunning2:SetSpeed(0.8);
    composes.ratRunning2:AttachTo(play_field);
    composes.ratRunning2:SetDestroysOnEnd(false);
    composes.ratRunning2:SetEndAction("RatRunningEnded");
    RatRunningEnded(composes.ratRunning2);
    
    local compos = createComposition17_Theatre_Entrance_rat();
    compos:SetCyclic(true);
    compos:SetZ(45);
    compos:SetSpeed(0.8);
    compos:AttachTo(play_field);
    compos:SetCyclic(true);

end

function RatRunningEnded(compos)
    compos:PlayAt(math.random(5, 10));
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 OnObjectMouseDown(object)
    local is_wrong_click = not DefaultOnObjectMouseDown(object)
    local obj_name = object:GetName()
    local pf = play_field
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/Scenes/30_Road_Fork/Zone_Main/Layers/BG_Road_Fork.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_Road_Fork',anim,512,384,40,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/30_Road_Fork/Zone_Main/Layers/Layer_Chain_L.PNG',0,0,49,434,1,0)
	anim:SetHotSpot(24,0)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Chain_L',anim,247,-55,120,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/30_Road_Fork/Zone_Main/Layers/Layer_Chain_R.PNG',0,0,53,308,1,0)
	anim:SetHotSpot(26,0)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Chain_R',anim,617,-17,120,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/30_Road_Fork/Zone_Main/Layers/Light_Entrance.PNG',0,0,540,418,1,0)
	anim:SetHotSpot(270,209)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Light_Entrance',anim,667,607,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/30_Road_Fork/Zone_Main/Zoom_Draw_well/BG_Drraw_well.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('BG_Drraw_well',anim,512,384,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/30_Road_Fork/Zone_Main/Zoom_Draw_well/Layer_Ladder.PNG',0,0,224,235,1,0)
	anim:SetHotSpot(112,117)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Ladder',anim,521,283,155,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_28',anim,838,584,110,0,15,40)
	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_well',anim,392,567,110,0,20,9)
	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_32',anim,286,448,100,0,15,33)
	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_dialog',anim,492,434,105,0,15,25)
	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_ladder',anim,545,283,160,0,40,30)
	local anim = CreateAnimation('data/Texture/Scenes/Common/fire3.png',0,0,64,128,31,12)
	anim:SetHotSpot(32,64)
	anim:SetBlendMode(0)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('fire',anim,948,327,45,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/fire1.png',0,0,86,128,31,12)
	anim:SetHotSpot(43,64)
	anim:SetBlendMode(0)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('fire1',anim,707,362,45,0,0.35,0.45)
	local anim = CreateAnimation('data/Texture/Scenes/Common/fire1.png',0,0,86,128,31,12)
	anim:SetHotSpot(43,64)
	anim:SetBlendMode(0)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('fire2',anim,93,360,45,0,0.35,0.55)
	local anim = CreateAnimation('data/Texture/Scenes/Common/fire1.png',0,0,86,128,31,12)
	anim:SetHotSpot(43,64)
	anim:SetBlendMode(0)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('fire3',anim,180,388,45,0,0.35,0.55)
	local anim = CreateAnimation('data/Texture/Scenes/Common/fire3.png',0,0,64,128,31,12)
	anim:SetHotSpot(32,64)
	anim:SetBlendMode(0)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('fire4',anim,41,348,45,0,0.75,0.75)
	local anim = CreateAnimation('data/Texture/Scenes/Common/fire3.png',0,0,64,128,31,12)
	anim:SetHotSpot(32,64)
	anim:SetBlendMode(0)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('fire5',anim,671,356,45,0,0.6,0.6)
	local anim = CreateAnimation('data/Texture/Scenes/Common/fire1.png',0,0,86,128,31,12)
	anim:SetHotSpot(43,64)
	anim:SetBlendMode(0)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('fire6',anim,594,387,45,0,0.3,0.55)
	local anim = CreateAnimation('data/Texture/Scenes/30_Road_Fork/Zone_Main/Layers/Layer_Fakel_L1.png',0,0,90,33,1,0)
	anim:SetHotSpot(45,16)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Fakel_L1',anim,62,376,50,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/30_Road_Fork/Zone_Main/Layers/Layer_Fakel_L2.png',0,0,30,28,1,0)
	anim:SetHotSpot(15,14)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Fakel_L2',anim,179,407,50,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/30_Road_Fork/Zone_Main/Layers/Layer_Fakel_R1.png',0,0,49,36,1,0)
	anim:SetHotSpot(24,18)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Fakel_R1',anim,943,366,50,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/30_Road_Fork/Zone_Main/Layers/Layer_Fakel_R2.png',0,0,66,28,1,0)
	anim:SetHotSpot(33,14)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Fakel_R2',anim,687,377,50,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/30_Road_Fork/Zone_Main/Layers/Layer_Fakel_R3.png',0,0,28,25,1,0)
	anim:SetHotSpot(14,12)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Fakel_R3',anim,592,406,50,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/radialGlow.png',0,0,97,99,1,0)
	anim:SetHotSpot(48,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFBB00')
	screen:CreateObject('FireRadialGlow1',anim,36,360,55,0,2,2)
	local anim = CreateAnimation('data/Texture/Scenes/Common/radialGlow.png',0,0,97,99,1,0)
	anim:SetHotSpot(48,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFCC00')
	screen:CreateObject('FireRadialGlow2',anim,709,366,55,0,1.3,1.3)
	local anim = CreateAnimation('data/Texture/Scenes/Common/radialGlow.png',0,0,97,99,1,0)
	anim:SetHotSpot(48,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFDD00')
	screen:CreateObject('FireRadialGlow3',anim,670,369,55,0,1.2,1.2)
	local anim = CreateAnimation('data/Texture/Scenes/Common/radialGlow.png',0,0,97,99,1,0)
	anim:SetHotSpot(48,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFDD00')
	screen:CreateObject('FireRadialGlow4',anim,92,365,100,0,1.3,1.3)
	local anim = CreateAnimation('data/Texture/Scenes/Common/radialGlow.png',0,0,97,99,1,0)
	anim:SetHotSpot(48,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFCC00')
	screen:CreateObject('FireRadialGlow5',anim,178,394,100,0,1.2,1.2)
	local anim = CreateAnimation('data/Texture/Scenes/Common/radialGlow.png',0,0,97,99,1,0)
	anim:SetHotSpot(48,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFDD00')
	screen:CreateObject('FireRadialGlow6',anim,593,394,100,0,1.2,1.2)
	local anim = CreateAnimation('data/Texture/Scenes/Common/radialGlow.png',0,0,97,99,1,0)
	anim:SetHotSpot(48,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFDD00')
	screen:CreateObject('FireRadialGlow7',anim,943,338,55,0,2,2)
	local anim = CreateAnimation('data/Texture/Scenes/30_Road_Fork/Zone_Main/Animations/ten_anim.png',0,0,107,100,24,20)
	anim:SetHotSpot(53,50)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('ten_anim',anim,503,430,100,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
