class 'l_06_Ship' (LevelEventHandler)

local tool_2 = '06_ship'
local tool_1 = '06_wave'

function l_06_Ship:__init(name)
	LevelEventHandler.__init(self, name)
	
	MakeSoundEntity('06_Ship'):PlaySound(true)
	
	self.return_zone_dheight = 30
	-- animations ----------------------------------
	
	
	-- link scenes ---------------------------------
	
	-- zoom zones ----------------------------------
	for i = 1,6 do
	    Object('light_'..i):SetZ(40)
	    ObjectOpacity('light_'..i,0)
	    Helpers.AttachInPlace(Object('light_'..i),Object('wave_'..i))
	    play_field:IgnoreOnTestPoint('light_'..i)
	end
	self.cur_ship = 1
	self.move_ship = 180
	self.win = 0
	self.time_rot_sun = 11
	self.sign = 1
	self.Pos_ship_x = {173,326,479,632,785}
	Helpers.AttachInPlace(Object('ship'),Object('wave_'..self.cur_ship))
	self.Pos_y = {[1] = 804,[2] = 675,[3] = 550}
	self.Wave_pos = {3,1,2,1,3,2}
	self.Link = {[1] = {2},
                 [2] = {3,5},
                 [3] = {1},
                 [4] = {2,3},
                 [5] = {1},
                 [6] = {4},
                }
    self:Print()
    
    ObjectOpacity('ship',0)
    ObjectOpacity('wave_2',0)
    self:Ignore(true)
    GetCurrentLevel().custom_cursors['area_tool'] = "CursorGlass";
    
    local toolApply = function(e)
        if e.toolName == tool_2 then
            ObjectOpacity('ship',1)
            ObjectOpacity('ship_siluet',0)
        else
            ObjectOpacity('wave_2',1)
        end
        if not e.save then
            MakeSoundEntity('06_mg_tool'):PlaySound(false)
        end
        if IsToolApplied(tool_1) and IsToolApplied(tool_2) then
            self:Ignore(false)
            ObjectOpacity('area_tool',0)
            self:GetPanel():GetAssociatedLuaObject():SetMode(EPanelMode.PM_MINIGAME);
        else
            GetCurrentLevel().custom_cursors['area_tool'] = "CursorGlass";
        end
    end
    
    local clickOnToolArea = function(e)
        if not IsToolApplied(tool_1) then
            return 'BBT_06_MG_NEED_WAVE'
        end
        
        return 'BBT_06_MG_NEED_SHIP'
    end
    
    RegisterToolApplyInfo(tool_2, 'area_tool', toolApply, clickOnToolArea)
    RegisterToolApplyInfo(tool_1, 'area_tool', toolApply, clickOnToolArea)
    self:SaveLoad()
end

function l_06_Ship:OnUpdate(dt)
    if self.time_rot_sun > 7 then
        self.time_rot_sun = 0
        self.sign = self.sign * (-1)
        Object('Sun'):Rotate(math.rad(90),7)
        Object('gull_1'):Rotate(-math.rad(18)*self.sign,7)
        Object('gull_2'):Rotate(-math.rad(25)*self.sign,7)
        Object('gull_3'):Rotate(math.rad(21)*self.sign,7)
        Object('clouds_1'):Move(10*self.sign,20*self.sign,7)
        Object('clouds_2'):Move(10*self.sign,-20*self.sign,7)
    else
        
        self.time_rot_sun = self.time_rot_sun + dt
    end
end

function l_06_Ship:SaveLoad()
    if IsMiniGameCompleted(self.name) then
        Object('ship'):SetScrXY(990,178)
        Object('wave_6'):SetY(self.Pos_y[1])
        self:Ignore(true)
        self:GetPanel():GetAssociatedLuaObject():SetMode(EPanelMode.PM_QUEST);
        CompleteMinigame(0.5)
        return
    end
    if HasGameState("06_Ship_cur_pos") then
        self.cur_ship = Helpers.RestoreObjectFromGameState("06_Ship_cur_pos")
        if self.cur_ship == 6 then
            Object('ship'):SetScrXY(990,178)
            Object('wave_6'):SetY(self.Pos_y[1])
            self:Ignore(true)
            self:GetPanel():GetAssociatedLuaObject():SetMode(EPanelMode.PM_QUEST);
            CompleteMinigame(0.5)
            return
        end
        Helpers.AttachInPlace(Object('ship'),Object('BG'))
        Object('ship'):SetScrX(self.Pos_ship_x[self.cur_ship])
        for i = 1,6 do
            Object('wave_'..i):SetY(self.Pos_y[3])
        end
        Helpers.AttachInPlace(Object('ship'),Object('wave_'..self.cur_ship))
    end
    if HasGameState("06_Ship_pos") then
        self.Wave_pos = Helpers.RestoreObjectFromGameState("06_Ship_pos")
        self:Print()
    end
    
