Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Countdown = Class(function(self, inst)
- self.inst = inst
- self.counting = false
- self.countdown_max = 0
- self.countdown_current = 0
- self.rate = 1
- self.period = 1
- self.countdown_finish = nil
- end)
- function Countdown:Refresh()
- if self.countdown_current > 0 then
- self:DoDelta(-self.countdown_current)
- end
- end
- function Countdown:OnSave()
- if self.countdown_current ~= self.countdown_max then
- return {countdown = self.countdown_current}
- end
- end
- function Countdown:OnLoad(data)
- if data.countdown then
- self:StartCountdown(data.countdown_current)
- end
- end
- function Countdown:SetRefreshFn(fn)
- self.countdown_finish = fn
- end
- function Countdown:IsReady()
- return self.countdown_current <= 0
- end
- function Countdown:SetUpdateFn(fn)
- self.updatefn = fn
- end
- function Countdown:GetPercent()
- if self.countdown_max > 0 then
- return math.min(1, self.countdown_current / self.countdown_max)
- else
- return 0
- end
- end
- function Countdown:SetPercent(amount)
- local target = (self.countdown_max * amount)
- self:DoDelta(target - self.countdown_current)
- end
- function Countdown:DoUpdate(dt)
- if self.counting then
- self:DoDelta(-dt*self.rate)
- end
- if self:IsReady() then
- self:Refresh()
- end
- if self.updatefn then
- self.updatefn(self.inst)
- end
- end
- function Countdown:StartCountdown()
- self.counting = true
- if self.task == nil then
- self.task = self.inst:DoPeriodicTask(self.period, function() self:DoUpdate(self.period) end)
- end
- end
- function Countdown:SetCountdown(amount)
- if self.countdown_max < amount then
- self.countdown_max = amount
- end
- self.countdown_current = amount
- end
- function Countdown:DoDelta(delta)
- self.countdown_current = math.max(0, math.min(self.countdown_max, self.countdown_current + delta) )
- end
- function Countdown:StopCounting()
- self.counting = false
- if self.task then
- self.task:Cancel()
- self.task = nil
- end
- end
- function Countdown:LongUpdate(dt)
- self:DoUpdate(dt)
- end
- return Countdown
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement