Lagado

Timers

Jul 12th, 2019 (edited)
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.29 KB | None | 0 0
  1. -- from https://www.lua.org/pil/11.4.html
  2. local List = {}
  3. function List.new ()
  4.     return {first = 0, last = -1}
  5. end
  6.  
  7. function List.pushleft (list, value)
  8.     local first = list.first - 1
  9.     list.first = first
  10.     list[first] = value
  11. end
  12.  
  13. function List.pushright (list, value)
  14.     local last = list.last + 1
  15.     list.last = last
  16.     list[last] = value
  17. end
  18.  
  19. function List.popleft (list)
  20.     local first = list.first
  21.     if first > list.last then
  22.         return nil
  23.     end
  24.     local value = list[first]
  25.     list[first] = nil        -- to allow garbage collection
  26.     list.first = first + 1
  27.     return value
  28. end
  29.  
  30. function List.popright (list)
  31.     local last = list.last
  32.     if list.first > last then
  33.         return nil
  34.     end
  35.     local value = list[last]
  36.     list[last] = nil         -- to allow garbage collection
  37.     list.last = last - 1
  38.     return value
  39. end
  40.  
  41. -- the lib
  42. local timerList = {}
  43. local timersPool = List.new()
  44.  
  45. function addTimer(callback, ms, loops, label, ...)
  46.     local id = List.popleft(timersPool)
  47.     if id then
  48.         local timer = timerList[id]
  49.         timer.callback = callback
  50.         timer.label = label
  51.         timer.arguments = {...}
  52.         timer.time = ms
  53.         timer.currentTime = 0
  54.         timer.currentLoop = 0
  55.         timer.loops = loops or 1
  56.         timer.isComplete = false
  57.         timer.isPaused = false
  58.         timer.isEnabled = true
  59.     else
  60.         id = #timerList+1
  61.         timerList[id] = {
  62.             callback = callback,
  63.             label = label,
  64.             arguments = {...},
  65.             time = ms,
  66.             currentTime = 0,
  67.             currentLoop = 0,
  68.             loops = loops or 1,
  69.             isComplete = false,
  70.             isPaused = false,
  71.             isEnabled = true,
  72.         }
  73.     end
  74.     return id
  75. end
  76.  
  77. function getTimerId(label)
  78.     local found
  79.     for id = 1, #timerList do
  80.         local timer = timerList[id]
  81.         if timer.label == label then
  82.             found = id
  83.             break
  84.         end
  85.     end
  86.     return found
  87. end
  88.  
  89. function pauseTimer(id)
  90.     if type(id) == 'string' then
  91.         id = getTimerId(id)
  92.     end
  93.  
  94.     if timerList[id] and timerList[id].isEnabled then
  95.         timerList[id].isPaused = true
  96.         return true
  97.     end
  98.     return false
  99. end
  100.  
  101. function resumeTimer(id)
  102.     if type(id) == 'string' then
  103.         id = getTimerId(id)
  104.     end
  105.  
  106.     if timerList[id] and timerList[id].isPaused then
  107.         timerList[id].isPaused = false
  108.         return true
  109.     end
  110.     return false
  111. end
  112.  
  113. function removeTimer(id)
  114.     if type(id) == 'string' then
  115.         id = getTimerId(id)
  116.     end
  117.  
  118.     if timerList[id] and timerList[id].isEnabled then
  119.         timerList[id].isEnabled = false
  120.         List.pushright(timersPool, id)
  121.         return true
  122.     end
  123.     return false
  124. end
  125.  
  126. function clearTimers()
  127.     local timer
  128.     repeat
  129.         timer = List.popleft(timersPool)
  130.         if timer then
  131.             table.remove(timerList, timer)
  132.         end
  133.     until timer == nil
  134. end
  135.  
  136. function timersLoop()
  137.     for id = 1, #timerList do
  138.         local timer = timerList[id]
  139.         if timer.isEnabled and timer.isPaused == false then
  140.             if not timer.isComplete then
  141.                 timer.currentTime = timer.currentTime + 500
  142.                 if timer.currentTime >= timer.time then
  143.                     timer.currentTime = 0
  144.                     timer.currentLoop = timer.currentLoop + 1
  145.                     if timer.loops > 0 then
  146.                         if timer.currentLoop >= timer.loops then
  147.                             timer.isComplete = true
  148.                             if eventTimerComplete ~= nil then
  149.                                 eventTimerComplete(id, timer.label)
  150.                             end
  151.                             removeTimer(id)
  152.                         end
  153.                     end
  154.                     if timer.callback ~= nil then
  155.                         timer.callback(timer.currentLoop, table.unpack(timer.arguments))
  156.                     end
  157.                 end
  158.             end
  159.         end
  160.     end
  161. end
Advertisement
Add Comment
Please, Sign In to add comment