
function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/13_Fountain_Minigame.lua', 'quest', 'data/Scenes/13_Theatre_Fountain_Zoom.lua')

	RegisterQuestObjects('area_lever', 'area_chain', 'area_relay')

    LinkScene('area', 'CursorDoorDown', 'data/Scenes/13_Theatre_Fountain_Main.lua', false)
    
    game_active = true;
    
    lever_active = false;
    LeverFlag = 0;
    ChainFlag = 0;
    PumpFlag = 0;
    
	Compositions = {};
	
    game_screen = screen;	
	skip_mini_game = false;
	
	local anim = CreateMechFountain(screen);
	anim:AttachTo(screen);
	anim:Play();
	
	screen:GetObject('switch'):SetVisible(false);
    Chain:SetVisible(false);
	PumpObj:SetVisible(false);
	
	for i = 1,6 do
		Compositions[i]:Play();
		Compositions[i]:Pause();
	end
	
	if not HasGameState('MiniGameCompleted', '13_fountain')
	then
        if (HasGameState('ToolFound', '22_switch') and
            HasGameState('ToolFound', '24_chain') and
            HasGameState('ToolFound', '25_plunger'))
        then
            ShowMonolog('C_13_FOUNTAIN_MG_HAVE_TOOLS', 'C_13_FOUNTAIN_MG_HAVE_TOOLS');
        end
    end
	

-------------------------------------------------------------------------------------------------------------------------
	local on_lever_apply = function(tool_name, object_name)
		screen:GetObject('switch'):SetVisible(true);
		screen:GetObject('switch'):ChangeOpacity(0.0, 1.0, 0.5);
		TimerDoString("lever_active = true;", 0.6, play_field:GetTimer())
		screen:GetObject('area_chain'):SetVisible(true);
		screen:GetObject('area_lever'):SetVisible(false);
		ShowAllToolsAppliedComment('22_switch');
		game_state.ignore_on_highlight['area_lever'] = true
		MakeSoundEntity('13_Apply_lever'):Play(false);
    end
    local on_load_when_level_applied = function(tool_name)
		screen:GetObject('switch'):SetVisible(true);
		screen:GetObject('area_chain'):SetVisible(true);
		screen:GetObject('area_lever'):SetVisible(false);
		game_state.ignore_on_highlight['area_lever'] = true
    end
    RegisterToolApplyInfo('22_switch', 'area_lever', nil, nil, on_lever_apply, on_load_when_level_applied)
-------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------
	local on_chain_apply = function(tool_name, object_name)
		Chain:SetVisible(true);
		ChainFlag = 1;
		UpdateMechanism();
		ShowAllToolsAppliedComment('24_chain');
		game_state.ignore_on_highlight['area_chain'] = true
		MakeSoundEntity('13_Apply_chain'):Play(false);
    end
    
	local on_load_when_chain_applied = function(tool_name)
		Chain:SetVisible(true);
		ChainFlag = 1;
		game_state.ignore_on_highlight['area_chain'] = true
    end
    RegisterToolApplyInfo('24_chain', 'area_chain', nil, nil, on_chain_apply, on_load_when_chain_applied)
-------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------
	local on_pump_apply = function(tool_name, object_name)
		PumpObj:SetVisible(true);
		PumpFlag = 1;
		UpdateMechanism();
		ShowAllToolsAppliedComment('25_plunger');
		game_state.ignore_on_highlight['area_relay'] = true
		MakeSoundEntity('13_Apply_plunger'):Play(false);
    end
    
	local on_load_when_pump_applied = function(tool_name)
		PumpObj:SetVisible(true);
		PumpFlag = 1;
		game_state.ignore_on_highlight['area_relay'] = true
    end
    RegisterToolApplyInfo('25_plunger', 'area_relay', nil, nil, on_pump_apply, on_load_when_pump_applied)
