Advertisement
Starly124

Untitled

Feb 11th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. local Countdown = Class(function(self, inst)
  2. self.inst = inst
  3. self.counting = false
  4.  
  5. self.countdown_max = 0
  6. self.countdown_current = 0
  7.  
  8. self.rate = 1
  9. self.period = 1
  10.  
  11. self.countdown_finish = nil
  12. end)
  13.  
  14. function Countdown:Refresh()
  15. if self.countdown_current > 0 then
  16. self:DoDelta(-self.countdown_current)
  17. end
  18. end
  19.  
  20. function Countdown:OnSave()
  21. if self.countdown_current ~= self.countdown_max then
  22. return {countdown = self.countdown_current}
  23. end
  24. end
  25.  
  26. function Countdown:OnLoad(data)
  27. if data.countdown then
  28. self:StartCountdown(data.countdown_current)
  29. end
  30. end
  31.  
  32. function Countdown:SetRefreshFn(fn)
  33. self.countdown_finish = fn
  34. end
  35.  
  36. function Countdown:IsReady()
  37. return self.countdown_current <= 0
  38. end
  39.  
  40. function Countdown:SetUpdateFn(fn)
  41. self.updatefn = fn
  42. end
  43.  
  44. function Countdown:GetPercent()
  45. if self.countdown_max > 0 then
  46. return math.min(1, self.countdown_current / self.countdown_max)
  47. else
  48. return 0
  49. end
  50. end
  51.  
  52. function Countdown:SetPercent(amount)
  53. local target = (self.countdown_max * amount)
  54. self:DoDelta(target - self.countdown_current)
  55. end
  56.  
  57. function Countdown:DoUpdate(dt)
  58. if self.counting then
  59. self:DoDelta(-dt*self.rate)
  60. end
  61.  
  62. if self:IsReady() then
  63. self:Refresh()
  64. end
  65.  
  66. if self.updatefn then
  67. self.updatefn(self.inst)
  68. end
  69.  
  70. end
  71.  
  72. function Countdown:StartCountdown()
  73. self.counting = true
  74. if self.task == nil then
  75. self.task = self.inst:DoPeriodicTask(self.period, function() self:DoUpdate(self.period) end)
  76. end
  77. end
  78.  
  79. function Countdown:SetCountdown(amount)
  80. if self.countdown_max < amount then
  81. self.countdown_max = amount
  82. end
  83. self.countdown_current = amount
  84. end
  85.  
  86. function Countdown:DoDelta(delta)
  87. self.countdown_current = math.max(0, math.min(self.countdown_max, self.countdown_current + delta) )
  88. end
  89.  
  90.  
  91.  
  92. function Countdown:StopCounting()
  93. self.counting = false
  94. if self.task then
  95. self.task:Cancel()
  96. self.task = nil
  97. end
  98. end
  99.  
  100. function Countdown:LongUpdate(dt)
  101. self:DoUpdate(dt)
  102. end
  103.  
  104. return Countdown
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement