local SurveyUntil_18 = false;

function add_fibrillation_effector_light(obj, delay)
	local duration = 8.0

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

    local dopacity = -0.6

	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 add_fibrillation_effector_door_light(obj, delay)
	local duration = 8.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.60 + 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.75 + 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/17_Theatre_Entrance_Main.lua', 'quest')
	
	RegisterQuestObjects('area_ho', 'area_theatre_hall_2', 'area_theatre_hall')

    if (SurveyUntil_18)
    then
        RegisterActiveObject('area_theatre_hall', ToBeContinued, nil, true, 'CursorDoorUp');
    else
        LinkScene('area_theatre_hall', 'CursorDoorUp', 'data/Scenes/18_Theatre_Hall_Main.lua', false)
    end
    LinkScene('area_Theatre_Fountain', 'CursorDoorDown', 'data/Scenes/13_Theatre_Fountain_Main.lua', false)
    LinkScene('area_ho', 'CursorGlass', 'data/Scenes/17_Theatre_Entrance_HO.lua', true)
	LinkScene('area_theatre_hall_2', 'CursorGlass', 'data/Scenes/17_Ballance.lua', false)

	--play_field:IgnoreOnTestPoint('Layer_svet')
	play_field:IgnoreOnTestPoint('Layer_door_Light')

	--add_fibrillation_effector_light(play_field:GetObject('Layer_svet'), 0.0)

	if HasGameState('MiniGameCompleted', '17_Ballance') then
		play_field:GetObject('Layer_door_Light'):SetVisible(true);
		play_field:GetObject('Layer_door'):SetVisible(false);

		if(MiniGame == true) then
			play_field:GetObject('Layer_door_Light'):SetOpacity(0.1);
			play_field:GetObject('Layer_door'):SetOpacity(1.);
			TimerDoString("play_field:GetObject('Layer_door_Light'):ChangeOpacity(0.0, 1.0, 0.8)", 0.1, screen:GetTimer())
			TimerDoString("play_field:GetObject('Layer_door'):ChangeOpacity(1.0, 0.0, 0.8)", 0.1, screen:GetTimer())

			TimerDoString("add_fibrillation_effector_door_light(play_field:GetObject('Layer_door_Light'), 0.3)", 1.0, screen:GetTimer())
		end		
		play_field:GetObject('area_theatre_hall_2'):SetVisible(false);
		play_field:GetObject('area_theatre_hall'):SetVisible(true);

		--dd_fibrillation_effector_light(play_field:GetObject('Layer_svet'), 0.0)
		--add_fibrillation_effector_light(play_field:GetObject('Layer_door_Light'), 0.3)
	else
		play_field:GetObject('Layer_door'):SetVisible(true);
		play_field:GetObject('Layer_door_Light'):SetVisible(false);
		play_field:GetObject('area_theatre_hall_2'):SetVisible(true);
		play_field:GetObject('area_theatre_hall'):SetVisible(false);
	end

	if HasGameState('ToolFound', '25_plunger') and
	   HasGameState('TaskComplete', '20_TurnOnLights') and -- 18, 19, 20, 25
	   HasGameState('ToolFound', '21_scissors') and -- 21
	   HasGameState('ToolFound', '22_switch') and -- 22
	   HasGameState('ToolFound', '24_chain') and
	   HasGameState('ToolFound', '24_runa') and
	   HasGameState('MiniGameCompleted', '24_Mosaic') -- 24
	then
		play_field:GetObject('Layer_door_Light'):Hide()
		game_state.ignore_on_highlight['area_theatre_hall'] = true
		if not HasGameState('LinkDisabled', '26_to_16') then
			AddGameState('LinkDisabled', '26_to_16')
		end
	end

    LoadSound("17_Spider", "data/Sound/17_Theatre_Entrance/Spider_Theatre_Entrance.ogg")
    LoadSound('17_Bats_fly', 'data/Sound/17_Theatre_Entrance/17_Bats_fly.ogg')

	AddSpiderMan(screen);
	
	AddRatsComposition();

    if not HasGameState('BatsFlyAway', '17')	
    then
        play_field.batsComp = BuildBatsAnimation();
        play_field.batsComp:AttachTo(play_field);
        play_field.batsComp:SetZ(50);
        
        RegisterActiveObject('area_bats', BatsFlyAway);
	end
end 