-------------------------------------------------------------------------------------------------------------------------
	
	init_steams();
	
	if HasGameState('Fountain_Mechanism', 'active') == true then
		LeverFlag = 1;
		do_steam_pseudo_recursive_iteration()
		game_screen:GetObject('switch'):SetAngle(- M_PI_OVER_2 / 2);
		UpdateMechanism();
	end
	
	screen:SetLayer(ADDITIONAL_LAYER);
	RegisterActiveObject('switch', PressSwitch, nil, true, 'CursorActive');
	
	LoadSound('13_Apply_chain', 'data/Sound/13_Theatre_Fountain/Apply_chain.ogg')
	LoadSound('13_Apply_lever', 'data/Sound/13_Theatre_Fountain/Apply_lever.ogg')
	LoadSound('13_Apply_plunger', 'data/Sound/13_Theatre_Fountain/Apply_plunger.ogg')
	LoadSound('13_Start_fountain', 'data/Sound/13_Theatre_Fountain/Start_fountain.ogg')
	LoadSound('13_Rotating_no_plunger', 'data/Sound/13_Theatre_Fountain/Rotating_no_plunger.ogg')
end


--------------------------------------------------------------------------------------------------------------------------
function init_steams()
	game_state.steams = {}

	table.insert(game_state.steams, create_steam(535., 90., 1.))
	table.insert(game_state.steams, create_steam(480., 210., 1.))
	
	par_flag = true;
	
	--do_steam_pseudo_recursive_iteration()
end

function create_steam(x, y, scale)
    scale = (scale ~= nil and scale or 1.0)

	local ps = CreateParticleSystemEntity('data/ParticleSystem/Steam.psi', 'SmokeParticle')
	ps:AttachTo(play_field)
	ps:SetXY(x, y)
	ps:SetVisible(false);
	ps:SetZ(130.0)
	ps:SetScale(scale)
	ps:GetParticleSystem():Stop(true)
	return ps
end

function do_steam_pseudo_recursive_iteration()
	if(par_flag == true) then
		start_random_steam()
		TimerDoString("do_steam_pseudo_recursive_iteration()", math.random(0.1, 1.), play_field:GetTimer())
	else
		par_flag = true;
	end
end

function stop_random_steam()
	par_flag = false;
end

function start_random_steam()
	if game_state.steams ~= nil and table.getn(game_state.steams) > 0 then
		local i = math.random(1, table.getn(game_state.steams))
		game_state.steams[i]:SetVisible(true);
		game_state.steams[i]:GetParticleSystem().info.fDirection = math.random(360) * M_PI / 180;
		game_state.steams[i]:GetParticleSystem():Fire()
	end
end

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

function PressSwitch()
	if(game_active == true) then
		PushLever();
		
		if LeverFlag == 1
		then
		    if (not HasGameState('ToolApplied', '24_chain') or 
                not HasGameState('ToolApplied', '25_plunger'))
            then
	            ShowMonolog('13_Fountain_MiniGame', 'C_13_FOUNTAIN_MG')
            end
        end
	end
end

function PushLever()
	local angle = 0;
	if(LeverFlag == 1) then
		angle = M_PI_OVER_2 / 2;
		LeverFlag = 0;
		Compositions[1]:Pause();
		Compositions[2]:Pause();
		Compositions[6]:Pause();
		if HasGameState('Fountain_Mechanism', 'active') == true then
			RemoveGameState('Fountain_Mechanism', 'active');
		end
		stop_random_steam();
		
		if (play_field.mechIdleSound and play_field.mechIdleSound:IsPlaying())
		then
		    play_field.mechIdleSound:StopSound();
		end
		
	else
		angle = -M_PI_OVER_2 / 2;
		LeverFlag = 1;
		UpdateMechanism();
		if HasGameState('Fountain_Mechanism', 'active') == false then
			AddGameState('Fountain_Mechanism', 'active');
		end
		do_steam_pseudo_recursive_iteration()
	end
	
	local Obj = game_screen:GetObject('switch');
	local af = CreateAnimationEffector()
    af:AttachTo(Obj)
    af:SetKey(_TRACK_A, 0.0, Obj:GetAngle())
    af:SetKey(_TRACK_A, 0.2, Obj:GetAngle() + angle)
    af:Play()
    
    lever_active = true;
    TimerDoString("lever_active = true;", 1.1, play_field:GetTimer())
