ui_timer.lua 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. -- An invisible timer element
  2. -- you can define its trigger function and then start it, and after an interval it will fire that function
  3. -- if single is false it will autoreload itself
  4. Timer = {}
  5. Timer.__index = Timer
  6. Timer.ident = "ui_timer"
  7. Timer.name = "Timer"
  8. Timer.updateable = true
  9. function Timer:new(name)
  10. local self = {}
  11. setmetatable(self,Timer)
  12. if name ~= nil then self.name = name end
  13. self.interval = 1
  14. self.t = 1
  15. self.isRunning = false
  16. self.single = true
  17. return self
  18. end
  19. setmetatable(Timer,{__index = Element})
  20. function Timer:start() self.isRunning = true self.t = self.interval end
  21. function Timer:update(dt)
  22. if self.isRunning == true then
  23. self.t = self.t-dt
  24. if self.t<=0 then
  25. self.t = self.interval
  26. self:trigger()
  27. if self.single == true then
  28. self:pause()
  29. end
  30. end
  31. end
  32. end
  33. function Timer:pause() self.isRunning = false end
  34. function Timer:resume() self.isRunning = true end
  35. function Timer:stop() self.isRunning = false self.t = self.interval end
  36. function Timer:trigger() end