local zoom_flock;
local zoom_rail;

function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/13_Crimeplace_Main.lua', 'quest', 'data/Scenes/13_Crimeplace_Street.lua')
    
    game_state.BedroomQuestCompleted = HasGameState('ActionCompleted', '17_BedroomQuestCompleted');
    
    zoom_flock = CreateZoomZone('area_flock',
                                'CloseZoneFlock',
                                'ZOOM_flock',   true,
                                'Flock_layer',  true);
    
    zoom_flock.on_open = function()
--        ShowMonolog('C_13_FLACK_HINT', 'C_13_FLACK_HINT');
    end
    
    zoom_rail  = CreateZoomZone('area_rail',
                                'CloseZoneRail',
                                'ZOOM_rail',    true,
                                'area_blood',   true);
    
    zoom_rail.spider = CreateSpider();
    zoom_rail.spider:AttachTo( play_field:GetObject('ZOOM_rail') );
    zoom_rail.spider:SetDestroysOnEnd(false);
    zoom_rail.spider:SetXY(-320, -240);
    zoom_rail.spider:SetClippingRect(512-320, 384-240, 640, 480)
    zoom_rail.spider:SetZ(1);
    
    zoom_rail.on_open = function()
--        ShowMonolog('C_13_RAIL_HINT', 'C_13_RAIL_HINT');
        zoom_rail.spider:Play();
    end
    
    play_field:GetObject('area_blood'):GetAnimation():SetColor('0x01FFFFFF');
    play_field:GetObject('area_flock'):SetVisible(false);
    play_field:GetObject('area_rail' ):SetVisible(false);
    play_field:GetObject('area_workshop'):SetVisible(false);
    play_field:GetObject('area_lock'):SetVisible(false);
    
    RegisterToolApplyInfo('07_pincers', 'Flock_layer', nil, nil,    function(tool_name)
                                                                        zoom_flock.Disable();
                                                                        zoom_flock.SetVisible(false);
                                                                        
                                                                        PlaySound('13_Pincers_Apply')
                                                                        FindTool('13_flock');
                                                                        
                                                                        if HasGameState('ToolApplied', '07_pipette') then
                                                                            AddNoteToNotebook('ENTRYWAY_3');
                                                                        end
                                                                        
                                                                        game_state.flock_found = true;
                                                                    end,
                                                                    function(tool_name)
                                                                        zoom_flock.Disable();
                                                                                                                                                
                                                                        game_state.flock_found = true;
                                                                    end);
                                                                    
    RegisterToolApplyInfo('07_pipette', 'area_blood', nil, nil,     function(tool_name)
                                                                        zoom_rail.Disable();
                                                                        zoom_rail.SetVisible(false);
                                                                        
                                                                        if HasGameState('ToolApplied', '07_pincers') then
                                                                            AddNoteToNotebook('ENTRYWAY_3');
                                                                        end
                                                                        
                                                                        PlaySound('13_PipetteApply');
                                                                        FindTool('13_blood_pipette');
                                                                    end,
                                                                    function(tool_name)
                                                                        zoom_rail.Disable();
                                                                    end);
    
    DoAfterEnterComics();
end

function Run13FinalComics()
   local final_comics = CreateComics('13_OnFinish',
                        CreateComicsFrame('Dupen', 'C_17_FINAL3_DUPEN',  'body', 'head1'));
    
    if final_comics then
        final_comics.on_finish = function()
            game_state.parent_scene = nil;
            play_field:TimerDoString("CompleteLevel('Atelier');", 0.5);
        end
        final_comics.SetVisible(true);
    else
        game_state.parent_scene = nil;
        play_field:TimerDoString("CompleteLevel('Atelier');", 0.5);
    end
end

function DoAfterEnterComics()
    LinkScene(  'area_ho',
                'CursorGlass',
                'data/Scenes/13_Crimeplace_Main_HO.lua',
                true);
    
    LinkScene(  'area_lock',
                'CursorGlass',
                'data/Scenes/Minigames/13_lock.lua',
                false);
                
    LinkScene(  'area_workshop',
                'CursorUpLeft',
                'data/Scenes/15_Workshop_Main.lua',
                false);
    
    LinkScene(  'area_corridor',
                'CursorUpLeft',
                'data/Scenes/14_Corridor_Main.lua',
                false);
    
    LinkScene(  'area_apartment',
                'CursorRight',
                'data/Scenes/18_Apartment_Main.lua',
                false);
    
    
    play_field:GetObject('area_flock'):SetVisible(true);
    play_field:GetObject('area_rail' ):SetVisible(true);
    
    if HasGameState('MiniGameCompleted', '13_lock') then
        play_field:GetObject('area_workshop'):SetVisible(true);
        
        local value = game_state.scene_path.."(WorkshopDoorOpened)";
        
        if not HasGameState('ActionCompleted', value) then
            AddGameState('ActionCompleted', value)
            
            PlaySound('13_WorkshopOpen')
            
            TimerDoString("OpenWorkShopDoor()", ZOOM_ZONE_APPEARING_TIME, play_field:GetTimer());
        else
            play_field:GetObject('Door_close'):SetOpacity(0);
        end
    else
        play_field:GetObject('area_lock'):SetVisible(true);
    end
end

function OpenWorkShopDoor()
    play_field:GetObject('Door_close'):ChangeOpacity(1, 0, OPEN_DOOR_TIME);
end

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

