class 'anims_morf'

function anims_morf:__init()
    self.arrObject={}
    self.arrParams={}

    self.arrTimeRand={}
    --MEMORY
    self.flagAlpha=true
    self.exeOneGo01={}
    self.exeOneGo02={}

end

function anims_morf:setsParamsFicker(arrObject,arrParams)
--    self.exeOneGo01=true
--    self.exeOneGo02=true
    self.arrObject=arrObject
    self.arrParams=arrParams
    --  self.arrParams[1]={timeGrow=0,timeReductionMax=0,timeIdle=0,timeReductionMin=0,
    --                    start_timeGrow=0,start_timeReductionMax=0,start_timeIdle=0,start_timeReductionMin=0,
    --                     time=0,minAlpha=0,maxAlpha=0,alpha=0,startAlpha=0}




    for i=1,#self.arrParams do
        if self.arrObject[i] then
            self.arrTimeRand[i]={}
            self:start_rand_inicial(self.arrTimeRand[i],self.arrParams[i])
            self:set_alpha(self.arrObject[i],self.arrParams[i])
            self.exeOneGo01[i]=true
            self.exeOneGo02[i]=true
        end
    end

end

function anims_morf:start_rand_inicial(arTimeRand,arPar)

    arTimeRand[1]= arPar.timeGrow*math.random()          +arPar.start_timeGrow
    arTimeRand[2]=  arPar.timeReductionMax*math.random() +arPar.start_timeReductionMax
    arTimeRand[3]= arPar.timeIdle*math.random()          +arPar.start_timeIdle
    arTimeRand[4]=  arPar.timeReductionMin*math.random() +arPar.start_timeReductionMin
    arPar.alpha= arPar.startAlpha

end

function anims_morf:set_alpha(obj,arrParams)
    obj:set_alpha(arrParams.startAlpha,true)
end

function anims_morf:update(dt)
    if self.flagAlpha then
        for i=1,#self.arrParams do
            if self.arrObject[i] then
                self:blinkObject(self.arrTimeRand[i],self.arrObject[i],self.arrParams[i],dt,i)

            end
        end
    end
end

function anims_morf:blinkObject(timeRand,object,alcount,dt,flag01,flag02,index)
    local dtalpha= math.abs(alcount.maxAlpha-alcount.minAlpha)

    local speedGrow         =dtalpha/timeRand[1]
    --local speedReductionMax =dtalpha/timeRand[2]
    local speedIdle         =dtalpha/timeRand[3]
    -- local speedReductionMin =dtalpha/timeRand[4]


    alcount.time= alcount.time+dt
  if self.exeOneGo02[index] then
     if  alcount.time<alcount.time_wate  then
       return
     else
        self.exeOneGo02[index]=false
        alcount.time=0
     end
  end

  if alcount.time <timeRand[1] and alcount.alpha<alcount.maxAlpha then
        --       print(timeRand[1])
        --       print(alcount.time)
        --       print(alcount.alpha)
        alcount.alpha=alcount.minAlpha+speedGrow*alcount.time
        object:set_alpha(alcount.alpha,true)
        -- print(alcount.time)
    else
            if  alcount.time<timeRand[1] then
               alcount.time=timeRand[1]
                alcount.alpha=alcount.maxAlpha
            end

        if alcount.time <timeRand[1]+timeRand[2]  then
          if self.exeOneGo01[index] then
           if alcount.startAlpha>0.9 then
               self.exeOneGo01[index]=false
               alcount.time = timeRand[1]+timeRand[2]
               alcount.alpha=timeRand[1]+timeRand[2]
           end
          end
          --print('1')
        else
            if alcount.time<timeRand[1]+timeRand[2]+timeRand[3] and alcount.alpha>alcount.minAlpha then
                alcount.alpha=alcount.maxAlpha -speedIdle*(alcount.time-timeRand[1]-timeRand[2])
                object:set_alpha(alcount.alpha,true)
            else
                if  alcount.time<timeRand[1]+timeRand[2]+timeRand[3] then
                    alcount.time=timeRand[1]+timeRand[2]+timeRand[3]
                    alcount.alpha=alcount.minAlpha
               end
                if alcount.time <timeRand[1]+timeRand[2]+timeRand[3]+timeRand[4] then
                   -- print('1')
                else
                    alcount.time=0
                    object:set_alpha(alcount.minAlpha,true)
                    timeRand[1]= alcount.timeGrow*math.random()          +alcount.start_timeGrow
                    timeRand[2]=  alcount.timeReductionMax*math.random() +alcount.start_timeReductionMax
                    timeRand[3]= alcount.timeIdle*math.random()         +alcount.start_timeIdle
                    timeRand[4]= alcount.timeReductionMin*math.random() +alcount.start_timeReductionMin
                end
            end
       end
   end
end