end

function UpdateMechanism()
	if(LeverFlag == 1) then
		TimerDoString("Compositions[1]:Resume();", 0.25, play_field:GetTimer())
		if(ChainFlag == 1) then
			TimerDoString("Compositions[2]:Resume();", 0.25, play_field:GetTimer())
			
			if(PumpFlag == 1) then
				FinishGame();
				return;
			end
		end
		
		if not play_field.mechIdleSound
        then
            play_field.mechIdleSound = MakeSoundEntity('13_Rotating_no_plunger');
        end
        
        if not play_field.mechIdleSound:IsPlaying()
        then
            play_field.mechIdleSound:Play(true);
        end
	end
	
end

function FinishGame()
	table.insert(game_state.steams, create_steam(890., 450., 1.))

	UnregisterActiveObject('switch')

	TimerDoString("Compositions[6]:Resume();", 0.25, play_field:GetTimer())
	TimerDoString("Compositions[4]:Resume();", 0.3, play_field:GetTimer())
	TimerDoString("Compositions[3]:Resume();", 3.0, play_field:GetTimer())
	TimerDoString("Compositions[5]:Resume();", 3.0, play_field:GetTimer())
	TimerDoString("Compositions[4]:Pause();", 3.0, play_field:GetTimer())
	game_active = false;
	AddGameState('MiniGameCompleted', '13_fountain')
	TimerDoString("StartLevel('"..game_state.parent_scene.."', true)", 5.0, play_field:GetTimer())
	MakeSoundEntity('13_Start_fountain'):Play(false);
end

