Newer
Older
ibsystem / ibmanager / logic / scripts / utils / DownCounter.lua
-- class definition
DownCounter = 
{
  -- time point in floating point cpu seconds
  timePoint = nil,
  -- dead time point is determined by adding timePoint to waitPeriod
  waitPeriod = nil,
  -- freeze flag
  freezeFlag = false,
  -- freeze value
  freezeValue = nil
};

DownCounter.__index = DownCounter;

-- constructor
function DownCounter.create()

  local dcnt = {};                -- our new object
  setmetatable(dcnt, DownCounter);    -- make Counter handle lookup
  
  -- initialize our object
  dcnt.timePoint = getClock();
  dcnt.waitPeriod = 0;
  dcnt.freezeFlag = false;
  dcnt.freezeValue = 0;
  
  return dcnt;
 
  end


-- returns time to 0 point in milliseconds. if negative, then 0 point was reached
function DownCounter:timeTo0()

  local result = 0

  if self.freezeFlag then
    result = self.freezeValue
  else
    result = self.timePoint + self.waitPeriod - getClock();
  end
   
  return result;
end

-- returns boolean value that tells if 0 point was reached or not
function DownCounter:elapsed()

  return self:timeTo0() <= 0;

end

-- updates down counter parameters
-- @param waitPeriod period in milliseconds since last reset when down counter will reach 0.
function DownCounter:updateParams(waitPeriod)

  self.waitPeriod = waitPeriod;

end

-- resets counter
function DownCounter:reset()

  self.timePoint = getClock();

end

-- freezes counter
function DownCounter:freeze()

  if (self.freezeFlag == false) then
    self.freezeValue = self:timeTo0();
    self.freezeFlag = true;
  end
  
end

-- freezes counter
function DownCounter:unfreeze()

  if (self.freezeFlag == true) then
    
    self.timePoint = getClock();
    self.waitPeriod = self.freezeValue
    self.freezeFlag = false;
  end
  
end