class 'M3AbilityCounter'
-------------------------------------------------------------------------------

M3AbilityCounter.uniqueId = 0
-------------------------------------------------------------------------------

function M3AbilityCounter:__init(maxCount)
    self.anchor = nil
    self.dots   = {}
    
    self.baseEffector = nil
    self.rotateAction = nil 
    self.animAction   = nil 
    
    self.count    = 0
    self.maxCount = maxCount
    self.radius   = 75
    
    self.baseAngle  = M_PI
    self.deltaAngle = 22 * M_PI/180
end
-------------------------------------------------------------------------------

function M3AbilityCounter:destroy()
    if self.rotateAction then
        self.rotateAction:kill()
        self.rotateAction = nil
        self:_destroyRotateEffectors()
    end
    
    if self.animAction then
        self.animAction:kill()
        self.animAction = nil
        self:_destroyAnimEffectors()
    end
    
    for i, info in ipairs(self.dots) do
        if play_field then
            play_field:DoNotIgnoreOnTestPoint(info.object:getName())
        end
        info.object:destroy()
    end
    
    self.anchor:destroy()
    self.anchor = nil
    self.dots   = nil
end
-------------------------------------------------------------------------------

function M3AbilityCounter:createDots(anchor, z)
    self.anchor = CreateSpriteEntity('')
    self.anchor:attachTo(anchor)
    self.anchor:setXY(0,0)
    self.anchor:setOrder(z)  

    local textureName = M3Game.TEXTURE_PATH..'Abilities/counter.png'
    
    for i = 1, self.maxCount do
        M3AbilityCounter.uniqueId = M3AbilityCounter.uniqueId + 1
        
        local objectName = 'abilityCounterDot'..M3AbilityCounter.uniqueId

        local anim       = CreateSpriteEntity(textureName)
        anim:setOrder(0)
        anim:setXY(0, 0)
        anim:setName(objectName)
        anim:setRespondable(false)    
        
        local info          = {}
        info.angle          = self.deltaAngle * (i)
        info.baseX          = self.radius * math.cos(info.angle)
        info.baseY          = self.radius * math.sin(info.angle)
        info.object         = anim
        info.effectorRotate = nil
        info.effectorAnim   = nil
        table.insert(self.dots, info)
        
        anim:attachTo(self.anchor)
        anim:setXY(info.baseX, info.baseY)
        anim:setOpacity(0)

        -- change in future
        anim:setVisible(false)
    end
    
    self:_rotate2Base()
end
-------------------------------------------------------------------------------

function M3AbilityCounter:setCount(count, time)
    time = time or 0
    
    if self.animAction then
        self.animAction:kill()
        self.animAction = nil
        self:_destroyAnimEffectors()
    end
    
    for i, info in ipairs(self.dots) do
        local opacity = (i <= count) and 1 or 0
        local dot = info.object
        
        if time == 0 then
            dot:setOpacity(opacity)
        else
            info.effectorAnim = dot:ChangeOpacity(dot:getOpacity(), opacity, time)
            info.effectorAnim:setDestroysOnEnd(false)
        end
    end
    
    if time ~= 0 then
        self.animAction = current_scene:getPlayField().timer:add(  function()
                                                            self.animAction = nil
                                                            self:_destroyAnimEffectors()
                                                        end,
                                                        time)
    end
    
    self.count = count
    self:_rotate2Base(time)
end
-------------------------------------------------------------------------------

function M3AbilityCounter:getCount()
    return self.count
end
-------------------------------------------------------------------------------

function M3AbilityCounter:_rotate2Base(time)
    time = time or 0
    
    if self.rotateAction then
        self.rotateAction:kill()
        self.rotateAction = nil
        self:_destroyRotateEffectors()
    end
    
    local targetAngle = self.baseAngle - math.max(0, self.count - 1) * self.deltaAngle / 2
    local deltaAngle  = targetAngle - self.anchor:getRotationDeg()
    
    if time == 0 then
        self.anchor:setRotationDeg(targetAngle)
    else
        self.baseEffector = self.anchor:Rotate(deltaAngle, time)
        self.rotateAction = current_scene:getPlayField().timer:add(function()
                                                            self.rotateAction = nil
                                                            self:_destroyRotateEffectors()
                                                        end,
                                                        time)
    end
end
-------------------------------------------------------------------------------

function M3AbilityCounter:_destroyRotateEffectors()
    if self.baseEffector then
        self.baseEffector:destroy()
        self.baseEffector = nil
    end
    
    for i, info in ipairs(self.dots) do
        if info.effectorRotate then
            info.effectorRotate:destroy()
            info.effectorRotate = nil
        end
    end
end
-------------------------------------------------------------------------------

function M3AbilityCounter:_destroyAnimEffectors()
    for i, info in ipairs(self.dots) do
        if info.effectorAnim then
            info.effectorAnim:destroy()
            info.effectorAnim = nil
        end
    end
end
-------------------------------------------------------------------------------