function CreateMechFountain()

    local composition = CreateCompositionSequence();
    local compositionDuration = 3.95833333333333;
    --composition:SetDuration(compositionDuration);
    composition:SetDestroysOnEnd(false);
    composition:SetZ(100);
    --composition:SetCyclic(true);
    
    local createLeversComposition = function()
    
        local element = nil;
        local subComposition = CreateCompositionSequence();
        local compositionDuration = 0.5;
        subComposition:SetDuration(compositionDuration);
        subComposition:SetCyclic(true);
        
        
        -- Layer Lever_1
        element = CreateCompositionElement("data/Texture/Minigames/Fountain/val01.png");
        element:SetName("Lever_1");
        subComposition:AddElement(element);
        element:SetHotSpot(33, 168);
        element:SetParentElement(subComposition);
        element:SetScaleX(1);
        element:SetScaleY(1);
        element:SetAngle(0);
        element:SetOpacity(1);
        
        effector = element:GetEffector();
        effector:SetLifeTime(0, 4);
        
        effector:SetKey(_TRACK_X, 0, 415);
        effector:SetKey(_TRACK_Y, 0, 386);
        effector:SetKey(_TRACK_X, 0.25, 415);
        effector:SetKey(_TRACK_Y, 0.25, 363);
        effector:SetKey(_TRACK_X, 0.5, 415);
        effector:SetKey(_TRACK_Y, 0.5, 386);
        
        effector:Play();
        
        
        -- Layer Lever_2
        element = CreateCompositionElement("data/Texture/Minigames/Fountain/val02.png");
        element:SetName("Lever_2");
        subComposition:AddElement(element);
        element:SetHotSpot(30.5, 151.5);
        element:SetParentElement(subComposition);
        element:SetScaleX(1);
        element:SetScaleY(1);
        element:SetAngle(0);
        element:SetOpacity(1);
        
        effector = element:GetEffector();
        effector:SetLifeTime(0, 4);
        
        effector:SetKey(_TRACK_X, 0, 473);
        effector:SetKey(_TRACK_Y, 0, 369);
        effector:SetKey(_TRACK_X, 0.25, 473);
        effector:SetKey(_TRACK_Y, 0.25, 390);
        effector:SetKey(_TRACK_X, 0.5, 473);
        effector:SetKey(_TRACK_Y, 0.5, 369);
        
        effector:Play();
        
        
        -- Layer Lever_3
        element = CreateCompositionElement("data/Texture/Minigames/Fountain/val03.png");
        element:SetName("Lever_3");
        subComposition:AddElement(element);
        element:SetHotSpot(28, 136);
        element:SetParentElement(subComposition);
        element:SetScaleX(1);
        element:SetScaleY(1);
        element:SetAngle(0);
        element:SetOpacity(1);
        
        effector = element:GetEffector();
        effector:SetLifeTime(0, 4);
        
        effector:SetKey(_TRACK_X, 0, 525);
        effector:SetKey(_TRACK_Y, 0, 384);
        effector:SetKey(_TRACK_X, 0.25, 525);
        effector:SetKey(_TRACK_Y, 0.25, 361);
        effector:SetKey(_TRACK_X, 0.5, 525);
        effector:SetKey(_TRACK_Y, 0.5, 384);
        
        effector:Play();
        
        subComposition:Play();
        return subComposition;
    end
    
    local element = nil;
    local anim = nil;
    local effector = nil;
    
    
	local createLeversChain = function()
        local element = nil;
        local subComposition = CreateCompositionSequence();
        local compositionDuration = 3.95833333333333;
        subComposition:SetDuration(compositionDuration);
		subComposition:SetCyclic(true);
		
		-- Layer Gear_4
		local element = CreateCompositionElement("data/Texture/Minigames/Fountain/Roll02.png");
		element:SetName("Gear_4");
		subComposition:AddElement(element);
		element:SetHotSpot(57.5, 57);
		element:SetParentElement(subComposition);
		element:SetXY(392, 448);
		
		element:SetScaleX(1);
		element:SetScaleY(1);
		element:SetOpacity(1);
		
		effector = element:GetEffector();
		effector:SetLifeTime(0, 4);
		
		effector:SetKey(_TRACK_A, 0, 0);
		effector:SetKey(_TRACK_A, 3.95833333333333, 18.8321026075);
		
		effector:Play();
		
		  -- Layer Chain_mech
		local element = CreateCompositionElement("data/Texture/Minigames/Fountain/Tsep.png");
		element:SetName("Chain_mech");
		subComposition:AddElement(element);
		element:SetHotSpot(104, 120);
		element:SetParentElement(subComposition);
		element:SetXY(461, 522);
		
		element:SetScaleX(1);
		element:SetScaleY(1);
		element:SetAngle(0);
		element:SetOpacity(1);
		
			anim = element:GetTexture();
			anim:SetSpeed(30);
			anim:SetFrames(6);
			anim:SetFrame(0);
			anim:SetTextureRect(0, 0, 208, 240);
			anim:SetMode(HGEANIM_FWD + HGEANIM_LOOP);
			anim:Play();
		
		effector = element:GetEffector();
		--effector:SetLifeTime(0, 4);
		
		effector:Play();
		
		Chain = element;
		
		-- Layer Rotor
		local element = CreateCompositionElement("data/Texture/Minigames/Fountain/rotor.png");
		element:SetName("Rotor");
		subComposition:AddElement(element);
		element:SetHotSpot(131.5, 130);
		element:SetParentElement(subComposition);
		element:SetXY(392, 447);
		
		element:SetScaleX(1);
		element:SetScaleY(1);
		element:SetOpacity(1);
		
		effector = element:GetEffector();
		effector:SetLifeTime(0, 4);
		
		effector:SetKey(_TRACK_A, 0, 0);
		effector:SetKey(_TRACK_A, 3.95833333333333, 31.3984732075);
		
		effector:Play();

		
		return subComposition;
	end
	
	local createLevers_2 = function()
        local element = nil;
        local subComposition = CreateCompositionSequence();
        local compositionDuration = 3.95833333333333;
        subComposition:SetDuration(compositionDuration);
		subComposition:SetCyclic(true);
	
		-- Layer Gear_1
		element = CreateCompositionElement("data/Texture/Minigames/Fountain/08.png");
		element:SetName("Gear_1");
		subComposition:AddElement(element);
		element:SetHotSpot(28.5, 28.5);
		element:SetParentElement(subComposition);
		element:SetScaleX(1);
		element:SetScaleY(1);
		element:SetOpacity(1);
		
		effector = element:GetEffector();
		effector:SetLifeTime(0, 4);
		
		effector:SetKey(_TRACK_X, 0, 556);
		effector:SetKey(_TRACK_Y, 0, 559);
		effector:SetKey(_TRACK_X, 0.5, 561);
		effector:SetKey(_TRACK_Y, 0.5, 559);
		effector:SetKey(_TRACK_X, 1, 556);
		effector:SetKey(_TRACK_Y, 1, 559);
		effector:SetKey(_TRACK_X, 1.5, 561);
		effector:SetKey(_TRACK_Y, 1.5, 559);
		effector:SetKey(_TRACK_X, 2, 556);
		effector:SetKey(_TRACK_Y, 2, 559);
		effector:SetKey(_TRACK_X, 2.5, 561);
		effector:SetKey(_TRACK_Y, 2.5, 559);
		effector:SetKey(_TRACK_X, 3, 556);
		effector:SetKey(_TRACK_Y, 3, 559);
		effector:SetKey(_TRACK_X, 3.5, 561);
		effector:SetKey(_TRACK_Y, 3.5, 559);
		effector:SetKey(_TRACK_X, 4, 556);
		effector:SetKey(_TRACK_Y, 4, 559);
		effector:SetKey(_TRACK_A, 0, 0);
		effector:SetKey(_TRACK_A, 3.95833333333333, 25.1327412);
		
		effector:Play();

		-- Layer Pump_2
		element = CreateCompositionElement("data/Texture/Minigames/Fountain/Nasos02.png");
		element:SetName("Pump_2");
		subComposition:AddElement(element);
		element:SetHotSpot(25, 28.5);
		element:SetParentElement(subComposition);
		element:SetScaleX(1);
		element:SetScaleY(1);
		element:SetAngle(0);
		element:SetOpacity(1);
		
		effector = element:GetEffector();
		--effector:SetLifeTime(0, 4);
		
		effector:SetKey(_TRACK_X, 0, 548.5);
		effector:SetKey(_TRACK_Y, 0, 552.25);
		effector:SetKey(_TRACK_X, 0.25, 562);
		effector:SetKey(_TRACK_Y, 0.25, 544);
		effector:SetKey(_TRACK_X, 0.5, 576);
		effector:SetKey(_TRACK_Y, 0.5, 552.5);
		effector:SetKey(_TRACK_X, 0.75, 562.75);
		effector:SetKey(_TRACK_Y, 0.75, 559.5);
		effector:SetKey(_TRACK_X, 1, 548.5);
		effector:SetKey(_TRACK_Y, 1, 552.25);
		effector:SetKey(_TRACK_X, 1.25, 562);
		effector:SetKey(_TRACK_Y, 1.25, 544);
		effector:SetKey(_TRACK_X, 1.5, 576);
		effector:SetKey(_TRACK_Y, 1.5, 552.5);
		effector:SetKey(_TRACK_X, 1.75, 562.75);
		effector:SetKey(_TRACK_Y, 1.75, 559.5);
		effector:SetKey(_TRACK_X, 2, 548.5);
		effector:SetKey(_TRACK_Y, 2, 552.25);
		effector:SetKey(_TRACK_X, 2.25, 562);
		effector:SetKey(_TRACK_Y, 2.25, 544);
		effector:SetKey(_TRACK_X, 2.5, 576);
		effector:SetKey(_TRACK_Y, 2.5, 552.5);
		effector:SetKey(_TRACK_X, 2.75, 562.75);
		effector:SetKey(_TRACK_Y, 2.75, 559.5);
		effector:SetKey(_TRACK_X, 3, 548.5);
		effector:SetKey(_TRACK_Y, 3, 552.25);
		effector:SetKey(_TRACK_X, 3.25, 562);
		effector:SetKey(_TRACK_Y, 3.25, 544);
		effector:SetKey(_TRACK_X, 3.5, 576);
		effector:SetKey(_TRACK_Y, 3.5, 552.5);
		effector:SetKey(_TRACK_X, 3.75, 562.75);
		effector:SetKey(_TRACK_Y, 3.75, 559.5);
		effector:SetKey(_TRACK_X, 4, 548.5);
		effector:SetKey(_TRACK_Y, 4, 552.25);
		
		effector:Play();
		PumpObj = element;
				
		-- Layer Gear_2
		element = CreateCompositionElement("data/Texture/Minigames/Fountain/Roll.png");
		element:SetName("Gear_2");
		subComposition:AddElement(element);
		element:SetHotSpot(104, 104.5);
		element:SetParentElement(subComposition);
		element:SetXY(537, 572);
		
		element:SetScaleX(1);
		element:SetScaleY(1);
		element:SetOpacity(1);
		
		effector = element:GetEffector();
		effector:SetLifeTime(0, 4);
		
		effector:SetKey(_TRACK_A, 0, 0);
		effector:SetKey(_TRACK_A, 3.95833333333333, 6.2657320075);
		
		effector:Play();
	
		return subComposition;
	end
	
	local createLevers_3 = function()
        local element = nil;
        local subComposition = CreateCompositionSequence();
        local compositionDuration = 3.95833333333333;
        subComposition:SetDuration(compositionDuration);
		subComposition:SetCyclic(true);
		
		-- Layer Mech_right
		element = CreateCompositionElement("data/Texture/Minigames/Fountain/meh02.png");
		element:SetName("Mech_right");
		subComposition:AddElement(element);
		element:SetHotSpot(53, 275.5);
		element:SetParentElement(subComposition);
		element:SetXY(970, 377);
		
			anim = element:GetTexture();
			anim:SetSpeed(30);
			anim:SetFrames(6);
			anim:SetFrame(0);
			anim:SetTextureRect(0, 0, 107, 551);
			anim:SetMode(HGEANIM_FWD + HGEANIM_LOOP);
			anim:Play();
		
		element:SetScaleX(1);
		element:SetScaleY(1);
		element:SetAngle(0);
		element:SetOpacity(1);
		
		effector = element:GetEffector();
		effector:SetLifeTime(0, 4);
		
		
		effector:Play();
		
		
		-- Layer Mech_left
		element = CreateCompositionElement("data/Texture/Minigames/Fountain/meh01.png");
		element:SetName("Mech_left");
		subComposition:AddElement(element);
		element:SetHotSpot(151, 102);
		element:SetParentElement(subComposition);
		element:SetXY(154, 550);
		
			anim = element:GetTexture();
			anim:SetSpeed(30);
			anim:SetFrames(6);
			anim:SetFrame(0);
			anim:SetTextureRect(0, 0, 302, 204);
			anim:SetMode(HGEANIM_FWD + HGEANIM_LOOP);
			anim:Play();
		
		element:SetScaleX(1);
		element:SetScaleY(1);
		element:SetAngle(0);
		element:SetOpacity(1);
		
		effector = element:GetEffector();
		effector:SetLifeTime(0, 4);
		
		
		effector:Play();
		
		return subComposition;
	end    
    
    local createLevers_4 = function()
		local element = nil;
        local subComposition = CreateCompositionSequence();
        local compositionDuration = 3.95833333333333;
        subComposition:SetDuration(compositionDuration);
		subComposition:SetCyclic(true);
		
		-- Layer Gear_3
		local element = CreateCompositionElement("data/Texture/Minigames/Fountain/01.png");
		element:SetName("Gear_3");
		subComposition:AddElement(element);
		element:SetHotSpot(149.5, 149.5);
		element:SetParentElement(subComposition);
		element:SetXY(799, 541);
		
		element:SetScaleX(1);
		element:SetScaleY(1);
		element:SetOpacity(1);
		
		effector = element:GetEffector();
		effector:SetLifeTime(0, 4);
		
		effector:SetKey(_TRACK_A, 0, 0);
		effector:SetKey(_TRACK_A, 3.95833333333333, -6.2831853);
		
		effector:Play();
		
    	return subComposition;
    end
    
    local createLevers_water = function()
        local element = nil;
        local subComposition = CreateCompositionSequence();
        local compositionDuration = 3.95833333333333;
        subComposition:SetDuration(compositionDuration);
		subComposition:SetCyclic(true);
		
		-- Layer Liguid_1
		local element = CreateCompositionElement("data/Texture/Minigames/Fountain/voda.png");
		element:SetName("Liguid_1");
		subComposition:AddElement(element);
		element:SetHotSpot(9.5, 201.5);
		element:SetParentElement(subComposition);
		element:SetXY(871.573028564453, 328.044937133789);
		
		element:SetAngle(-1.570796325);
		element:SetOpacity(1);
		
		effector = element:GetEffector();
		effector:SetLifeTime(0, 4);
		
		effector:SetKey(_TRACK_SX, 1, 1);
		effector:SetKey(_TRACK_SY, 1, 0);
		effector:SetKey(_TRACK_SX, 1.5, 1);
		effector:SetKey(_TRACK_SY, 1.5, 0.32);
		effector:SetMode(HGEANIM_FWD + HGEANIM_NOLOOP);
		effector:Play();
		
		
		-- Layer Liguid_2
		local element = CreateCompositionElement("data/Texture/Minigames/Fountain/voda.png");
		element:SetName("Liguid_2");
		subComposition:AddElement(element);
		element:SetHotSpot(9.5, 201.5);
		element:SetParentElement(subComposition);
		element:SetXY(785.573028564453, 314.044937133789);
		
		element:SetAngle(0);
		element:SetOpacity(1);
		
		effector = element:GetEffector();
		effector:SetLifeTime(0, 4);
		
		effector:SetKey(_TRACK_SX, 1.5, 1);
		effector:SetKey(_TRACK_SY, 1.5, 0.05);
		effector:SetKey(_TRACK_SX, 2.5, 1);
		effector:SetKey(_TRACK_SY, 2.5, 1);
		effector:SetMode(HGEANIM_FWD + HGEANIM_NOLOOP);
		effector:Play();
		
		
		-- Layer Liquid_3
		local element = CreateCompositionElement("data/Texture/Minigames/Fountain/voda.png");
		element:SetName("Liquid_3");
		subComposition:AddElement(element);
		element:SetHotSpot(9.5, 201.5);
		element:SetParentElement(subComposition);
		element:SetXY(890.573028564453, 420.044937133789);
		
		element:SetAngle(0);
		element:SetOpacity(1);
		
		effector = element:GetEffector();
		effector:SetLifeTime(0, 4);
		
		effector:SetKey(_TRACK_SX, 0, 1);
		effector:SetKey(_TRACK_SY, 0, 0.02);
		effector:SetKey(_TRACK_SX, 1, 1);
		effector:SetKey(_TRACK_SY, 1, 0.41);
		effector:SetMode(HGEANIM_FWD + HGEANIM_NOLOOP);
		effector:Play();
 
		return subComposition;
	end
	
    -- Layer Pump_1
    element = CreateCompositionElement("data/Texture/Minigames/Fountain/nasos.png");
    element:SetName("Pump_1");
    composition:AddElement(element);
    element:SetHotSpot(83, 77);

    element:SetParentElement(composition);
    element:SetXY(736, 555);
    
    element:SetScaleX(1);
    element:SetScaleY(1);
    element:SetAngle(0);
    element:SetOpacity(1);
    
    effector = element:GetEffector();
    --effector:SetLifeTime(0, 4);
    
    
    effector:Play();
    
    
    -- Layer Mech_cover
    element = CreateCompositionElement("data/Texture/Minigames/Fountain/Ramka.png");
    element:SetName("Mech_cover");
    composition:AddElement(element);
    element:SetHotSpot(512, 384);
    element:SetParentElement(composition);
    element:SetXY(512, 384);
    
    element:SetScaleX(1);
    element:SetScaleY(1);
    element:SetAngle(0);
    element:SetOpacity(1);
    
    effector = element:GetEffector();
    --effector:SetLifeTime(0, 4);
    
    
    effector:Play();
    
	Compositions[6] = createLevers_4();
	Compositions[6]:SetName("Levers_4");
	composition:AddElement(Compositions[6]);
	Compositions[6]:SetParentElement(composition);
	
    Compositions[5] = createLeversComposition();
    Compositions[5]:SetName("Levers_1");
    composition:AddElement(Compositions[5]);
    Compositions[5]:SetHotSpot(512, 384);
    Compositions[5]:SetParentElement(composition);
    Compositions[5]:SetXY(715, 202);
    
    Compositions[4] = createLevers_water();
	Compositions[4]:SetName("Water");
	composition:AddElement(Compositions[4]);
	Compositions[4]:SetParentElement(composition);
	
	Compositions[3] = createLevers_3();
	Compositions[3]:SetName("Lave3");
	composition:AddElement(Compositions[3]);
	Compositions[3]:SetParentElement(composition);
	
	Compositions[2] = createLevers_2();
	Compositions[2]:SetName("Lave2");
	composition:AddElement(Compositions[2]);
	Compositions[2]:SetParentElement(composition);
	
   Compositions[1] = createLeversChain();
   Compositions[1]:SetName("Chain");
   composition:AddElement(Compositions[1]);
   Compositions[1]:SetParentElement(composition);
		
    -- Layer Bg
    local element = CreateCompositionElement("data/Texture/Minigames/Fountain/Back.png");
    element:SetName("Bg");
    composition:AddElement(element);
    element:SetHotSpot(512, 384);
    element:SetParentElement(composition);
    element:SetXY(512, 384);
    
    element:SetScaleX(1);
    element:SetScaleY(1);
    element:SetAngle(0);
    element:SetOpacity(1);
    
    effector = element:GetEffector();
    --effector:SetLifeTime(0, 4);
    
    effector:Play();
    
    return composition;
