quilt.lua 915 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. local quilt = {}
  2. function quilt.init()
  3. quilt.threads = {}
  4. quilt.delays = {}
  5. love.update:subscribe(quilt.update)
  6. end
  7. function quilt.add(thread)
  8. quilt.threads[thread] = coroutine.create(thread)
  9. quilt.delays[thread] = 0
  10. return thread
  11. end
  12. function quilt.remove(thread, ...)
  13. if not thread then return end
  14. quilt.threads[thread] = nil
  15. return quilt.remove(...)
  16. end
  17. function quilt.reset(thread)
  18. quilt.delays[thread] = 0
  19. return thread
  20. end
  21. function quilt.update()
  22. for thread, cr in pairs(quilt.threads) do
  23. if quilt.delays[thread] <= lib.tick.rate then
  24. local success, delay = coroutine.resume(cr)
  25. quilt.delays[thread] = delay or 0
  26. if not success then
  27. error(delay)
  28. end
  29. if coroutine.status(cr) == 'dead' then
  30. quilt.remove(thread)
  31. end
  32. else
  33. quilt.delays[thread] = quilt.delays[thread] - lib.tick.rate
  34. end
  35. end
  36. end
  37. return quilt