actorHUDHPManager.lua 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. local actorHUDHPManager = class("actorHUDHPManager")
  2. local optionsUtils = requireProxy("optionsUtils")
  3. local Queue = requireUtil("queue")
  4. function actorHUDHPManager:ctor()
  5. self._Queue = Queue.new()
  6. self._Cache = {}
  7. end
  8. function actorHUDHPManager:destory()
  9. if actorHUDHPManager.instance then
  10. actorHUDHPManager.instance = nil
  11. end
  12. end
  13. function actorHUDHPManager:Inst()
  14. if not actorHUDHPManager.instance then
  15. actorHUDHPManager.instance = actorHUDHPManager.new()
  16. end
  17. return actorHUDHPManager.instance
  18. end
  19. function actorHUDHPManager:Update(actor)
  20. if not actor then
  21. return false
  22. end
  23. local actorID = actor:GetID()
  24. if self._Cache[actorID] then
  25. return false
  26. end
  27. self._Cache[actorID] = actor
  28. self._Queue:push(actorID)
  29. if self._timerID then
  30. return false
  31. end
  32. local function callback()
  33. self:Tick()
  34. end
  35. self._timerID = Schedule(callback, 0.01)
  36. callback()
  37. end
  38. function actorHUDHPManager:UpdateNow(actor)
  39. if not actor then
  40. return false
  41. end
  42. self._Cache[actor:GetID()] = nil
  43. global.Facade:sendNotification(global.NoticeTable.RefreshHUDHP, actor)
  44. optionsUtils:refreshHUDHMpLabelVisible(actor)
  45. end
  46. function actorHUDHPManager:Tick()
  47. if self._Queue:size() > 0 then
  48. self:UpdateNow(self._Cache[self._Queue:pop()])
  49. else
  50. self:Cleanup()
  51. end
  52. end
  53. function actorHUDHPManager:DelActor(actor, isUpdate)
  54. if isUpdate then
  55. self:UpdateNow(actor)
  56. else
  57. self._Cache[actor:GetID()] = nil
  58. end
  59. end
  60. function actorHUDHPManager:Cleanup()
  61. if self._timerID then
  62. UnSchedule(self._timerID)
  63. self._timerID = nil
  64. end
  65. self._Cache = {}
  66. self._Queue:clear()
  67. end
  68. return actorHUDHPManager