function AddRatsComposition()

    createCompositionRatEating1 = function()
    
        local composition17_Theatre_Entrance_rat = CreateCompositionSequence();
        local compositionDuration = 3.375;
        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(404, 539);
    
        element:SetScaleX(0.138671875);
        element:SetScaleY(0.15104166666667);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 3.375);
        
    
        effector:Play();
        
        
        composition17_Theatre_Entrance_rat:Play();
    
        return composition17_Theatre_Entrance_rat;
    end
    
    createCompositionRatEating2 = function()
    
        local composition17_Theatre_Entrance_rat = CreateCompositionSequence();
        local compositionDuration = 3.375;
        composition17_Theatre_Entrance_rat:SetDuration(compositionDuration);
    
    
        -- Layer Rat_eating_2
        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(116, 711);
    
        element:SetScaleX(-0.2734375);
        element:SetScaleY(0.27604166666667);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 3.375);
        
    
        effector:Play();
    
    
        composition17_Theatre_Entrance_rat:Play();
    
        return composition17_Theatre_Entrance_rat;
    end
    
    local createCompositionRatRunning1 = function()
    
        local compositionRatRunning1 = CreateCompositionSequence();
        local compositionDuration = 3.375;
        compositionRatRunning1:SetDuration(compositionDuration);
        
        -- Layer Rat_running
        local element = BuildRatRunningComposition();
        element:SetName("Rat_running");
        compositionRatRunning1:AddElement(element);
        element:SetHotSpot(512, 384);
        element:SetParentElement(compositionRatRunning1);
         element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 5);
    
        effector:SetKey(_TRACK_X, 0, 1058);
        effector:SetKey(_TRACK_Y, 0, 712);
        effector:SetKey(_TRACK_X, 2.83333333333333, 490);
        effector:SetKey(_TRACK_Y, 2.83333333333333, 806);
        effector:SetKey(_TRACK_SX, 0, 0.14);
        effector:SetKey(_TRACK_SY, 0, 0.14);
        effector:SetKey(_TRACK_SX, 2.83333333333333, 0.24);
        effector:SetKey(_TRACK_SY, 2.83333333333333, 0.24);
    
        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:SetScaleX(0.21);
        element:SetScaleY(0.21);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 5);
    
        effector:SetKey(_TRACK_X, 0, 900);
        effector:SetKey(_TRACK_Y, 0, 550);
        effector:SetKey(_TRACK_X, 1.30000813802083, 344.178771972656);
        effector:SetKey(_TRACK_Y, 1.30000813802083, 567.770950317383);
    
        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);
    
    local compos = createCompositionRatEating1();
    compos:SetCyclic(true);
    compos:SetZ(45);
    compos:SetSpeed(0.8);
    compos:AttachTo(play_field);
    compos:SetCyclic(true);
    
    local compos = createCompositionRatEating2();
    compos:SetCyclic(true);
    compos:SetZ(45);
    compos:SetSpeed(0.8);
    compos:AttachTo(play_field);
    compos:SetCyclic(true);
    compos:PlayAt(1);

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

    DefaultOnObjectMouseDownProcessed(is_wrong_click);
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)
    local anim = level:CreateAnimation('data/Texture/Scenes/Common/Clouds.png');
	anim:SetPosXY(512, 200);
	anim:SetOpacity(0.6);
	anim:SetScaleXY(1, 1);
	anim:SetPosZ(35);
	
	local effector = CreateTextureEffector();
	effector:AttachToAnimation(anim);
	effector:SetSpeed(-16);
	effector:Play();
end

function OnSceneDestroy(playfield)
    DefaultOnSceneDestroy(playfield)
end

--------------------------------------------------------------------------------
function AddSpiderMan(scene)

    local state = GetGameManager():GetCurrPlayer().m_gameState;
    if (state:HasState('SpiderManVanished') and state:GetState('SpiderManVanished'):HasValue('17'))
    then
    else
        state:AddState('SpiderManVanished', '17');
        PlaySpiderManFleeingAnimation(scene);
    end
end

--------------------------------------------------------------------------------
function PlaySpiderManFleeingAnimation(scene)

    --------
    local anim = nil;
    local object = nil;
    local effector = nil;
    
    local timeOffset = 1.2;
    
    TimerDoString("MakeSoundEntity('17_Spider'):Play(false);", timeOffset + 0.5, play_field:GetTimer());
    
    anim = CreateAnimation('data/Texture/Scenes/17_Theatre_Entrance/Zone_Main/Animations/krab.png',0,0,140,178,70,12)
    anim:SetHotSpot(70, 89);
    anim:SetBlendMode(2)
    anim:SetColor('0xFFFFFFFF')
    object = scene:CreateObject('SpiderManFleeing', anim, 646, 295, 101, 0, 1, 1);
    
    effector = CreateAnimationEffector();
    effector:SetLifeTime(0.0 + timeOffset, 5.83 + timeOffset);
    effector:SetAnimationTime(6.5 + timeOffset);
    effector:AttachTo(object);
    effector:Play();
    
    TimerDoString("ShowMonolog('C_17_SPIDER', 'C_17_SPIDER')", 6.5 + timeOffset, play_field:GetTimer());
    
end

