class 'anims'

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

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

 self.timeFlag01=false
end

function anims:setsParamsFicker(arrObject,arrParams)
  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])
   end
  end

end

function anims: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:set_alpha(obj,arrParams)
 obj:set_alpha(arrParams.startAlpha)
end

function anims: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)
   end
  end
 end
end

function anims:blinkObject(timeRand,object,alcount,dt)
  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 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)
  -- print(alcount.time)
  else
    if  alcount.time<timeRand[1] then
        alcount.time=timeRand[1]
    end
    if alcount.time <timeRand[1]+timeRand[2]  then


    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)
      else
       if  alcount.time<timeRand[1]+timeRand[2]+timeRand[3] then
              alcount.time=timeRand[1]+timeRand[2]+timeRand[3]
        end
       if alcount.time <timeRand[1]+timeRand[2]+timeRand[3]+timeRand[4] then
       else
        alcount.time =0
         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