end

function l_06_Ship:Print()
    for i = 1,6 do
        Object('wave_'..i):SetY(self.Pos_y[self.Wave_pos[i]])
    end
end

function l_06_Ship:OnObjectMouseEnter(object)
    if string.find(object:GetName(),'wave_') then
        local i = tonumber(string.sub(object:GetName(),-1))
        ObjectOpacity('light_'..i,1)
    end
end

function l_06_Ship:OnObjectMouseLeave(object)
    if string.find(object:GetName(),'wave_') then
        local i = tonumber(string.sub(object:GetName(),-1))
        ObjectOpacity('light_'..i,0)
    end
end

function l_06_Ship:OnScreenObjectMouseDown(object, key)
    if string.find(object:GetName(),'wave_') then
        MakeSoundEntity('06_mg_wave'):StopSound()
        MakeSoundEntity('06_mg_wave'):PlaySound(false)
        local time = 0.6
        local i = tonumber(string.sub(object:GetName(),-1))
        self:Ignore(true)
        if self.Wave_pos[i] == 3 then
            self.Wave_pos[i] = 1
            Object('wave_'..i):MoveTo(Object('wave_'..i):GetX(),self.Pos_y[self.Wave_pos[i]],time)
        else
            self.Wave_pos[i] = self.Wave_pos[i] + 1
            Object('wave_'..i):MoveTo(Object('wave_'..i):GetX(),self.Pos_y[self.Wave_pos[i]],time)
        end
        for j = 1,#self.Link[i] do
            if self.Wave_pos[self.Link[i][j]] == 3 then
                self.Wave_pos[self.Link[i][j]] = 1
                Object('wave_'..self.Link[i][j]):MoveTo(Object('wave_'..self.Link[i][j]):GetX(),self.Pos_y[self.Wave_pos[self.Link[i][j]]],time)
            else
                self.Wave_pos[self.Link[i][j]] = self.Wave_pos[self.Link[i][j]] + 1
                Object('wave_'..self.Link[i][j]):MoveTo(Object('wave_'..self.Link[i][j]):GetX(),self.Pos_y[self.Wave_pos[self.Link[i][j]]],time)
            end
        end
        if  ( self.cur_ship < 5 and self.Wave_pos[self.cur_ship] == self.Wave_pos[self.cur_ship + 1] and self.Wave_pos[self.cur_ship] == self.Wave_pos[self.cur_ship + 2] ) then
            if self.Wave_pos[self.cur_ship + 2] == 1 then
                self.Wave_pos[self.cur_ship + 2] = 3
            else
                self.Wave_pos[self.cur_ship + 2] = self.Wave_pos[self.cur_ship + 2] - 1
            end
            Object('wave_'..(self.cur_ship + 2)):MoveTo(Object('wave_'..(self.cur_ship + 2)):GetX(),self.Pos_y[self.Wave_pos[(self.cur_ship + 2)]],time)
        end
        if ( self.cur_ship ~= 6 and self.Wave_pos[self.cur_ship] == self.Wave_pos[self.cur_ship + 1] ) then
            self.cur_ship = self.cur_ship + 1
            self.timer:Add(function()
                                     Object('ship'):Move(self.move_ship,0,0.9)
                                     self.timer:Add(function()
                                     end,1)
            end,time)
            time = 1.7
        end
        
        if   self.cur_ship == 6   then
            time = 1.7
            self:GetPanel():GetAssociatedLuaObject():SetMode(EPanelMode.PM_QUEST);
            self.win = 1  
            if self.Wave_pos[self.cur_ship] == 3 then
                 self.timer:Add(function()
                                          
                                          Object('wave_6'):Move(0,-5,0.2)
                                          --Object('ship'):Move(20,0,0.2)
                                          self.timer:Add(function()
                                                                   MakeSoundEntity('06_mg_wave'):PlaySound(false)
                                                                   Object('wave_6'):MoveTo(Object('wave_6'):GetX(),self.Pos_y[1],1.2)
                                                                   Helpers.AttachInPlace(Object('ship'),Object('BG'))
                                          end,0.3)
                                         
                                          CompleteMinigame(1.5)
                                          
                 end,1.6)
            else
                self.timer:Add(function()
                                          MakeSoundEntity('06_mg_wave'):PlaySound(false)
                                          Object('wave_6'):MoveTo(Object('wave_6'):GetX(),self.Pos_y[3],0.6)
                                          Object('ship'):Move(20,0,0.6)
                                          self.timer:Add(function()
                                                                    Helpers.AttachInPlace(Object('ship'),Object('BG'))
                                                                    Object('wave_6'):MoveTo(Object('wave_6'):GetX(),self.Pos_y[1],0.6)
                                          end,1)
                                          CompleteMinigame(3)
                                          
                 end,time + 0.2)
            end 
        end
        
        self.timer:Add(function()
                                Helpers.AttachInPlace(Object('ship'),Object('wave_'..self.cur_ship))
                                 if self.win == 1 then
                                    return
                                 end
                                 self:Ignore(false)
        end,time + 0.1)
        Helpers.SaveObjectToGameState("06_Ship_pos",self.Wave_pos)
        Helpers.SaveObjectToGameState("06_Ship_cur_pos",self.cur_ship)
    end