function BuildBatsAnimation()

    createCompositionBG_Theatre_Entrance = function()
    
        local compositionBG_Theatre_Entrance = CreateCompositionSequence();
        local compositionDuration = 10;
        compositionBG_Theatre_Entrance:SetDuration(compositionDuration);
    
        -- Layer Bat_sleep5
        local element = CreateCompositionElement("data/Texture/Scenes/17_Theatre_Entrance/Zone_Main/Animations/batsleep2_01(30x60).png");
        element:SetName("Bat_sleep5");
        compositionBG_Theatre_Entrance:AddElement(element);
        element:SetHotSpot(15, 30);
        element:SetParentElement(compositionBG_Theatre_Entrance);
        element:SetXY(218.601226806641, 321.110427856445);
        
            anim = element:GetTexture();
			anim:SetSpeed(25);
			anim:SetFrames(21);
			anim:SetFrame(0);
			anim:SetTextureRect(0, 0, 30, 60);
			anim:SetMode(HGEANIM_FWD + HGEANIM_LOOP);
			anim:Play();
			anim:SetFrame(0);
    
        element:SetScaleX(0.65);
        element:SetScaleY(0.65);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 10);
    
    
        effector:Play();
    
    
        -- Layer Bat_sleep4
        local element = CreateCompositionElement("data/Texture/Scenes/17_Theatre_Entrance/Zone_Main/Animations/batsleep2_01(30x60).png");
        element:SetName("Bat_sleep4");
        compositionBG_Theatre_Entrance:AddElement(element);
        element:SetHotSpot(15, 30);
        element:SetParentElement(compositionBG_Theatre_Entrance);
        element:SetXY(242.601226806641, 321.610427856445);
        
            anim = element:GetTexture();
			anim:SetSpeed(25);
			anim:SetFrames(21);
			anim:SetFrame(0);
			anim:SetTextureRect(0, 0, 30, 60);
			anim:SetMode(HGEANIM_FWD + HGEANIM_LOOP);
			anim:Play();
			anim:SetFrame(12);
    
        element:SetScaleX(0.55);
        element:SetScaleY(0.55);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 10);
    
    
        effector:Play();
    
    
        -- Layer Bat_sleep3
        local element = CreateCompositionElement("data/Texture/Scenes/17_Theatre_Entrance/Zone_Main/Animations/batsleep2_01(30x60).png");
        element:SetName("Bat_sleep3");
        compositionBG_Theatre_Entrance:AddElement(element);
        element:SetHotSpot(15, 30);
        element:SetParentElement(compositionBG_Theatre_Entrance);
        element:SetXY(189.101226806641, 327.610427856445);
        
            anim = element:GetTexture();
			anim:SetSpeed(25);
			anim:SetFrames(21);
			anim:SetFrame(0);
			anim:SetTextureRect(0, 0, 30, 60);
			anim:SetMode(HGEANIM_FWD + HGEANIM_LOOP);
			anim:Play();
			anim:SetFrame(6);
    
        element:SetScaleX(0.5);
        element:SetScaleY(0.5);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 10);
    
    
        effector:Play();
    
    
        -- Layer Bat_sleep2
        local element = CreateCompositionElement("data/Texture/Scenes/17_Theatre_Entrance/Zone_Main/Animations/batsleep2_01(30x60).png");
        element:SetName("Bat_sleep2");
        compositionBG_Theatre_Entrance:AddElement(element);
        element:SetHotSpot(15, 30);
        element:SetParentElement(compositionBG_Theatre_Entrance);
        element:SetXY(235.101226806641, 330.610427856445);
        
            anim = element:GetTexture();
			anim:SetSpeed(25);
			anim:SetFrames(21);
			anim:SetFrame(0);
			anim:SetTextureRect(0, 0, 30, 60);
			anim:SetMode(HGEANIM_FWD + HGEANIM_LOOP);
			anim:Play();
			anim:SetFrame(18);
    
        element:SetScaleX(0.45);
        element:SetScaleY(0.45);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 10);
    
    
        effector:Play();
    
    
        -- Layer Bat_sleep1
        local element = CreateCompositionElement("data/Texture/Scenes/17_Theatre_Entrance/Zone_Main/Animations/batsleep2_01(30x60).png");
        element:SetName("Bat_sleep1");
        compositionBG_Theatre_Entrance:AddElement(element);
        element:SetHotSpot(15, 30);
        element:SetParentElement(compositionBG_Theatre_Entrance);
        element:SetXY(176.101226806641, 319.610427856445);
        
            anim = element:GetTexture();
			anim:SetSpeed(25);
			anim:SetFrames(21);
			anim:SetFrame(0);
			anim:SetTextureRect(0, 0, 30, 60);
			anim:SetMode(HGEANIM_FWD + HGEANIM_LOOP);
			anim:Play();
			anim:SetFrame(16);
    
        element:SetScaleX(0.6);
        element:SetScaleY(0.6);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 10);
    
    
        effector:Play();
    
    
        compositionBG_Theatre_Entrance:Play();
    
        return compositionBG_Theatre_Entrance;
    end

    local result = createCompositionBG_Theatre_Entrance();
    result:SetCyclic(true);
    
    return result;

end

