MonsterAffiliationInfo.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ---@class MonsterAffiliationInfo
  2. MonsterAffiliationInfo = class()
  3. local this = MonsterAffiliationInfo
  4. function this:ctor()
  5. end
  6. function this:Reset()
  7. self.affiliationInfo = {}
  8. self.removeList = {}
  9. if self.timer then
  10. SL:UnSchedule(self.timer)
  11. self.timer = nil
  12. end
  13. end
  14. function this:Init()
  15. self:InitData()
  16. self:RegistMessages()
  17. end
  18. function this:InitData()
  19. self.affiliationInfo = {}
  20. self.removeList = {}
  21. self.tempData = {rid= 0,ownerRid = 0}
  22. end
  23. function this:RegistMessages()
  24. SL:RegisterLuaNetMsg(LuaMessageIdToClient.RES_MONSTER_FIRST_ATTACK_OWNER_CHANGE,self.RES_MONSTER_FIRST_ATTACK_OWNER_CHANGE,self)
  25. end
  26. function this:RES_MONSTER_FIRST_ATTACK_OWNER_CHANGE(id,message)
  27. if not self.affiliationInfo[message.target.id] then
  28. self.affiliationInfo[message.target.id] = {ownerRid = message.owner,endTime= message.endTime,maxHurt=message.maxHurt}
  29. else
  30. self.affiliationInfo[message.target.id].ownerRid = message.owner
  31. self.affiliationInfo[message.target.id].endTime = message.endTime
  32. self.affiliationInfo[message.target.id].maxHurt = message.maxHurt
  33. end
  34. self.tempData.rid = message.target.id
  35. self.tempData.ownerRid = message.owner
  36. self.tempData.maxHurt = message.maxHurt
  37. SL:onLUAEvent(LUA_EVENT_MONSTER_AFFILIATION_CHANGE,self.tempData)
  38. if not self.timer then
  39. self.timer = SL:Schedule(self.timer,0,1,-1,function()
  40. for rid, v in pairs(self.affiliationInfo) do
  41. local isDie = SL:GetMetaValue(EMetaVarGetKey.ACTOR_IS_DIE,rid)
  42. if (v.ownerRid and v.ownerRid ~= 0 and v.endTime*1000 < Time.GetServerTime()) or isDie then
  43. table.insert(self.removeList,rid)
  44. self.tempData.rid = rid
  45. self.tempData.ownerRid = 0
  46. self.tempData.maxHurt = 0
  47. SL:onLUAEvent(LUA_EVENT_MONSTER_AFFILIATION_CHANGE,self.tempData)
  48. end
  49. end
  50. for i, rid in pairs(self.removeList) do
  51. self.affiliationInfo[rid] = nil
  52. end
  53. table.clear(self.removeList)
  54. if table.count(self.affiliationInfo) <= 0 then
  55. if self.timer then
  56. SL:UnSchedule(self.timer)
  57. self.timer = nil
  58. end
  59. end
  60. end)
  61. end
  62. end
  63. ---通过id获取怪物的归属
  64. function this:GetMonsterAffiliationByRid(rid)
  65. if self.affiliationInfo[rid] then
  66. return self.affiliationInfo[rid].ownerRid
  67. end
  68. return 0
  69. end
  70. ---通过id获取怪物的最大仇恨或最高伤害
  71. function this:GetMonsterMaxHurtByRid(rid)
  72. if self.affiliationInfo[rid] then
  73. return self.affiliationInfo[rid].maxHurt
  74. end
  75. return 0
  76. end