end

function l_06_Ship:SkipMiniGame()
    self.win = 1
    self:Ignore(true)
    self:GetPanel():GetAssociatedLuaObject():SetMode(EPanelMode.PM_QUEST);
    local time = 3 + (6-self.cur_ship)*0.41
    CompleteMinigame(time)
    self.timer:Add(function()
                             for i = 1,6 do
                                Object('wave_'..i):MoveTo(Object('wave_'..i):GetX(),self.Pos_y[2],0.4)
                             end
                             self.timer:Add(function()
                                                      Object('ship'):Move((6-self.cur_ship)*182 + 25 ,0,(6-self.cur_ship)*0.4)
                                                      self.timer:Add(function()
                                                                               Helpers.AttachInPlace(Object('ship'),Object('wave_6'))
                                                                               Object('wave_6'):MoveTo(Object('wave_6'):GetX(),self.Pos_y[3]-10,0.6)
                                                                               self.timer:Add(function()
                                                                                                        Helpers.AttachInPlace(Object('ship'),Object('BG'))
                                                                                                        Object('wave_6'):MoveTo(Object('wave_6'):GetX(),self.Pos_y[2],0.6)
                                                                               end,0.65)
                                                      end,(6-self.cur_ship)*0.41)
                             end,0.41)
    end,1.5)
    
end

function l_06_Ship:OnMouseMove(previous_pos, current_pos)
end

function l_06_Ship:OnObjectMouseUp(object)
end

function l_06_Ship:OnObjectFound(object)
end

function l_06_Ship:OnToolFound(tool)
end

function l_06_Ship:OnToolApply(tool, object)
end

function l_06_Ship:OnToolAddToInventory(tool)
end

function l_06_Ship:OnObjectReachPanel(object)
end

function l_06_Ship:OnButtonPress(title)
end

function l_06_Ship:OnSetTool(tool)
end

function l_06_Ship:OnLoaded()
end

function l_06_Ship:Ignore(bool)
    if bool then
        for i = 1,6 do
            play_field:IgnoreOnTestPoint('wave_'..i)
        end
    else
        for i = 1,6 do
            play_field:DoNotIgnoreOnTestPoint('wave_'..i)
            ObjectOpacity('light_'..i,0)
        end
    end
end