function BuildBatsFlyAwayAnimation()

    createCompositionBG_Theatre_Entrance = function()
    
        local compositionBG_Theatre_Entrance = CreateCompositionSequence();
        local compositionDuration = 10;
        compositionBG_Theatre_Entrance:SetDuration(compositionDuration);
    
    
        -- Layer bat_fly5
        local element = CreateCompositionElement("data/Texture/Scenes/17_Theatre_Entrance/Zone_Main/Animations/batfly2_01(160x100).png");
        element:SetName("bat_fly5");
        compositionBG_Theatre_Entrance:AddElement(element);
        element:SetHotSpot(80, 50);
        element:SetParentElement(compositionBG_Theatre_Entrance);
        
            anim = element:GetTexture();
			anim:SetSpeed(25);
			anim:SetFrames(5);
			anim:SetFrame(0);
			anim:SetTextureRect(0, 0, 160, 100);
			anim:SetMode(HGEANIM_FWD + HGEANIM_LOOP);
			anim:Play();
    
        effector = element:GetEffector();
        effector:SetLifeTime(1.08, 2.76);
    
        effector:SetKey(_TRACK_X, 1.08, 235.5);
        effector:SetKey(_TRACK_Y, 1.08, 332);
        effector:SetKey(_TRACK_X, 1.16, 235.5);
        effector:SetKey(_TRACK_Y, 1.16, 336);
        effector:SetKey(_TRACK_X, 1.24, 236);
        effector:SetKey(_TRACK_Y, 1.24, 343);
        effector:SetKey(_TRACK_X, 1.32, 236.25);
        effector:SetKey(_TRACK_Y, 1.32, 368.75);
        effector:SetKey(_TRACK_X, 1.44, 222);
        effector:SetKey(_TRACK_Y, 1.44, 395);
        effector:SetKey(_TRACK_X, 1.52, 196);
        effector:SetKey(_TRACK_Y, 1.52, 407.5);
        effector:SetKey(_TRACK_X, 1.6, 165.75);
        effector:SetKey(_TRACK_Y, 1.6, 409.25);
        effector:SetKey(_TRACK_X, 1.68, 138.5);
        effector:SetKey(_TRACK_Y, 1.68, 393.25);
        effector:SetKey(_TRACK_X, 1.92, 62);
        effector:SetKey(_TRACK_Y, 1.92, 314);
        effector:SetKey(_TRACK_X, 2, 21.5);
        effector:SetKey(_TRACK_Y, 2, 278);
        effector:SetKey(_TRACK_X, 2.08, -56.5);
        effector:SetKey(_TRACK_Y, 2.08, 226);
        effector:SetKey(_TRACK_X, 2.24, -186.5);
        effector:SetKey(_TRACK_Y, 2.24, 201);
        effector:SetKey(_TRACK_X, 2.32, -324);
        effector:SetKey(_TRACK_Y, 2.32, 163.5);
        effector:SetKey(_TRACK_X, 2.44, -539);
        effector:SetKey(_TRACK_Y, 2.44, 48.5);
        effector:SetKey(_TRACK_SX, 1.08, 0.45);
        effector:SetKey(_TRACK_SY, 1.08, 0.45);
        effector:SetKey(_TRACK_SX, 1.28, 0.65);
        effector:SetKey(_TRACK_SY, 1.28, 0.65);
        effector:SetKey(_TRACK_SX, 2.12, 1.2);
        effector:SetKey(_TRACK_SY, 2.12, 1.2);
        effector:SetKey(_TRACK_SX, 2.32, 1.8);
        effector:SetKey(_TRACK_SY, 2.32, 1.8);
        effector:SetKey(_TRACK_SX, 2.48, 2.2);
        effector:SetKey(_TRACK_SY, 2.48, 2.2);
        effector:SetKey(_TRACK_A, 1.08, -3.14159265);
        effector:SetKey(_TRACK_A, 1.2, -3.47267698871667);
        effector:SetKey(_TRACK_A, 1.28, -4.36826615163618);
        effector:SetKey(_TRACK_A, 1.36, -5.89916424866635);
        effector:SetKey(_TRACK_A, 1.44, -6.24802885632564);
        effector:SetKey(_TRACK_A, 1.52, -6.51574162019341);
        effector:SetKey(_TRACK_A, 1.6, -6.77324345485876);
        effector:SetKey(_TRACK_A, 1.84, -6.5765117156665);
        effector:SetKey(_TRACK_A, 2, -6.46007212322153);
        effector:SetKey(_TRACK_O, 1.08, 0);
        effector:SetKey(_TRACK_O, 1.16, 1);
    
        effector:Play();
    
    
        -- Layer bat_fly4
        local element = CreateCompositionElement("data/Texture/Scenes/17_Theatre_Entrance/Zone_Main/Animations/batfly2_01(160x100).png");
        element:SetName("bat_fly4");
        compositionBG_Theatre_Entrance:AddElement(element);
        element:SetHotSpot(80, 50);
        element:SetParentElement(compositionBG_Theatre_Entrance);
        
            anim = element:GetTexture();
			anim:SetSpeed(25);
			anim:SetFrames(5);
			anim:SetFrame(0);
			anim:SetTextureRect(0, 0, 160, 100);
			anim:SetMode(HGEANIM_FWD + HGEANIM_LOOP);
			anim:Play();
    
        effector = element:GetEffector();
        effector:SetLifeTime(0.2, 1.88);
    
        effector:SetKey(_TRACK_X, 0.2, 235.5);
        effector:SetKey(_TRACK_Y, 0.2, 327.5);
        effector:SetKey(_TRACK_X, 0.28, 235.5);
        effector:SetKey(_TRACK_Y, 0.28, 331.5);
        effector:SetKey(_TRACK_X, 0.36, 236);
        effector:SetKey(_TRACK_Y, 0.36, 338.5);
        effector:SetKey(_TRACK_X, 0.44, 228.75);
        effector:SetKey(_TRACK_Y, 0.44, 350.25);
        effector:SetKey(_TRACK_X, 0.56, 211.5);
        effector:SetKey(_TRACK_Y, 0.56, 356.5);
        effector:SetKey(_TRACK_X, 0.64, 191);
        effector:SetKey(_TRACK_Y, 0.64, 350);
        effector:SetKey(_TRACK_X, 0.72, 177.25);
        effector:SetKey(_TRACK_Y, 0.72, 332.75);
        effector:SetKey(_TRACK_X, 0.8, 177.5);
        effector:SetKey(_TRACK_Y, 0.8, 302.25);
        effector:SetKey(_TRACK_X, 0.88, 185.5);
        effector:SetKey(_TRACK_Y, 0.88, 271.5);
        effector:SetKey(_TRACK_X, 1.04, 201.5);
        effector:SetKey(_TRACK_Y, 1.04, 210);
        effector:SetKey(_TRACK_X, 1.12, 205);
        effector:SetKey(_TRACK_Y, 1.12, 162);
        effector:SetKey(_TRACK_X, 1.2, 192.5);
        effector:SetKey(_TRACK_Y, 1.2, 109.5);
        effector:SetKey(_TRACK_X, 1.36, 148.5);
        effector:SetKey(_TRACK_Y, 1.36, 9.5);
        effector:SetKey(_TRACK_X, 1.44, 86);
        effector:SetKey(_TRACK_Y, 1.44, -135);
        effector:SetKey(_TRACK_X, 1.56, 25);
        effector:SetKey(_TRACK_Y, 1.56, -324);
        effector:SetKey(_TRACK_SX, 0.2, 0.45);
        effector:SetKey(_TRACK_SY, 0.2, 0.45);
        effector:SetKey(_TRACK_SX, 0.4, 0.65);
        effector:SetKey(_TRACK_SY, 0.4, 0.65);
        effector:SetKey(_TRACK_SX, 1.24, 1.2);
        effector:SetKey(_TRACK_SY, 1.24, 1.2);
        effector:SetKey(_TRACK_SX, 1.44, 1.8);
        effector:SetKey(_TRACK_SY, 1.44, 1.8);
        effector:SetKey(_TRACK_SX, 1.6, 2.2);
        effector:SetKey(_TRACK_SY, 1.6, 2.2);
        effector:SetKey(_TRACK_A, 0.2, -3.14159265);
        effector:SetKey(_TRACK_A, 0.28, -2.72740532141285);
        effector:SetKey(_TRACK_A, 0.36, -2.15332329587864);
        effector:SetKey(_TRACK_A, 0.44, -2.08051173669909);
        effector:SetKey(_TRACK_A, 0.52, -0.54061855176249);
        effector:SetKey(_TRACK_A, 0.6, -0.16789532296067);
        effector:SetKey(_TRACK_A, 0.68, -0.07986219245096);
        effector:SetKey(_TRACK_A, 0.76, 0.01065193880678);
        effector:SetKey(_TRACK_A, 0.88, 0.26742559853443);
        effector:SetKey(_TRACK_A, 1.04, 0.1721772884237);
        effector:SetKey(_TRACK_A, 1.12, -0.06978803873452);
        effector:SetKey(_TRACK_A, 1.2, -0.24516954112361);
        effector:SetKey(_TRACK_A, 1.48, -0.27934966115959);
        effector:SetKey(_TRACK_O, 0.2, 0);
        effector:SetKey(_TRACK_O, 0.28, 1);
    
        effector:Play();
    
    
        -- Layer bat_fly3
        local element = CreateCompositionElement("data/Texture/Scenes/17_Theatre_Entrance/Zone_Main/Animations/batfly2_01(160x100).png");
        element:SetName("bat_fly3");
        compositionBG_Theatre_Entrance:AddElement(element);
        element:SetHotSpot(80, 50);
        element:SetParentElement(compositionBG_Theatre_Entrance);
        
            anim = element:GetTexture();
			anim:SetSpeed(25);
			anim:SetFrames(5);
			anim:SetFrame(0);
			anim:SetTextureRect(0, 0, 160, 100);
			anim:SetMode(HGEANIM_FWD + HGEANIM_LOOP);
			anim:Play();
    
        effector = element:GetEffector();
        effector:SetLifeTime(0.64, 2.32);
    
        effector:SetKey(_TRACK_X, 0.64, 189);
        effector:SetKey(_TRACK_Y, 0.64, 321.5);
        effector:SetKey(_TRACK_X, 0.72, 189);
        effector:SetKey(_TRACK_Y, 0.72, 325.5);
        effector:SetKey(_TRACK_X, 0.8, 189.5);
        effector:SetKey(_TRACK_Y, 0.8, 332.5);
        effector:SetKey(_TRACK_X, 0.88, 198.25);
        effector:SetKey(_TRACK_Y, 0.88, 344.25);
        effector:SetKey(_TRACK_X, 1, 216.5);
        effector:SetKey(_TRACK_Y, 1, 352);
        effector:SetKey(_TRACK_X, 1.08, 234.5);
        effector:SetKey(_TRACK_Y, 1.08, 345.5);
        effector:SetKey(_TRACK_X, 1.16, 248.25);
        effector:SetKey(_TRACK_Y, 1.16, 322.25);
        effector:SetKey(_TRACK_X, 1.24, 258.5);
        effector:SetKey(_TRACK_Y, 1.24, 293.25);
        effector:SetKey(_TRACK_X, 1.32, 267.5);
        effector:SetKey(_TRACK_Y, 1.32, 259);
        effector:SetKey(_TRACK_X, 1.48, 283.5);
        effector:SetKey(_TRACK_Y, 1.48, 209.5);
        effector:SetKey(_TRACK_X, 1.56, 313);
        effector:SetKey(_TRACK_Y, 1.56, 157);
        effector:SetKey(_TRACK_X, 1.64, 356);
        effector:SetKey(_TRACK_Y, 1.64, 100.5);
        effector:SetKey(_TRACK_X, 1.68, 404.5);
        effector:SetKey(_TRACK_Y, 1.68, 45);
        effector:SetKey(_TRACK_X, 1.8, 441);
        effector:SetKey(_TRACK_Y, 1.8, -41.5);
        effector:SetKey(_TRACK_X, 1.88, 460.5);
        effector:SetKey(_TRACK_Y, 1.88, -126);
        effector:SetKey(_TRACK_X, 2, 468.5);
        effector:SetKey(_TRACK_Y, 2, -248);
        effector:SetKey(_TRACK_SX, 0.64, 0.45);
        effector:SetKey(_TRACK_SY, 0.64, 0.45);
        effector:SetKey(_TRACK_SX, 0.84, 0.65);
        effector:SetKey(_TRACK_SY, 0.84, 0.65);
        effector:SetKey(_TRACK_SX, 1.68, 1.2);
        effector:SetKey(_TRACK_SY, 1.68, 1.2);
        effector:SetKey(_TRACK_SX, 1.88, 1.8);
        effector:SetKey(_TRACK_SY, 1.88, 1.8);
        effector:SetKey(_TRACK_SX, 2.04, 2.2);
        effector:SetKey(_TRACK_SY, 2.04, 2.2);
        effector:SetKey(_TRACK_A, 0.64, 3.14159265);
        effector:SetKey(_TRACK_A, 0.72, 2.82841856527168);
        effector:SetKey(_TRACK_A, 0.8, 1.94206831957613);
        effector:SetKey(_TRACK_A, 0.88, 0.61234841895383);
        effector:SetKey(_TRACK_A, 1, 0.39882408574938);
        effector:SetKey(_TRACK_A, 1.16, 0.32864974898854);
        effector:SetKey(_TRACK_A, 1.6, 0.02570894501387);
        effector:SetKey(_TRACK_O, 0.64, 0);
        effector:SetKey(_TRACK_O, 0.72, 1);
    
        effector:Play();
    
    
        -- Layer bat_fly2
        local element = CreateCompositionElement("data/Texture/Scenes/17_Theatre_Entrance/Zone_Main/Animations/batfly2_01(160x100).png");
        element:SetName("bat_fly2");
        compositionBG_Theatre_Entrance:AddElement(element);
        element:SetHotSpot(80, 50);
        element:SetParentElement(compositionBG_Theatre_Entrance);
        
            anim = element:GetTexture();
			anim:SetSpeed(25);
			anim:SetFrames(5);
			anim:SetFrame(0);
			anim:SetTextureRect(0, 0, 160, 100);
			anim:SetMode(HGEANIM_FWD + HGEANIM_LOOP);
			anim:Play();
    
        effector = element:GetEffector();
        effector:SetLifeTime(0.32, 2.12);
    
        effector:SetKey(_TRACK_X, 0.32, 219.5);
        effector:SetKey(_TRACK_Y, 0.32, 317.5);
        effector:SetKey(_TRACK_X, 0.4, 219.5);
        effector:SetKey(_TRACK_Y, 0.4, 321.5);
        effector:SetKey(_TRACK_X, 0.48, 220);
        effector:SetKey(_TRACK_Y, 0.48, 328.5);
        effector:SetKey(_TRACK_X, 0.56, 228.75);
        effector:SetKey(_TRACK_Y, 0.56, 340.25);
        effector:SetKey(_TRACK_X, 0.68, 247);
        effector:SetKey(_TRACK_Y, 0.68, 348);
        effector:SetKey(_TRACK_X, 0.76, 268);
        effector:SetKey(_TRACK_Y, 0.76, 348.5);
        effector:SetKey(_TRACK_X, 0.84, 290.75);
        effector:SetKey(_TRACK_Y, 0.84, 340.75);
        effector:SetKey(_TRACK_X, 0.92, 311);
        effector:SetKey(_TRACK_Y, 0.92, 323.25);
        effector:SetKey(_TRACK_X, 1, 337.5);
        effector:SetKey(_TRACK_Y, 1, 295);
        effector:SetKey(_TRACK_X, 1.16, 378);
        effector:SetKey(_TRACK_Y, 1.16, 225.5);
        effector:SetKey(_TRACK_X, 1.24, 422.5);
        effector:SetKey(_TRACK_Y, 1.24, 189.5);
        effector:SetKey(_TRACK_X, 1.32, 482);
        effector:SetKey(_TRACK_Y, 1.32, 166.5);
        effector:SetKey(_TRACK_X, 1.4, 561);
        effector:SetKey(_TRACK_Y, 1.4, 157);
        effector:SetKey(_TRACK_X, 1.48, 646.5);
        effector:SetKey(_TRACK_Y, 1.48, 159.5);
        effector:SetKey(_TRACK_X, 1.56, 723);
        effector:SetKey(_TRACK_Y, 1.56, 142);
        effector:SetKey(_TRACK_X, 1.68, 787);
        effector:SetKey(_TRACK_Y, 1.68, 84);
        effector:SetKey(_TRACK_X, 1.8, 821);
        effector:SetKey(_TRACK_Y, 1.8, 1);
        effector:SetKey(_TRACK_X, 1.88, 845);
        effector:SetKey(_TRACK_Y, 1.88, -103);
        effector:SetKey(_TRACK_SX, 0.32, 0.65);
        effector:SetKey(_TRACK_SY, 0.32, 0.65);
        effector:SetKey(_TRACK_SX, 0.52, 0.65);
        effector:SetKey(_TRACK_SY, 0.52, 0.65);
        effector:SetKey(_TRACK_SX, 1.36, 1.2);
        effector:SetKey(_TRACK_SY, 1.36, 1.2);
        effector:SetKey(_TRACK_SX, 1.56, 1.8);
        effector:SetKey(_TRACK_SY, 1.56, 1.8);
        effector:SetKey(_TRACK_SX, 1.88, 2.2);
        effector:SetKey(_TRACK_SY, 1.88, 2.2);
        effector:SetKey(_TRACK_A, 0.32, 3.14159265);
        effector:SetKey(_TRACK_A, 0.4, 2.82841856527168);
        effector:SetKey(_TRACK_A, 0.48, 1.94206831957613);
        effector:SetKey(_TRACK_A, 0.56, 0.61234841895383);
        effector:SetKey(_TRACK_A, 0.68, 0.39882408574938);
        effector:SetKey(_TRACK_A, 0.84, 0.32864974898854);
        effector:SetKey(_TRACK_A, 1.28, 0.17489107657719);
        effector:SetKey(_TRACK_O, 0.32, 0);
        effector:SetKey(_TRACK_O, 0.4, 1);
    
        effector:Play();
    
    
        -- Layer bat_fly1
        local element = CreateCompositionElement("data/Texture/Scenes/17_Theatre_Entrance/Zone_Main/Animations/batfly2_01(160x100).png");
        element:SetName("bat_fly1");
        compositionBG_Theatre_Entrance:AddElement(element);
        element:SetHotSpot(80, 50);
        element:SetParentElement(compositionBG_Theatre_Entrance);
        
            anim = element:GetTexture();
			anim:SetSpeed(25);
			anim:SetFrames(5);
			anim:SetFrame(0);
			anim:SetTextureRect(0, 0, 160, 100);
			anim:SetMode(HGEANIM_FWD + HGEANIM_LOOP);
			anim:Play();
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 1.44);
    
        effector:SetKey(_TRACK_X, 0, 176.5);
        effector:SetKey(_TRACK_Y, 0, 323.5);
        effector:SetKey(_TRACK_X, 0.08, 176.5);
        effector:SetKey(_TRACK_Y, 0.08, 327.5);
        effector:SetKey(_TRACK_X, 0.16, 176);
        effector:SetKey(_TRACK_Y, 0.16, 334.5);
        effector:SetKey(_TRACK_X, 0.24, 166.5);
        effector:SetKey(_TRACK_Y, 0.24, 342.5);
        effector:SetKey(_TRACK_X, 0.36, 157);
        effector:SetKey(_TRACK_Y, 0.36, 339.5);
        effector:SetKey(_TRACK_X, 0.44, 148.5);
        effector:SetKey(_TRACK_Y, 0.44, 330);
        effector:SetKey(_TRACK_X, 0.52, 143);
        effector:SetKey(_TRACK_Y, 0.52, 312.5);
        effector:SetKey(_TRACK_X, 0.6, 134);
        effector:SetKey(_TRACK_Y, 0.6, 287.5);
        effector:SetKey(_TRACK_X, 0.68, 116.5);
        effector:SetKey(_TRACK_Y, 0.68, 254);
        effector:SetKey(_TRACK_X, 0.84, 84);
        effector:SetKey(_TRACK_Y, 0.84, 213.5);
        effector:SetKey(_TRACK_X, 0.92, 52.5);
        effector:SetKey(_TRACK_Y, 0.92, 179.5);
        effector:SetKey(_TRACK_X, 1, 22);
        effector:SetKey(_TRACK_Y, 1, 145.5);
        effector:SetKey(_TRACK_X, 1.08, -7);
        effector:SetKey(_TRACK_Y, 1.08, 112);
        effector:SetKey(_TRACK_X, 1.16, -42.5);
        effector:SetKey(_TRACK_Y, 1.16, 76.5);
        effector:SetKey(_TRACK_X, 1.24, -98);
        effector:SetKey(_TRACK_Y, 1.24, 28);
        effector:SetKey(_TRACK_SX, 0, 0.65);
        effector:SetKey(_TRACK_SY, 0, 0.65);
        effector:SetKey(_TRACK_SX, 0.2, 0.65);
        effector:SetKey(_TRACK_SY, 0.2, 0.65);
        effector:SetKey(_TRACK_SX, 1.24, 1);
        effector:SetKey(_TRACK_SY, 1.24, 1);
        effector:SetKey(_TRACK_A, 0, 3.14159265);
        effector:SetKey(_TRACK_A, 0.08, 3.36786601176908);
        effector:SetKey(_TRACK_A, 0.16, 4.37539450754332);
        effector:SetKey(_TRACK_A, 0.24, 5.41202164608502);
        effector:SetKey(_TRACK_A, 0.36, 6.03846683893795);
        effector:SetKey(_TRACK_A, 0.52, 6.11635315608726);
        effector:SetKey(_TRACK_O, 0, 0);
        effector:SetKey(_TRACK_O, 0.08, 1);
    
        effector:Play();
    
    
        compositionBG_Theatre_Entrance:Play();
    
        return compositionBG_Theatre_Entrance;
    end

    local result = createCompositionBG_Theatre_Entrance();
    return result;

