class 'M3AbilityLogicMatch' (M3AbilityLogicBase)
-------------------------------------------------------------------------------

function M3AbilityLogicMatch:__init(ability, field)
    M3AbilityLogicBase.__init(self, ability, field)
    
    self.fire = nil
end
-------------------------------------------------------------------------------

function M3AbilityLogicMatch:affect(cell)
    M3AbilityLogicBase.affect(self, cell)
    
    local item = cell:getItem()
    self.pField:resetFlyingCharge(cell)
    
    if item:isLocked() then
        self.pField:setItemLocked(item, false, 2)
    else
        item:crash()
    end
    
    self.pField:playSound('m3_match_fire', true)
    self.pField:playSound('m3_match',      true)
end
-------------------------------------------------------------------------------

function M3AbilityLogicMatch:run(spriteInfo, time)
    M3AbilityLogicBase.run(self, spriteInfo, time)
    
    self:_createFireAnimation()
end
-------------------------------------------------------------------------------

function M3AbilityLogicMatch:stop(time)
    self:_destroyFireAnimation(time)
    
    M3AbilityLogicBase.stop(self, time)
end
-------------------------------------------------------------------------------

function M3AbilityLogicMatch:canAffect(cell)
    return M3AbilityLogicBase.canAffect(self, cell) or self:_additionalCanAffect(cell)
end
-------------------------------------------------------------------------------

function M3AbilityLogicMatch:destroy()
    if self.fire then
        if play_field then
            play_field:DoNotIgnoreOnTestPoint(self.fire:GetName())
        end
        self.fire:Destroy()
        self.fire = nil
    end
    
    M3AbilityLogicBase.destroy(self)
end
-------------------------------------------------------------------------------


-------------------------------------------------------------------------------
-- PRIVATE
-------------------------------------------------------------------------------

function M3AbilityLogicMatch:_additionalCanAffect(cell)
     local item = cell:getItem()
    
    return not self.pField.bShuffle    and
            item                       and
            item:isLocked()            and
            item:isCalm()              and
            not item.ability
end
-------------------------------------------------------------------------------

function M3AbilityLogicMatch:_createFireAnimation()
    do
        local objectName = 'abilityIndicatorFire'
        local texture = M3Game.TEXTURE_PATH..'animMatchFire.png'
        
        local anim = CreateAnimation(texture,0,0,74,78,6,24)
        anim:SetHotSpot(37,45)
        anim:SetBlendMode(2)
        anim:SetColor('0xFFFFFFFF')
        
        self.fire = play_field:CreateObject(objectName,anim,41,-40,1,0,1,1)
        self.fire:AttachTo(self.indicator)
        
        play_field:IgnoreOnTestPoint(objectName)
    end
    
    do
        local objectName = 'abilityIndicatorSparkles'
        local texture = M3Game.TEXTURE_PATH..'animMatchSparkles.png'
        
        local anim = CreateAnimation(texture,0,0,162,147,16,24)
        anim:SetHotSpot(81,73)
        anim:SetBlendMode(2)
        anim:SetColor('0xFFFFFFFF')
        
        self.sparkles = play_field:CreateObject(objectName,anim,41,-40,1,0,1,1)
        self.sparkles:AttachTo(self.indicator)
        
        play_field:IgnoreOnTestPoint(objectName)
    end
    
    do
        local time = GetAnimationLength(self.sparkles:GetAnimation())
        local effector = CreateAnimationEffector()
        
        effector:SetDestroysOnEnd(false)
        effector:AttachTo(self.sparkles)
        effector:SetKey(_TRACK_O, 0,           1)
        effector:SetKey(_TRACK_O, time * 0.99, 1)
        effector:SetKey(_TRACK_O, time,        0)
        effector:Play()
        
        self.sparkleEffector = effector
    end
    
    do
        local effector = CreateAnimationEffector()
        effector:SetDestroysOnEnd(false)
        effector:AttachTo(self.fire)
        
        effector:SetKey(_TRACK_Y, 0.08333333333333, -40)
        effector:SetKey(_TRACK_Y, 0.25, -43)
        effector:SetKey(_TRACK_SX, 0.08333333333333, 0.62569064117024)
        effector:SetKey(_TRACK_SY, 0.08333333333333, 0.26626681351021)
        effector:SetKey(_TRACK_SX, 0.25, 1.37837837837838)
        effector:SetKey(_TRACK_SY, 0.25, 1.97435897435897)
        effector:SetKey(_TRACK_O, 0.04166666666667, 0)
        effector:SetKey(_TRACK_O, 0.08333333333333, 1)
        effector:Play()
        
        self.fireEffector = effector
    end
end
-------------------------------------------------------------------------------

function M3AbilityLogicMatch:_destroyFireAnimation(time)
    time = time or M3AbilityLogicBase.APPEARING_TIME
    
    if self.fireEffector then
        self.fireEffector:Destroy()
        self.fireEffector = nil
    end
    
    if self.sparkleEffector then
        self.sparkleEffector:Destroy()
        self.sparkleEffector = nil
    end
    
    if self.sparkles then
        if play_field then
            play_field:DoNotIgnoreOnTestPoint(self.sparkles:GetName())
        end
        self.sparkles:Destroy()
        self.sparkles = nil
    end
    
    if time == 0 then
        if self.fire then
            if play_field then
                play_field:DoNotIgnoreOnTestPoint(self.fire:GetName())
            end
            self.fire:Destroy()
            self.fire = nil
        end
    else
        self.fire:ChangeOpacity(1, 0, time)
        
        self.pField.timer:Add( function()
                                    if self.fire then
                                        if play_field then
                                            play_field:DoNotIgnoreOnTestPoint(self.fire:GetName())
                                        end
                                        self.fire:Destroy()
                                        self.fire = nil
                                    end
                                end,
                                time)
    end
end
-------------------------------------------------------------------------------