end

function ShowAllToolsAppliedComment(currentTool)

    if ((HasGameState('ToolApplied', '22_switch') or currentTool == '22_switch') and
        (HasGameState('ToolApplied', '24_chain')  or currentTool == '24_chain') and
        (HasGameState('ToolApplied', '25_plunger')  or currentTool == '25_plunger') and
		HasGameState('Fountain_Mechanism', 'active') == false)
    then
        ShowMonolog('C_13_FOUNTAIN_MG_TOOLS_APPLIED', 'C_13_FOUNTAIN_MG_TOOLS_APPLIED');
        return true;
    end
    return false;
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
		
    if(obj_name ~= 'switch' and game_active == true) then
	    if not ShowAllToolsAppliedComment()
	    then
	        if(obj_name == 'area_chain' and HasGameState('ToolApplied', '24_chain') == false) then
                ShowMonolog('C_13_FOUNTAIN_MG', 'C_13_FOUNTAIN_MG');
            end
            
            if(obj_name == 'area_lever' and HasGameState('ToolApplied', '22_switch') == false) then
                ShowMonolog('C_13_FOUNTAIN_MG', 'C_13_FOUNTAIN_MG');
            end
            
            if(obj_name == 'area_relay' and HasGameState('ToolApplied', '25_plunger') == false) then
                ShowMonolog('C_13_FOUNTAIN_MG', 'C_13_FOUNTAIN_MG');
            end
        end
	end
        
	DefaultOnObjectMouseDownProcessed(is_wrong_click);
end

function SkipMiniGame()
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_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,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_lever',anim,457,195,300,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_chain',anim,426,493,300,0,50,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_relay',anim,648,559,300,0,30,20)
	local anim = CreateAnimation('data/Texture/Minigames/Fountain/switch.png',0,0,102,155,1,0)
	anim:SetHotSpot(76,130)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('switch',anim,454,197,200,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',anim,486,713,150,0,90,15)
	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('none_object',anim,512,323,100,0,125,80)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
