actorRefreshController.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. local DebugMediator = requireMediator("DebugMediator")
  2. local actorRefreshController = class("actorRefreshController", DebugMediator)
  3. actorRefreshController.NAME = "actorRefreshController"
  4. local optionsUtils = requireProxy( "optionsUtils" )
  5. local REFRESH_TYPE_HUDLABEL = 1
  6. local REFRESH_TYPE_HUDTITLE = 2
  7. local REFRESH_TYPE_FEATURE = 3
  8. local REFRESH_TYPE_HUDBAR = 4
  9. local REFRESH_TYPE_DIRTY_FEATURE = 5
  10. function actorRefreshController:ctor()
  11. actorRefreshController.super.ctor(self)
  12. self._createCache = {}
  13. self._refreshCache = {}
  14. self._npcCreateCache = {}
  15. self._npcRefreshCache = {}
  16. end
  17. function actorRefreshController:destory()
  18. if actorRefreshController.instance then
  19. global.Facade:removeMediator( actorRefreshController.NAME )
  20. actorRefreshController.instance = nil
  21. end
  22. end
  23. function actorRefreshController:Inst()
  24. if not actorRefreshController.instance then
  25. actorRefreshController.instance = actorRefreshController.new()
  26. global.Facade:registerMediator(actorRefreshController.instance)
  27. end
  28. return actorRefreshController.instance
  29. end
  30. function actorRefreshController:listNotificationInterests()
  31. local noticeTable = global.NoticeTable
  32. return
  33. {
  34. noticeTable.ActorOutOfView,
  35. noticeTable.DropItemOutOfView,
  36. noticeTable.ActorEffectOutOfView,
  37. noticeTable.DelayRefreshHUDLabel,
  38. noticeTable.DelayRefreshHUDTitle,
  39. noticeTable.DelayActorFeatureChange,
  40. noticeTable.DelayDirtyFeature,
  41. noticeTable.DelayCreateHUDBar,
  42. }
  43. end
  44. function actorRefreshController:handleNotification(notification)
  45. local noticeTable = global.NoticeTable
  46. local noticeID = notification:getName()
  47. local data = notification:getBody()
  48. if noticeTable.ActorOutOfView == noticeID then
  49. self:OnActorOutOfView(data)
  50. elseif noticeTable.DropItemOutOfView == noticeID then
  51. self:OnDropItemOutOfView(data)
  52. elseif noticeTable.ActorEffectOutOfView == noticeID then
  53. self:OnActorEffectOutOfView(data)
  54. elseif noticeTable.DelayRefreshHUDLabel == noticeID then
  55. self:OnDelayRefreshHUDLabel(data)
  56. elseif noticeTable.DelayRefreshHUDTitle == noticeID then
  57. self:OnDelayRefreshHUDTitle(data)
  58. elseif noticeTable.DelayActorFeatureChange == noticeID then
  59. self:OnDelayActorFeatureChange(data)
  60. elseif noticeTable.DelayDirtyFeature == noticeID then
  61. self:OnDelayDirtyFeature(data)
  62. elseif noticeTable.DelayCreateHUDBar == noticeID then
  63. self:OnDelayCreateHUDBar(data)
  64. end
  65. end
  66. function actorRefreshController:Cleanup()
  67. self._createCache = {}
  68. self._refreshCache = {}
  69. self._npcCreateCache = {}
  70. self._npcRefreshCache = {}
  71. end
  72. function actorRefreshController:CleanupByActorID(actorID)
  73. self._createCache[actorID] = nil
  74. self._refreshCache[actorID] = nil
  75. self._npcCreateCache[actorID] = nil
  76. self._npcRefreshCache[actorID] = nil
  77. end
  78. function actorRefreshController:Tick()
  79. local n = 0
  80. local actorID = nil
  81. for i = 1, 3 do
  82. n, actorID = n + self:nextCreate(self._npcCreateCache)
  83. self:nextRefresh(self._npcRefreshCache, actorID)
  84. end
  85. for i = n+1, 3 do
  86. n, actorID = self:nextCreate(self._createCache)
  87. self:nextRefresh(self._refreshCache, actorID)
  88. end
  89. end
  90. function actorRefreshController:nextCreate(caches)
  91. local actorID = next(caches)
  92. if not actorID then
  93. return 0
  94. end
  95. local cache = caches[actorID]
  96. local item = nil
  97. -- hud bar
  98. item = cache[REFRESH_TYPE_HUDBAR]
  99. if item then
  100. self:OnCreateHUDBar(item)
  101. end
  102. -- feature
  103. item = cache[REFRESH_TYPE_FEATURE]
  104. if item then
  105. global.Facade:sendNotification( global.NoticeTable.ActorFeatureChange, item)
  106. end
  107. -- feature
  108. item = cache[REFRESH_TYPE_DIRTY_FEATURE]
  109. if item then
  110. self:DirtyFeature(item)
  111. end
  112. -- cleanup
  113. caches[actorID] = nil
  114. return 1, actorID
  115. end
  116. function actorRefreshController:nextRefresh(caches, actorID)
  117. local actorID = actorID or next(caches)
  118. if not actorID then
  119. return nil
  120. end
  121. local refresh = caches[actorID]
  122. if not refresh then
  123. return nil
  124. end
  125. local item = nil
  126. -- hud label
  127. item = refresh[REFRESH_TYPE_HUDLABEL]
  128. if item then
  129. if item.hudParam then
  130. local actor = item.actor
  131. optionsUtils:InitHUDVisibleValue(actor, global.MMO.HUD_NAMELabel_VISIBLE)
  132. optionsUtils:InitHUDVisibleValue(actor, global.MMO.HUD_GUILDLabel_VISIBLE)
  133. optionsUtils:InitHUDVisibleValue(actor, global.MMO.HUD_TITLELabel_VISIBLE)
  134. global.Facade:sendNotification( global.NoticeTable.RefreshHUDLabel, item )
  135. global.Facade:sendNotification( global.NoticeTable.RefreshActorNameColor, item )
  136. else
  137. global.Facade:sendNotification( global.NoticeTable.RefreshActorNameColor, item )
  138. end
  139. end
  140. -- hud title
  141. item = refresh[REFRESH_TYPE_HUDTITLE]
  142. if item then
  143. local actor = item.actor
  144. optionsUtils:InitHUDVisibleValue(actor, global.MMO.HUD_TITLE_VISIBLE)
  145. global.Facade:sendNotification( global.NoticeTable.RefreshHUDTitle, item )
  146. end
  147. -- cleanup
  148. caches[actorID] = nil
  149. end
  150. function actorRefreshController:OnActorOutOfView(data)
  151. if not data.actorID or not data.actor then
  152. return nil
  153. end
  154. self:CleanupByActorID(data.actorID)
  155. end
  156. function actorRefreshController:OnDropItemOutOfView(data)
  157. if not data.actorID then
  158. return nil
  159. end
  160. self:CleanupByActorID(data.actorID)
  161. end
  162. function actorRefreshController:OnActorEffectOutOfView(data)
  163. if not data.actorID then
  164. return nil
  165. end
  166. self:CleanupByActorID(data.actorID)
  167. end
  168. function actorRefreshController:OnDelayRefreshHUDLabel(data)
  169. local hudParam = data.hudParam
  170. local actorID = data.actorID
  171. local actor = data.actor
  172. if not actorID or not actor then
  173. return nil
  174. end
  175. local caches = actor:IsNPC() and self._npcRefreshCache or self._refreshCache
  176. if not hudParam and caches[actorID] and caches[actorID][REFRESH_TYPE_HUDLABEL] then
  177. return nil
  178. end
  179. caches[actorID] = caches[actorID] or {}
  180. caches[actorID][REFRESH_TYPE_HUDLABEL] = data
  181. end
  182. function actorRefreshController:OnDelayRefreshHUDTitle(data)
  183. local actorID = data.actorID
  184. local actor = data.actor
  185. if not actorID or not actor then
  186. return nil
  187. end
  188. local caches = actor:IsNPC() and self._npcRefreshCache or self._refreshCache
  189. caches[actorID] = caches[actorID] or {}
  190. caches[actorID][REFRESH_TYPE_HUDTITLE] = data
  191. end
  192. function actorRefreshController:OnDelayActorFeatureChange(data)
  193. local actorID = data.actorID
  194. local actor = data.actor
  195. if not actorID or not actor then
  196. return nil
  197. end
  198. local caches = actor:IsNPC() and self._npcCreateCache or self._createCache
  199. caches[actorID] = caches[actorID] or {}
  200. caches[actorID][REFRESH_TYPE_FEATURE] = data
  201. end
  202. function actorRefreshController:OnDelayCreateHUDBar(data)
  203. local actorID = data.actorID
  204. local actor = data.actor
  205. if not actorID or not actor then
  206. return nil
  207. end
  208. local caches = actor:IsNPC() and self._npcCreateCache or self._createCache
  209. caches[actorID] = caches[actorID] or {}
  210. caches[actorID][REFRESH_TYPE_HUDBAR] = data
  211. end
  212. function actorRefreshController:OnCreateHUDBar(data)
  213. local actorID = data.actorID
  214. local actor = global.actorManager:GetActor(actorID)
  215. if not actorID or not actor then
  216. return false
  217. end
  218. -- HUD for actor
  219. local hpSpriteID = global.MMO.HP_SPRITE_ID
  220. if actor:IsPlayer() and actor:IsMainPlayer() and SL:GetMetaValue("GAME_DATA", "hight_main_player_hp") == 1 then
  221. hpSpriteID = global.MMO.HP_MAIN_SPRITE_ID
  222. end
  223. local hpBorder = global.HUDManager:CreateHUDBar( actorID, global.MMO.HUD_SPRITE_HP_BG, global.MMO.HUD_OFFSET_BG_1, global.MMO.HP_BG_SPRITE_ID )
  224. local hpBar = global.HUDManager:CreateHUDBar( actorID, global.MMO.HUD_SPRITE_HP, global.MMO.HUD_OFFSET_1, hpSpriteID )
  225. actor.mHUDUI[global.MMO.HUDHPUI_BAR] = hpBar
  226. actor.mHUDUI[global.MMO.HUDHPUI_BORDER] = hpBorder
  227. local HP = actor:GetHP()
  228. local maxHP = actor:GetMaxHP()
  229. local percent = HP / maxHP
  230. if global.ConstantConfig.showFewHp == 1 and not actor:GetValueByKey(global.MMO.HUD_BAR_SHOW_FULL_HP) then
  231. local HeroPropertyProxy = global.Facade:retrieveProxy(global.ProxyTable.HeroPropertyProxy)
  232. local heroActor = HeroPropertyProxy:GetHeroActor()
  233. local mainPlayer = global.gamePlayerController:GetMainPlayer()
  234. if actor ~= heroActor and actor ~= mainPlayer then
  235. percent = 1
  236. end
  237. end
  238. global.HUDManager:SetHUDBarPercent( hpBar, percent )
  239. -- HUD MP for actor
  240. local mpBorder = global.HUDManager:CreateHUDBar( actorID, global.MMO.HUD_SPRITE_MP_BG, global.MMO.HUD_OFFSET_BG_2, global.MMO.HP_BG_SPRITE_ID )
  241. local mpBar = global.HUDManager:CreateHUDBar( actorID, global.MMO.HUD_SPRITE_MP, global.MMO.HUD_OFFSET_2, global.MMO.MP_SPRITE_ID )
  242. actor.mHUDUI[global.MMO.HUDMPUI_BAR] = mpBar
  243. actor.mHUDUI[global.MMO.HUDMPUI_BORDER] = mpBorder
  244. local MP = actor:GetMP()
  245. local maxMP = actor:GetMaxMP()
  246. local percent = MP / maxMP
  247. global.HUDManager:SetHUDBarPercent( mpBar, percent )
  248. -- HUD NG for actor
  249. local ngBorder = global.HUDManager:CreateHUDBar( actorID, global.MMO.HUD_SPRITE_NG_BG, global.MMO.HUD_OFFSET_BG_3, global.MMO.HP_BG_SPRITE_ID )
  250. local ngBar = global.HUDManager:CreateHUDBar( actorID, global.MMO.HUD_SPRITE_NG, global.MMO.HUD_OFFSET_3, global.MMO.NG_SPRITE_ID )
  251. actor.mHUDUI[global.MMO.HUDNGUI_BAR] = ngBar
  252. actor.mHUDUI[global.MMO.HUDNGUI_BORDER] = ngBorder
  253. if actor.GetForce and actor.GetMaxForce then
  254. local force = actor:GetForce()
  255. local maxForce = actor:GetMaxForce()
  256. local percent = maxForce == 0 and 0 or (force / maxForce)
  257. global.HUDManager:SetHUDBarPercent( ngBar, percent )
  258. end
  259. -- 刷新坐标
  260. local position = actor:getPosition()
  261. actor:setPosition(position.x, position.y)
  262. global.Facade:sendNotification(global.NoticeTable.RefreshActorSceneOptions, actor)
  263. end
  264. function actorRefreshController:OnDelayDirtyFeature(data)
  265. local actorID = data.actorID
  266. local actor = global.actorManager:GetActor(actorID)
  267. if not actorID or not actor then
  268. return nil
  269. end
  270. local caches = actor:IsNPC() and self._npcCreateCache or self._createCache
  271. caches[actorID] = caches[actorID] or {}
  272. caches[actorID][REFRESH_TYPE_DIRTY_FEATURE] = data
  273. end
  274. function actorRefreshController:DirtyFeature(data)
  275. local actorID = data.actorID
  276. local actor = global.actorManager:GetActor(actorID)
  277. if not actorID or not actor then
  278. return nil
  279. end
  280. actor:dirtyFeature()
  281. end
  282. return actorRefreshController