class 'l_06_garden_hp_z_frog' (ZoomEventHandler)

function l_06_garden_hp_z_frog:__init(name)
    ZoomEventHandler.__init(self, name)
    self.zone.mgHelpText = 'HELP_06_HP_MMG_FROG'
    
    self.SLOTS_COUNT = 5
    self.MAX_JUMP    = 2
    self.JUMP_TIME   = 0.4
    self.SELECTOR_TIME = 0.1
    
    self.jumpHeights = {50, 100}
    self.bJumping = false
    self.posCorrections = {}
    
    self.frogs   = {}
    self.slots   = {}
    self.default = {1,2,nil,3,4}
    self.correct = {3,4,nil,1,2}
    ---------------
    
    ObjectOpacity('z_frog_f1_jump1', 0)
    ObjectOpacity('z_frog_f1_jump2', 0)
    ObjectOpacity('z_frog_f2_jump1', 0)
    ObjectOpacity('z_frog_f2_jump2', 0)
    
    ObjectOpacity('z_frog_wave', 0)
    ObjectOpacity('z_frog_wave_selector', 0)
    ObjectOpacity('z_frog_lily_3_turn', 0)
    
    ObjectVisible('area_find_wave', false)
    ---------------
    
    self.reset = ScriptCreateButton( Object('z_frog_reset'), 'Button_frog_reset', play_field:GetLevelScreen() )
    self.reset:AttachTo(Object('z_frog_bg'))
    self.reset:SetZ(50)
    self.reset:SetX(-200)
    self.reset:SetY(130)
    ---------------
    
    local function on_click_area(e)
        if e.save then
            return
        end
        
        local slotId   = tonumber(string.sub(e.target, -1))
        local targetId = nil
        
        local slot = self.slots[slotId]
        local frog = self.frogs[slot.frogId]
        
        for i=1, self.MAX_JUMP do
            targetId = slotId + (i * frog.dir)
            
            if self.slots[targetId] and self.slots[targetId].frogId == nil then
                break
            else
                targetId = nil
            end
        end
        
        if targetId then
            self:jumpFrog(slotId, targetId)
        end
    end
    ---------------
    
    -- init
    do
        self:initFrog(1, 'z_frog_f1_1')
        self:initFrog(2, 'z_frog_f1_2')
        self:initFrog(3, 'z_frog_f2_2')
        self:initFrog(4, 'z_frog_f2_1')
        
        self.posCorrections['1_3'] = Vector(0, 10)
        self.posCorrections['1_4'] = Vector(0, 10)
        self.posCorrections['1_5'] = Vector(0, 10)
        self.posCorrections['2_3'] = Vector(0, 10)
        self.posCorrections['2_4'] = Vector(0, 10)
        self.posCorrections['2_5'] = Vector(0, 10)
        
        for i=1, self.SLOTS_COUNT do
            self:initSlot(i, on_click_area)
        end
        
        if self:isCompleted() then
            self:onCompleted(true)
        end
    end
end
-----------------------------

function l_06_garden_hp_z_frog:initFrog(id, name)
    local info  = {}
    info.id     = id
    info.type   = tonumber(string.sub(name, 9, 9))
    info.dir    = (info.type == 1) and 1 or -1
    info.obj    = Object(name)
    info.shadow = Object(name..'_shadow')
    info.selector = Object(name..'_selector')
    
    info.shadowDPos = Vector(info.shadow:GetX() - info.obj:GetX(),
                             info.shadow:GetY() - info.obj:GetY())
    
    info.selector:AttachTo(info.obj)
    info.selector:SetXY(0,0)
    info.selector:SetZ(30)
    info.selector:SetOpacity(0)
    
    play_field:IgnoreOnTestPoint(info.selector:GetName())
    
    self.frogs[id] = info
end
-----------------------------

function l_06_garden_hp_z_frog:initSlot(id, callback)
    local area
    
    do
        local areaName = 'area_click_slot_'..id
        local anim = CreateAnimation('data/Texture/Common/area_round.png',0,0,128,128,1,0)
        anim:SetHotSpot(64,64)
        anim:SetBlendMode(2)
        anim:SetColor('0x01FFFFFF')
        area = play_field:CreateObject(areaName,anim,0,0,0,0,1,1)
        self.zone:AddObject(areaName)
        area:SetZ(100)
        
        RegisterActiveObject(areaName, callback, true)
    end
    
    local info  = {}
    info.id     = id
    info.lily   = Object('z_frog_lily_'..id)
    info.area   = Object('area_click_slot_'..id)
    info.x      = info.lily:GetX()
    info.y      = info.lily:GetY()
    info.frogId = nil
    
    self.slots[id] = info
    
    area:SetX(info.x)
    area:SetY(info.y)
    area:SetVisible(false)
    
    local frogId
    
    if HasGameState('SCENE_06HP_FROG_SLOT_'..id) then
        frogId = tonumber(GetGameState('SCENE_06HP_FROG_SLOT_'..id):Get(0))
    else
        frogId = self.default[id]
    end
    
    self:setFrog(id, frogId)
