class 'M3Charge'

M3Charge.uniqueId = 0
M3Charge.CHARGES_Z = 10
-------------------------------------------------------------------------------

function M3Charge:__init(pTimer)
    M3Charge.uniqueId = M3Charge.uniqueId + 1
    
    self.pTimer   = pTimer
    self.anchor   = nil
    self.particle = nil
    self.effector = nil
    self.id       = M3Charge.uniqueId
    
    self:createAnchor()
end
-------------------------------------------------------------------------------

function M3Charge:destroy()
    if self.action then
        self.action:kill()
        self.action = nil
    end
    
    if self.effector then
        self.effector:destroy()
        self.effector = nil
    end
    
    --if play_field then
    --   self.anchor:setRespondable(true)
    --end
    self.anchor:destroy()
    self.anchor = nil
    
    self.pTimer = nil
end
-------------------------------------------------------------------------------

function M3Charge:createAnchor()
	local objectName = 'chargeAnchor_'..tostring(self.id)
	--local anim       = CreateAnimationByTextureName()
	
	--self.anchor = play_field:CreateObject(objectName,anim,0,0,1,0,1,1)

    self.anchor = CreateSpriteEntity('')
    self.anchor:setName(objectName)
	self.anchor:setColor(1, 1, 1, 0)
    self.anchor:setRespondable(false)

    self.particle  = CreateParticleEntity()
    self.particle:setParticle('Data/ParticleSystems/M3Charge.psi', 'Data/Textures/Common/particle_10.png')
    self.particle:attachTo(self.anchor)
    self.particle:setParticlesScale(0.55)
    self.particle:setOrder(0)
    self.particle:setXY(0, 0)
    self.particle:setRespondable(false)
    self.particle:fire()
	
	-- local ps = self.anchor:GetParticleSystem(0)
 --    ps:AffectedByOpacity(true)
	
	-- play_field:IgnoreOnTestPoint(objectName)
end
-------------------------------------------------------------------------------

function M3Charge:getAnchor()
    return self.anchor
end
-------------------------------------------------------------------------------

function M3Charge:fly(targetPos, rect, time)
    if self.action then
        self.action:kill()
        self.action = nil
    end
    
    if self.effector then
        self.effector:destroy()
        self.effector = nil
    end
    
    self.effector = CreateAnimationEffector()
    self.effector:setDestroysOnEnd(false)
    self.effector:attachTo(self.anchor)
    
    do
        local startPos  = Vector2f(self.anchor:getX(), self.anchor:getY())
        local dot1      = Vector2f(rect.x + math.random() * rect.w, rect.y + math.random() * rect.h)
        local dot2      = Vector2f(rect.x + math.random() * rect.w, rect.y + math.random() * rect.h)       

        local keys = HelpersEffect.createSplineBezier(startPos, dot1, targetPos, dot2, 20)
    
        local time_step = time / 20
        for i, position in ipairs(keys) do
            self.effector:setKey(_TRACK_X, (i - 1) * time_step, position.x)
            self.effector:setKey(_TRACK_Y, (i - 1) * time_step, position.y)
        end
    end
    
    self.effector:play()
    self.action = self.pTimer:add(  function()
                                        self.action = nil
                                        
                                        self.effector:destroy()
                                        self.effector = nil
                                    end,
                                    time)
end
-------------------------------------------------------------------------------