end

function BatsFlyAway()
    play_field.batsComp:Destroy();
    AddGameState('BatsFlyAway', '17');
    
    local compos = BuildBatsFlyAwayAnimation();
    compos:AttachTo(play_field);
    compos:SetZ(46);
    
    MakeSoundEntity('17_Bats_fly'):Play(false);
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/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,6)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_theatre_hall',anim,516,495,110,0,30,20)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,6)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_theatre_hall_2',anim,516,495,111,0,30,20)
	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_ho',anim,866,613,100,0,30,30)
	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_Theatre_Fountain',anim,527,708,90,0,54,15)
	local anim = CreateAnimation('data/Texture/Scenes/17_Theatre_Entrance/Zone_Main/Layers/BG_Theatre_Entrance.JPG',0,299,1024,469,1,0)
	anim:SetHotSpot(0,0)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_Theatre_Entrance',anim,0,299,40,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/17_Theatre_Entrance/Zone_Main/Layers/Layer_door.jpg',0,0,68,78,1,0)
	anim:SetHotSpot(34,39)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_door',anim,518,491,50,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/17_Theatre_Entrance/Zone_Main/Layers/Layer_door_Light.png',0,0,146,167,1,0)
	anim:SetHotSpot(73,83)
	anim:SetBlendMode(0)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_door_Light',anim,517,444,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/17_Theatre_Entrance/Zone_Main/Layers/Layer_Top.png',0,0,1024,299,1,0)
	anim:SetHotSpot(0,0)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Top',anim,0,0,45,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/17_Theatre_Entrance/Zone_Main/Layers/Layer_Sky.JPG',0,0,1024,400,1,0)
	anim:SetHotSpot(0,0)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Sky',anim,0,0,30,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/17_Theatre_Entrance/Zone_Main/Layers/Layer_P1.PNG',0,0,45,54,1,0)
	anim:SetHotSpot(22,27)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_P1',anim,500,264,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area_round.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_bats',anim,220,325,105,0,0.85,0.35)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