end
-----------------------------

function l_06_garden_hp_z_frog:setAreaVisible(visible)
    for slotId, slot in ipairs(self.slots) do
        slot.area:SetVisible(visible and slot.frogId ~= nil)
    end
end
-----------------------------

function l_06_garden_hp_z_frog:setFrog(slotId, frogId)
    local slot = self.slots[slotId]
    
    if slot.frogId then
        local frog = self.frogs[slot.frogId]
        
        frog.obj:SetOpacity(0)
        frog.shadow:SetOpacity(0)
        
        slot.area:SetVisible(false)
    end
    
    slot.frogId = frogId
    SetGameState('SCENE_06HP_FROG_SLOT_'..slot.id, tostring(slot.frogId))
    
    if slot.frogId then
        local frog = self.frogs[slot.frogId]
        local posCorrection = self.posCorrections[frog.id..'_'..slot.id]
        
        if posCorrection then
            frog.obj:SetXY(slot.x + posCorrection.x, slot.y + posCorrection.y)
        else
            frog.obj:SetXY(slot.x, slot.y)
        end
        
        frog.shadow:SetXY(frog.obj:GetX() + frog.shadowDPos.x, frog.obj:GetY() + frog.shadowDPos.y)
        
        frog.obj:SetOpacity(1)
        frog.shadow:SetOpacity(1)
        
        slot.area:SetVisible(true)
    end
end
-----------------------------

function l_06_garden_hp_z_frog:jumpFrog(fromId, toId)
    local slot1 = self.slots[fromId]
    local slot2 = self.slots[toId]
    local frog  = self.frogs[slot1.frogId]
    
    slot1.frogId = nil
    slot2.frogId = frog.id
    
    slot1.area:SetVisible(false)
    slot2.area:SetVisible(true)
    
    SetGameState('SCENE_06HP_FROG_SLOT_'..slot1.id, tostring(slot1.frogId))
    SetGameState('SCENE_06HP_FROG_SLOT_'..slot2.id, tostring(slot2.frogId))
    
    local posCorrection1 = self.posCorrections[frog.id..'_'..slot1.id]
    local posCorrection2 = self.posCorrections[frog.id..'_'..slot2.id]
    
    if not posCorrection1 then
        posCorrection1 = Vector(0,0)
    end
    
    if not posCorrection2 then
        posCorrection2 = Vector(0,0)
    end
    
    
    local jumpId  = math.abs(slot2.id - slot1.id)
    local jumpObj = Object('z_frog_f'..frog.type..'_jump'..jumpId)
    local correctedX1 = slot1.x + posCorrection1.x
    local correctedX2 = slot2.x + posCorrection2.x
    local correctedY1 = slot1.y + posCorrection1.y
    local correctedY2 = slot2.y + posCorrection2.y
    
    local jumpX = correctedX1 + (correctedX2 - correctedX1) / 2
    local jumpY = (correctedY1 + correctedY2)/2 - self.jumpHeights[jumpId]
    
    jumpObj:SetXY(jumpX, jumpY)
    frog.selector:ChangeOpacity(frog.selector:GetOpacity(), 0, self.SELECTOR_TIME)
    
    do
        local eff = CreateAnimationEffector()
        eff:SetDestroysOnEnd(true)
        eff:AttachTo(jumpObj)
        eff:SetKey(_TRACK_O, 0,                  0)
        eff:SetKey(_TRACK_O, self.JUMP_TIME / 2, 1)
        eff:SetKey(_TRACK_O, self.JUMP_TIME,     0)
        eff:Play()
    end
    
    do
        local eff = CreateAnimationEffector()
        eff:SetDestroysOnEnd(true)
        eff:AttachTo(frog.obj)
        eff:SetKey(_TRACK_O, 0,                  1)
        eff:SetKey(_TRACK_O, self.JUMP_TIME / 2, 0)
        eff:SetKey(_TRACK_O, self.JUMP_TIME,     1)
        eff:Play()
    end
    
    do
        local eff = CreateAnimationEffector()
        eff:SetDestroysOnEnd(true)
        eff:AttachTo(frog.shadow)
        eff:SetKey(_TRACK_O, 0,                  1)
        eff:SetKey(_TRACK_O, self.JUMP_TIME / 2, 0)
        eff:SetKey(_TRACK_O, self.JUMP_TIME,     1)
        eff:Play()
    end
    
    if not self.sound_jump then
        self.sound_jump = MakeSoundEntity('06_hp_jump')
    end
    
    self.sound_jump:StopSound()
    self.sound_jump:PlaySound(false)
    
    GetCurrentLevel().timer:Add(function()
                                    frog.obj:SetXY(slot2.x + posCorrection2.x, slot2.y + posCorrection2.y)
                                    frog.shadow:SetXY(frog.obj:GetX() + frog.shadowDPos.x, frog.obj:GetY() + frog.shadowDPos.y)
                                end,
                                self.JUMP_TIME / 2)
    
    self:setAreaVisible(false)
    
    GetCurrentLevel().timer:Add(function()
                                    if self:isCompleted() then
                                        self:onCompleted(false)
                                    else
                                        self:setAreaVisible(true)
                                    end
                                end,
                                self.JUMP_TIME)