function OnUpdate(dt)
    
    if not game_state.is_completed
        and game_state.BedroomQuestCompleted
        and game_state.flock_found
        and game_state.fly_tools:GetNReferences() == 0
        and game_state.locker:GetNReferences() == 0
    then
        Run13FinalComics();
        
        game_state.is_completed = true;
    end
    
    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 == 'ZOOM_flock'
    or obj_name == 'Flock_layer'
    then
        ShowMonolog('C_13_FLACK_HINT', 'C_13_FLACK_HINT');
    end
    
    if obj_name == 'ZOOM_rail'
    or obj_name == 'area_blood'
    then
        ShowMonolog('C_13_RAIL_HINT', 'C_13_RAIL_HINT');
    end

    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)
end

function OnSceneDestroy(playfield)
    DefaultOnSceneDestroy(playfield)
end

function CreateSpider()
    local composition = CreateCompositionSequence();
    local compositionDuration = 3.4034034034034;
    composition:SetDuration(compositionDuration);


    -- Layer Layer_1
    local element = CreateCompositionElement("data/Texture/Scenes/13_Crimeplace/Zone_main/Zoom_rail/Animations/spider_anim.png");
    element:SetName("Spider_anim");
    composition:AddElement(element);
        element:GetTexture():SetTextureRect(0,0,89,123);
        element:GetTexture():SetFrames(10);
        element:GetTexture():SetSpeed(12);
        element:GetTexture():SetMode(HGEANIM_FWD + HGEANIM_LOOP);
        element:GetTexture():Play();
    element:SetHotSpot(44.5, 61.5);
    element:SetParentElement(composition);
    element:SetScaleX(1);
    element:SetScaleY(1);
    element:SetAngle(0);
    element:SetOpacity(1);
    

    effector = element:GetEffector();
    effector:SetLifeTime(0, 3.4034034034034);

    effector:SetKey(_TRACK_X, 0, 206);
    effector:SetKey(_TRACK_Y, 0, 557);
    effector:SetKey(_TRACK_X, 0.66733400066733, 290.663120557444);
    effector:SetKey(_TRACK_Y, 0.66733400066733, 400.85119099551);
    effector:SetKey(_TRACK_X, 1.4014014014014, 343.811690157517);
    effector:SetKey(_TRACK_Y, 1.4014014014014, 311.694241178774);
    effector:SetKey(_TRACK_X, 2.53586920253587, 411.036902139307);
    effector:SetKey(_TRACK_Y, 2.53586920253587, 159.890093244675);
    effector:SetKey(_TRACK_X, 3.33667000333667, 588);
    effector:SetKey(_TRACK_Y, 3.33667000333667, -76);

    effector:Play();

    composition:Play();

    return composition;
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/13_Crimeplace/Zone_main/Layers/background.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('background',anim,512,384,100,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_ho',anim,447,379,101,0,7,10)
	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_workshop',anim,155,393,102,0,15,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_lock',anim,153,428,103,0,10,10)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area_circle.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_corridor',anim,470,104,103,0,3,2)
	local anim = CreateAnimation('data/Texture/Scenes/13_Crimeplace/Zone_main/Zoom_rail/ZOOM_rail.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('ZOOM_rail',anim,512,384,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/13_Crimeplace/Zone_main/Zoom_flock/ZOOM_flock.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('ZOOM_flock',anim,512,384,160,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area_circle.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_rail',anim,654,447,101,0,0.6,0.6)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area_circle.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_flock',anim,680,328,101,0,0.6,0.6)
	local anim = CreateAnimation('data/Texture/Scenes/13_Crimeplace/Zone_main/Zoom_flock/Flock_layer.png',0,0,165,115,1,0)
	anim:SetHotSpot(82,57)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Flock_layer',anim,301,513,161,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_apartment',anim,964,405,101,0,15,40)
	local anim = CreateAnimation('data/Texture/Scenes/13_Crimeplace/Zone_main/Layers/Door_close.png',0,0,161,319,1,0)
	anim:SetHotSpot(80,159)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Door_close',anim,116,400,101,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_blood',anim,584,382,151,299,50,20)
	local anim = CreateAnimation('data/Texture/Scenes/Common/Animations/candle.png',0,0,20,40,25,20)
	anim:SetHotSpot(10,20)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('candle_flame1',anim,372,156,102,0,0.8,0.8)
	local anim = CreateAnimation('data/Texture/Scenes/Common/Animations/candle.png',0,0,20,40,25,18)
	anim:SetHotSpot(10,20)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('candle_flame2',anim,383,163,102,0,0.8,0.8)
	local anim = CreateAnimation('data/Texture/Scenes/Common/Animations/candle.png',0,0,20,40,25,16)
	anim:SetHotSpot(10,20)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('candle_flame3',anim,452,170,102,0,0.8,0.8)
	local anim = CreateAnimation('data/Texture/Scenes/Common/Animations/candle.png',0,0,20,40,25,21)
	anim:SetHotSpot(10,20)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('candle_flame4',anim,466,152,102,0,0.8,0.8)
	local anim = CreateAnimation('data/Texture/Scenes/Common/Animations/candle.png',0,0,20,40,25,19)
	anim:SetHotSpot(10,20)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('candle_flame5',anim,487,156,102,0,0.8,0.8)
	local anim = CreateAnimation('data/Texture/Scenes/Common/Animations/candle.png',0,0,20,40,25,17)
	anim:SetHotSpot(10,20)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('candle_flame6',anim,414,164,102,0,0.8,0.8)
	local anim = CreateAnimation('data/Texture/Characters/Dupen/body.png',0,0,376,520,1,0)
	anim:SetHotSpot(188,260)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('body',anim,240,509,120,0,1,1)
	local anim = CreateAnimation('data/Texture/Characters/Dupen/head1.png',0,0,185,244,1,0)
	anim:SetHotSpot(92,122)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('head1',anim,272,203,121,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