--------------------------------------------------------------------------------
-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function l_06_Ship:LoadData(screen)
	local anim
	anim = CreateAnimation('data/Texture/Minigames/06_Ship/BG.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG',anim,512,384,10,0,1,1)
	
	anim = CreateAnimation('data/Texture/Minigames/06_Ship/Chajka_1.png',0,0,146,66,1,0)
	anim:SetHotSpot(77,70)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('gull_3',anim,427,143,100,0,1,1)
	
	anim = CreateAnimation('data/Texture/Minigames/06_Ship/Chajka_2.png',0,0,110,66,1,0)
	anim:SetHotSpot(41,65)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('gull_2',anim,362,235,110,0,1,1)
	
	anim = CreateAnimation('data/Texture/Minigames/06_Ship/Chajka_3.png',0,0,174,52,1,0)
	anim:SetHotSpot(87,70)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('gull_1',anim,263,116,100,0,1,1)
	
	anim = CreateAnimation('data/Texture/Minigames/06_Ship/Korablik.png',0,0,280,212,1,0)
	anim:SetHotSpot(140,106)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('ship',anim,173,192,150,0,1,1)
	
	anim = CreateAnimation('data/Texture/Minigames/06_Ship/Oblako_1.png',0,0,312,160,1,0)
	anim:SetHotSpot(156,80)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('clouds_2',anim,838,125,90,0,1,1)
	
	anim = CreateAnimation('data/Texture/Minigames/06_Ship/Oblako_2.png',0,0,336,162,1,0)
	anim:SetHotSpot(168,81)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('clouds_1',anim,649,165,100,0,1,1)
	
	anim = CreateAnimation('data/Texture/Minigames/06_Ship/Sun.png',0,0,226,226,1,0)
	anim:SetHotSpot(113,113)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Sun',anim,153,149,20,0,1,1)
	
	anim = CreateAnimation('data/Texture/Minigames/06_Ship/Volna.png',0,0,308,486,1,0)
	anim:SetHotSpot(154,243)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('wave_1',anim,124,550,100,0,0.85,1.2)
	
	anim = CreateAnimation('data/Texture/Minigames/06_Ship/Volna_light.png',0,0,332,502,1,0)
	anim:SetHotSpot(154,243)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('light_1',anim,108,529,100,0,0.85,1.2)
	
	anim = CreateAnimation('data/Texture/Minigames/06_Ship/Volna.png',0,0,308,486,1,0)
	anim:SetHotSpot(154,243)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('wave_2',anim,293,550,90,0,0.85,1.2)
	
	anim = CreateAnimation('data/Texture/Minigames/06_Ship/Volna.png',0,0,308,486,1,0)
	anim:SetHotSpot(154,243)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('wave_3',anim,444,550,80,0,0.85,1.2)
	
	anim = CreateAnimation('data/Texture/Minigames/06_Ship/Volna.png',0,0,308,486,1,0)
	anim:SetHotSpot(154,243)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('wave_4',anim,614,550,70,0,0.85,1.2)
	
	anim = CreateAnimation('data/Texture/Minigames/06_Ship/Volna.png',0,0,308,486,1,0)
	anim:SetHotSpot(154,243)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('wave_5',anim,754,550,60,0,0.85,1.2)
	
	anim = CreateAnimation('data/Texture/Minigames/06_Ship/Volna.png',0,0,308,486,1,0)
	anim:SetHotSpot(154,243)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('wave_6',anim,903,550,50,0,0.85,1.2)
	
	anim = CreateAnimation('data/Texture/Minigames/06_Ship/Volna_light.png',0,0,332,502,1,0)
	anim:SetHotSpot(154,243)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('light_2',anim,277,530,100,0,0.85,1.2)
	
	anim = CreateAnimation('data/Texture/Minigames/06_Ship/Volna_light.png',0,0,332,502,1,0)
	anim:SetHotSpot(154,243)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('light_3',anim,427,528,100,0,0.85,1.2)
	
	anim = CreateAnimation('data/Texture/Minigames/06_Ship/Volna_light.png',0,0,332,502,1,0)
	anim:SetHotSpot(154,243)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('light_4',anim,598,528,100,0,0.85,1.2)
	
	anim = CreateAnimation('data/Texture/Minigames/06_Ship/Volna_light.png',0,0,332,502,1,0)
	anim:SetHotSpot(154,243)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('light_5',anim,738,528,100,0,0.85,1.2)
	
	anim = CreateAnimation('data/Texture/Minigames/06_Ship/Volna_light.png',0,0,332,502,1,0)
	anim:SetHotSpot(154,243)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('light_6',anim,886,530,100,0,0.85,1.2)
	
	anim = CreateAnimation('data/Texture/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_tool',anim,498,270,1000,0,120,80)
	
	anim = CreateAnimation('data/Texture/Minigames/06_Ship/Korablik_siluet_trensparant.png',0,0,280,212,1,0)
	anim:SetHotSpot(140,106)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('ship_siluet',anim,173,192,102,0,1,1)
	
	anim = CreateAnimation('data/Texture/Minigames/06_Ship/nakladka.png',0,0,956,152,1,0)
	anim:SetHotSpot(478,76)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('nakladka',anim,502,673,500,0,1.1,1.3)
	
end
-- CLEAR_ON_SAVE_END