end
-----------------------------

function l_06_garden_hp_z_frog:isCompleted()
    for id, slot in ipairs(self.slots) do
        if self.correct[id] ~= slot.frogId then
            return false
        end
    end
    
    return true
end
-----------------------------

function l_06_garden_hp_z_frog:onCompleted(save)
    self.reset:Hide()
    
    if not HasActionCompletedState('frogMMGCompleted') then
        AddActionCompletedState('frogMMGCompleted')
    end
    
    if save then
        self:setAreaVisible(false)
        
        ObjectOpacity('z_frog_lily_3',      0)
        ObjectOpacity('z_frog_lily_3_turn', 1)
        ObjectOpacity('z_frog_wave', 1)
        ObjectVisible('area_find_wave', true)
    else
        local time = 0.5
        ObjectOpacity('z_frog_lily_3',      0, time)
        ObjectOpacity('z_frog_lily_3_turn', 1, time)
        ObjectOpacity('z_frog_wave',        1, time)
        
        MakeSoundEntity('06_hp_wave_appearing'):PlaySound(false)
        
        GetCurrentLevel():GetPanelLuaObject():SetMode( EPanelMode.PM_HIDDENPICTURE )
        GetCurrentLevel().timer:Add(function()
                                        ObjectVisible('area_find_wave', true)
                                    end,
                                    time)
    end
end
-----------------------------

function l_06_garden_hp_z_frog:resetFrog()
    for id, slot in ipairs(self.slots) do
        self:setFrog(id, nil)
    end
    
    for id, slot in ipairs(self.slots) do
        self:setFrog(id, self.default[id])
    end
    
    if not self.sound_reset then
        self.sound_reset = MakeSoundEntity('06_hp_reset')
    end
    
    self.sound_reset:StopSound()
    self.sound_reset:PlaySound(false)
end
-----------------------------

function l_06_garden_hp_z_frog:onSlotEnter(slot)
    local frog = self.frogs[slot.frogId]
    
    if frog then
        frog.selector:ChangeOpacity(frog.selector:GetOpacity(), 1, self.SELECTOR_TIME)
    end
end
-----------------------------

function l_06_garden_hp_z_frog:onSlotLeave(slot)
    local frog = self.frogs[slot.frogId]
    
    if frog then
        frog.selector:ChangeOpacity(frog.selector:GetOpacity(), 0, self.SELECTOR_TIME)
    end
end
-----------------------------

function l_06_garden_hp_z_frog:OnObjectMouseEnter(object)
    local objectName = object:GetName()
    
    if objectName == 'area_find_wave' then
        local selector = Object('z_frog_wave_selector')
        selector:ChangeOpacity(selector:GetOpacity(), 1, self.SELECTOR_TIME)
        return
    end
    
    for id, slot in ipairs(self.slots) do
        if slot.area:GetName() == objectName then
            self:onSlotEnter(slot)
            return
        end
    end
end
-----------------------------

function l_06_garden_hp_z_frog:OnObjectMouseLeave(object)
    local objectName = object:GetName()
    
    if objectName == 'area_find_wave' then
        local selector = Object('z_frog_wave_selector')
        selector:ChangeOpacity(selector:GetOpacity(), 0, self.SELECTOR_TIME)
        return
    end
    
    for id, slot in ipairs(self.slots) do
        if slot.area:GetName() == objectName then
            self:onSlotLeave(slot)
            return
        end
    end
end
-----------------------------

function l_06_garden_hp_z_frog:OnButtonPress(title)
	if title == 'Button_frog_reset' then
	    if self.bJumping then
	        ShowBlackBar('BBT_06HP_CANNOT_FROG_RESET')
	    else
	        self:resetFrog()
	    end
	end
end
-----------------------------

