actorInViewController.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. local actorInViewController = class('actorInViewController', framework.Mediator)
  2. actorInViewController.NAME = "actorInViewController"
  3. local Max = 3
  4. local Min = 0
  5. local ShowMaxNumInView = 200
  6. function actorInViewController:ctor()
  7. actorInViewController.super.ctor(self, self.NAME)
  8. self._actorCounts = {} -- 位置ID 计数
  9. self._actorPosIDs = {} -- actorID = 位置ID
  10. self._posIDActors = {} -- 位置ID = actorIDs
  11. self._indexs = {} --
  12. end
  13. function actorInViewController:destory()
  14. if actorInViewController.instance then
  15. global.Facade:removeMediator(actorInViewController.NAME)
  16. actorInViewController.instance = nil
  17. end
  18. end
  19. function actorInViewController:Inst()
  20. if not actorInViewController.instance then
  21. actorInViewController.instance = actorInViewController.new()
  22. global.Facade:registerMediator(actorInViewController.instance)
  23. end
  24. return actorInViewController.instance
  25. end
  26. function actorInViewController:onRegister()
  27. actorInViewController.super.onRegister(self)
  28. local function dealAct(actor, act)
  29. -- 移动中视野处理
  30. if actor and IsMoveAction(act) then
  31. global.actorInViewController:AddActor(actor)
  32. global.Facade:sendNotification(global.NoticeTable.RefreshMoveInView, actor)
  33. end
  34. end
  35. global.netPlayerController:AddHandleOnActBegin(dealAct)
  36. global.netPlayerController:AddHandleOnActCompleted(dealAct)
  37. global.netMonsterController:AddHandleOnActBegin(dealAct)
  38. global.netMonsterController:AddHandleOnActCompleted(dealAct)
  39. end
  40. function actorInViewController:GetActorCount()
  41. return global.playerManager:PlayerCountInView() + global.monsterManager:MonsterCountInView()
  42. end
  43. function actorInViewController:GetActorPosSign(actor)
  44. return actor:GetMapX() * 65536 + actor:GetMapY()
  45. end
  46. function actorInViewController:UpdateActor(actor)
  47. -- 移除上个位置
  48. self:DelActor(actor)
  49. -- 处理新位置
  50. local posID = self:GetActorPosSign(actor)
  51. local actorID = actor:GetID()
  52. local count = (self._actorCounts[posID] or 0) + 1
  53. self._actorCounts[posID] = count
  54. self._actorPosIDs[actorID] = posID
  55. self._posIDActors[posID] = self._posIDActors[posID] or {}
  56. self._posIDActors[posID][actorID] = count
  57. self._indexs[posID] = self._indexs[posID] or {}
  58. self._indexs[posID][count] = actor
  59. actor:SetKeyValue("InView", self:IsVisible(actor))
  60. end
  61. function actorInViewController:AddActor(actor)
  62. if not actor then
  63. return false
  64. end
  65. local mainPlayerID = global.gamePlayerController:GetMainPlayerID()
  66. if not mainPlayerID then
  67. return false
  68. end
  69. if actor:GetID() == mainPlayerID then
  70. actor:SetKeyValue("InView", true)
  71. return false
  72. end
  73. -- 自己的宝宝
  74. if actor:GetMasterID() == mainPlayerID then
  75. actor:SetKeyValue("InView", true)
  76. return false
  77. end
  78. if not (actor:IsMonster() or actor:IsPlayer()) then
  79. actor:SetKeyValue("InView", true)
  80. return false
  81. end
  82. if actor:IsDie() or actor:IsDeath() then
  83. actor:SetKeyValue("InView", true)
  84. return false
  85. end
  86. self:UpdateActor(actor)
  87. end
  88. function actorInViewController:IsVisible(actor)
  89. if not actor then
  90. return false
  91. end
  92. if self:GetActorCount() < ShowMaxNumInView then
  93. return true
  94. end
  95. if not (actor:IsMonster() or actor:IsPlayer()) then
  96. return true
  97. end
  98. local posID = self:GetActorPosSign(actor)
  99. if not self._posIDActors[posID] then
  100. return false
  101. end
  102. local count = self._posIDActors[posID][actor:GetID()]
  103. if not count then
  104. return false
  105. end
  106. if count > Min and count < Max then
  107. return true
  108. end
  109. return false
  110. end
  111. function actorInViewController:DelActor(actor)
  112. if not actor then
  113. return false
  114. end
  115. local actorID = actor:GetID()
  116. local posID = self._actorPosIDs[actorID]
  117. if not posID then
  118. return false
  119. end
  120. self._actorPosIDs[actorID] = nil
  121. if self._posIDActors[posID] and self._posIDActors[posID][actorID] then
  122. self._posIDActors[posID][actorID] = nil
  123. end
  124. local count = self._actorCounts[posID] or 0
  125. if count < 2 then
  126. self._actorCounts[posID] = nil
  127. self._posIDActors[posID] = nil
  128. self._indexs[posID] = nil
  129. return false
  130. end
  131. count = count - 1
  132. self._actorCounts[posID] = count
  133. local actor = self._indexs[posID] and self._indexs[posID][count]
  134. if actor and not tolua.isnull(actor) then
  135. global.Facade:sendNotification(global.NoticeTable.RefreshMoveInView, actor)
  136. end
  137. end
  138. function actorInViewController:Cleanup()
  139. self._actorCounts = {}
  140. self._posIDActors = {}
  141. self._actorPosIDs = {}
  142. self._indexs = {}
  143. end
  144. return actorInViewController