function l_06_garden_hp_z_frog:SkipMiniGame()
    for id, slot in ipairs(self.slots) do
        self:setFrog(id, nil)
    end
    
    for id, slot in ipairs(self.slots) do
        self:setFrog(id, self.correct[id])
    end
    
    self:setAreaVisible(false)
    self:onCompleted(false)
end
-----------------------------

--------------------------------------------------------------------------------
-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function l_06_garden_hp_z_frog:LoadData(screen)
	local anim
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/z_frog_bg.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_bg',anim,512,384,50,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/lily/z_frog_lily_1.png',0,0,124,56,1,0)
	anim:SetHotSpot(67,14)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_lily_1',anim,262,379,60,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/lily/z_frog_lily_2.png',0,0,130,60,1,0)
	anim:SetHotSpot(60,12)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_lily_2',anim,382,379,60,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/lily/z_frog_lily_3.png',0,0,114,60,1,0)
	anim:SetHotSpot(55,7)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_lily_3',anim,510,369,60,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/lily/z_frog_lily_4.png',0,0,116,64,1,0)
	anim:SetHotSpot(60,5)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_lily_4',anim,638,371,60,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/lily/z_frog_lily_5.png',0,0,116,62,1,0)
	anim:SetHotSpot(59,7)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_lily_5',anim,768,371,60,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/lily/z_frog_lily_reset.png',0,0,172,64,1,0)
	anim:SetHotSpot(86,32)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_lily_reset',anim,321,485,60,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/frogs/z_frog_f1_jump1.png',0,0,166,64,1,0)
	anim:SetHotSpot(83,32)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_f1_jump1',anim,57,198,62,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/frogs/z_frog_f1_jump2.png',0,0,154,98,1,0)
	anim:SetHotSpot(77,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_f1_jump2',anim,70,251,62,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/frogs/z_frog_f1_selector.png',0,0,90,82,1,0)
	anim:SetHotSpot(45,41)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_f1_1_selector',anim,117,328,80,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/frogs/z_frog_f1_selector.png',0,0,90,82,1,0)
	anim:SetHotSpot(45,41)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_f1_2_selector',anim,121,410,80,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/frogs/z_frog_f2_jump1.png',0,0,170,78,1,0)
	anim:SetHotSpot(85,39)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_f2_jump1',anim,942,198,62,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/frogs/z_frog_f2_jump2.png',0,0,152,112,1,0)
	anim:SetHotSpot(76,56)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_f2_jump2',anim,937,256,62,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/frogs/z_frog_f2_selector.png',0,0,106,82,1,0)
	anim:SetHotSpot(53,41)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_f2_1_selector',anim,926,309,80,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/frogs/z_frog_f2_selector.png',0,0,106,82,1,0)
	anim:SetHotSpot(53,41)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_f2_2_selector',anim,927,395,80,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/frogs/z_frog_f1.png',0,0,72,64,1,0)
	anim:SetHotSpot(36,32)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_f1_1',anim,262,379,61,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/frogs/z_frog_f1.png',0,0,72,64,1,0)
	anim:SetHotSpot(36,32)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_f1_2',anim,382,379,61,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/frogs/z_frog_f1_shadow.png',0,0,88,48,1,0)
	anim:SetHotSpot(44,24)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_f1_1_shadow',anim,262,393,70,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/frogs/z_frog_f1_shadow.png',0,0,88,48,1,0)
	anim:SetHotSpot(44,24)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_f1_2_shadow',anim,382,393,70,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/frogs/z_frog_f2.png',0,0,92,66,1,0)
	anim:SetHotSpot(46,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_f2_1',anim,768,371,61,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/frogs/z_frog_f2.png',0,0,92,66,1,0)
	anim:SetHotSpot(46,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_f2_2',anim,638,371,61,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/frogs/z_frog_f2_shadow.png',0,0,96,46,1,0)
	anim:SetHotSpot(48,23)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_f2_1_shadow',anim,769,390,70,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/frogs/z_frog_f2_shadow.png',0,0,96,46,1,0)
	anim:SetHotSpot(48,23)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_f2_2_shadow',anim,639,390,70,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/lily/z_frog_lily_3_turn.png',0,0,116,74,1,0)
	anim:SetHotSpot(58,37)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_lily_3_turn',anim,511,390,61,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/z_frog_wave.png',0,0,82,78,1,0)
	anim:SetHotSpot(41,39)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_wave',anim,509,367,90,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/06_garden/hp/z_frog/z_frog_wave_selector.png',0,0,100,94,1,0)
	anim:SetHotSpot(50,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_frog_wave_selector',anim,509,367,91,0,1,1)
	
	anim = CreateAnimation('data/Texture/Common/area_round.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_find_wave',anim,510,377,100,0,1,1)
	
end
-- CLEAR_ON_SAVE_END
