RefreshActorHUDTitleCommand.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. local RefreshActorHUDTitleCommand = class('RefreshActorHUDTitleCommand', framework.SimpleCommand)
  2. local sLen = string.len
  3. local MMO = global.MMO
  4. local IsPC = global.isWinPlayMode
  5. local IsAuto = tonumber(SL:GetMetaValue("GAME_DATA", "auto_set_topHat_posY")) == 1
  6. local GetTag = function (type)
  7. return (({
  8. [MMO.ACTOR_MONSTER] = MMO.ACTOR_MONSTER, [MMO.ACTOR_NPC] = MMO.ACTOR_NPC
  9. })[type] or (function ()
  10. return MMO.ACTOR_NONE
  11. end)())
  12. end
  13. local GetAnr1 = function (anim, sfxID)
  14. local sfx = anim:getChildByTag(sfxID)
  15. return sfx and sfx:GetStandPoint() or {x = 0, y = 0}
  16. end
  17. local GetAnr2 = function (sfxID)
  18. local modelProxy = global.Facade:retrieveProxy( global.ProxyTable.ModelConfigProxy )
  19. local config = modelProxy:GetConfig(GetFrameAnimConfigID(sfxID, 0, 4))
  20. return config and {x = config.stand_pos_x or 0.5, y = config.stand_pos_y or 0.5} or {x = 0.5, y = 0.5}
  21. end
  22. -- 以下三个局部变量改变的时候
  23. -- 解决封号显示隐藏后现有机制下动态改变上部称号问题
  24. --[[
  25. 整体结构:
  26. 图片称号
  27. 封号
  28. 血量文本
  29. 血条
  30. ------------ 分割
  31. 前缀文本
  32. 行会信息
  33. 角色名
  34. 血量血条固定 封号称号在上
  35. 下部为分割线为起点 向下排
  36. ]]
  37. local DEFAUTL_HEIGHT = 25
  38. local SPRITE_HUD_OFFX = {
  39. [MMO.HUD_SPRITE_TITLE_1] = -40,
  40. [MMO.HUD_SPRITE_TITLE_2] = -40,
  41. [MMO.HUD_SPRITE_TITLE_3] = -40
  42. }
  43. local animHUDTable = {
  44. [0] = MMO.HUD_NODE_TITLE_1,
  45. [1] = MMO.HUD_NODE_TITLE_2,
  46. [2] = MMO.HUD_NODE_TITLE_3,
  47. [3] = MMO.HUD_NODE_TITLE_4,
  48. [4] = MMO.HUD_NODE_TITLE_5,
  49. [5] = MMO.HUD_NODE_TITLE_6,
  50. [6] = MMO.HUD_NODE_TITLE_7,
  51. [7] = MMO.HUD_NODE_TITLE_8,
  52. [8] = MMO.HUD_NODE_TITLE_9,
  53. [9] = MMO.HUD_NODE_TITLE_10,
  54. [10] = MMO.HUD_NODE_TITLE_11
  55. }
  56. local spriteHUDTable = {
  57. [0] = MMO.HUD_SPRITE_ICON_1,
  58. [1] = MMO.HUD_SPRITE_ICON_2,
  59. [2] = MMO.HUD_SPRITE_ICON_3,
  60. [3] = MMO.HUD_SPRITE_ICON_4,
  61. [4] = MMO.HUD_SPRITE_ICON_5,
  62. [5] = MMO.HUD_SPRITE_ICON_6,
  63. [6] = MMO.HUD_SPRITE_ICON_7,
  64. [7] = MMO.HUD_SPRITE_ICON_8,
  65. [8] = MMO.HUD_SPRITE_ICON_9,
  66. [9] = MMO.HUD_SPRITE_ICON_10,
  67. [10] = MMO.HUD_SPRITE_ICON_11
  68. }
  69. function RefreshActorHUDTitleCommand:ctor()
  70. self._upOffY = 78
  71. self._titleH = 0
  72. end
  73. function RefreshActorHUDTitleCommand:execute(notification)
  74. local data = notification:getBody()
  75. if not data.actorID then
  76. return false
  77. end
  78. local actor = global.actorManager:GetActor(data.actorID)
  79. if not actor then
  80. return false
  81. end
  82. local actorID = actor:GetID()
  83. -- check visible
  84. local visible = actor:GetValueByKey(MMO.HUD_TITLE_VISIBLE)
  85. -- 称号-文本、图片
  86. local titles = GetActorAttrByID(actorID, MMO.ACTOR_ATTR_HUDTITLE)
  87. if titles then
  88. self:loadTitles(actor, titles, visible)
  89. end
  90. -- 顶戴花翎 - 特效、图片
  91. local icons = GetActorAttrByID(actorID, MMO.ACTOR_ATTR_HUDICONS)
  92. if icons then
  93. self:loadTopHat(actor, icons, visible)
  94. end
  95. -- refresh actor position
  96. local pos = actor:getPosition()
  97. actor:setPosition(pos.x, pos.y)
  98. end
  99. -- 称号
  100. function RefreshActorHUDTitleCommand:loadTitles(actor, titles, visible)
  101. local offsetY = 0
  102. local t1 = self:refreshTitleImage(actor, MMO.HUD_SPRITE_TITLE_1, titles.t1, visible)
  103. local l1 = self:refreshTitleLabel(actor, MMO.HUD_LABEL_TITLE_1, titles.t1, visible)
  104. offsetY = (t1 or l1) and offsetY + DEFAUTL_HEIGHT or offsetY
  105. local t2 = self:refreshTitleImage(actor, MMO.HUD_SPRITE_TITLE_2, titles.t2, visible)
  106. local l2 = self:refreshTitleLabel(actor, MMO.HUD_LABEL_TITLE_2, titles.t2, visible)
  107. offsetY = (t2 or l2) and offsetY + DEFAUTL_HEIGHT or offsetY
  108. local t3 = self:refreshTitleImage(actor, MMO.HUD_SPRITE_TITLE_3, titles.t3, visible)
  109. local l3 = self:refreshTitleLabel(actor, MMO.HUD_LABEL_TITLE_3, titles.t3, visible)
  110. offsetY = (t3 or l3) and offsetY + DEFAUTL_HEIGHT or offsetY
  111. self._titleH = self._titleH + offsetY
  112. end
  113. -- 顶戴
  114. function RefreshActorHUDTitleCommand:loadTopHat(actor, icons, visible)
  115. if IsAuto then
  116. self._upOffY = self._upOffY + self._titleH
  117. end
  118. local lastY = 0 -- 顶戴的最后一个高度
  119. for i = 0, 10 do
  120. local params = icons[i + 1]
  121. if params and params.iY then
  122. params.Y = self._upOffY - params.iY
  123. end
  124. -- 记录是否是996盒子专属顶戴
  125. if params and params.iId == 10 then
  126. if lastY ~= 0 then
  127. params.Y = lastY
  128. end
  129. if not IsAuto then
  130. params.Y = math.max(self._titleH + self._upOffY, params.Y)
  131. end
  132. params.isBox996 = true
  133. local Box996Proxy = global.Facade:retrieveProxy(global.ProxyTable.Box996Proxy)
  134. local visible = Box996Proxy:isShowTitleByActor(actor)
  135. self:refreshTopHat(actor, i, params, visible)
  136. else
  137. local titleH, anr = self:refreshTopHat(actor, i, params, visible)
  138. if params and params.after ~= 1 then
  139. local Y = params.Y or 0
  140. local aY = anr and anr.y or 0
  141. lastY = math.max(lastY, math.ceil(Y + titleH * aY))
  142. end
  143. end
  144. end
  145. end
  146. -- 称号图标
  147. function RefreshActorHUDTitleCommand:refreshTitleImage(actor, hudIndex, titleID, visible)
  148. local actorID = actor:GetID()
  149. local imagePath = ""
  150. if titleID and titleID > 0 then
  151. local PlayerTitleProxy = global.Facade:retrieveProxy(global.ProxyTable.PlayerTitleProxy)
  152. imagePath = PlayerTitleProxy:getSceneTitleImage(titleID)
  153. end
  154. if sLen(imagePath) < 1 then
  155. global.HUDManager:RemoveHUD(actorID, MMO.HUD_TYPE_SPRITE, hudIndex)
  156. return false
  157. end
  158. -- reserved: 0 图标显示在左边; 1 图标显示在中间; 2不显示图标
  159. local ItemConfigProxy = global.Facade:retrieveProxy(global.ProxyTable.ItemConfigProxy)
  160. local reserved = ItemConfigProxy:GetItemReservedByIndex(titleID)
  161. if reserved == 2 then
  162. global.HUDManager:RemoveHUD(actorID, MMO.HUD_TYPE_SPRITE, hudIndex)
  163. return false
  164. end
  165. local oY = IsPC and 0 or 5
  166. local offsetX = reserved == 1 and 0 or (SPRITE_HUD_OFFX[hudIndex] or 0)
  167. local offsetY = self._upOffY + oY
  168. local p = cc.p(offsetX, offsetY)
  169. local sprite = global.HUDManager:GetHUD(actorID, MMO.HUD_TYPE_SPRITE, hudIndex)
  170. if not sprite then
  171. sprite = global.HUDManager:CreateHUD(actorID, MMO.HUD_TYPE_SPRITE, hudIndex, p)
  172. sprite:setAnchorPoint(0.5, 0.5)
  173. end
  174. global.HUDManager:SetHUDOffset(actorID, MMO.HUD_TYPE_SPRITE, hudIndex, p)
  175. sprite:setTexture(imagePath)
  176. sprite:setTag(GetTag(actor:GetType()))
  177. sprite:setVisible(visible)
  178. return visible
  179. end
  180. -- 称号文本
  181. function RefreshActorHUDTitleCommand:refreshTitleLabel(actor, hudIndex, titleID, visible)
  182. local actorID = actor:GetID()
  183. local hudType = MMO.HUD_TYPE_BATCH_LABEL
  184. local color = MMO.HUD_COLOR_DEFAULT
  185. local itemName = ""
  186. local reserved = 0
  187. if titleID then
  188. local ItemConfigProxy = global.Facade:retrieveProxy(global.ProxyTable.ItemConfigProxy)
  189. color = ItemConfigProxy:GetItemNameColorId(titleID)
  190. itemName = ItemConfigProxy:GetItemNameByIndex(titleID)
  191. reserved = ItemConfigProxy:GetItemReservedByIndex(titleID)
  192. end
  193. -- reserved: 0 显示称号名字; 否则不显示称号名字
  194. if reserved > 0 or sLen(itemName) < 1 then
  195. global.HUDManager:RemoveHUD(actorID, hudType, hudIndex)
  196. return false
  197. end
  198. local p = cc.p(0, self._upOffY)
  199. local hudLabel = global.HUDManager:GetHUD(actorID, hudType, hudIndex)
  200. if not hudLabel then
  201. hudLabel = global.HUDManager:CreateHUD(actorID, hudType, hudIndex, p)
  202. hudLabel:setAlignment(cc.TEXT_ALIGNMENT_CENTER, cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM)
  203. end
  204. global.HUDManager:SetHUDOffset(actorID, hudType, hudIndex, p)
  205. hudLabel:setString(itemName)
  206. hudLabel:setColor(GET_COLOR_BYID_C3B(color))
  207. hudLabel:setVisible(visible)
  208. return visible
  209. end
  210. function RefreshActorHUDTitleCommand:refreshTopHat(actor, idx, params, visible)
  211. params = params or {}
  212. local efftype = params.iEff or 0 -- 0:图片; 1:特效
  213. local effSrc = params.sEffSrc or "" -- 图片名字或者特效ID
  214. local x = params.iX or 0 -- X
  215. local y = params.Y or 0 -- Y
  216. local isBox996 = params.isBox996 or false -- 是否是盒子顶戴
  217. local isBehide = params.after == 1 -- after:1 在后面播放; 否则在前面
  218. if isBehide then
  219. y = y - MMO.PLAYER_AVATAR_OFFSET.y
  220. end
  221. local actorID = actor:GetID()
  222. local hudSpriteIndex = spriteHUDTable[idx]
  223. local hudAnimIndex = animHUDTable[idx]
  224. -- 顶戴的高度(图片读取真实高度, 特效读取默认高度DEFAUTL_HEIGHT)
  225. local p = cc.p(x, y)
  226. if efftype > 0 then
  227. -- 先移除旧的图片
  228. global.HUDManager:RemoveHUD(actorID, MMO.HUD_TYPE_SPRITE, hudSpriteIndex)
  229. local animID = tonumber(effSrc) or -1
  230. if animID < 0 then
  231. global.HUDManager:RemoveHUD(actorID, MMO.HUD_TYPE_ANIM, hudAnimIndex)
  232. return 0
  233. end
  234. local hudTitleH = DEFAUTL_HEIGHT
  235. local hudAnimName = "HUD_ANIM_" .. animID
  236. local hudAnim = global.HUDManager:GetHUD(actorID, MMO.HUD_TYPE_ANIM, hudAnimIndex, isBehide)
  237. if hudAnim and hudAnimName == hudAnim:getName() then
  238. global.HUDManager:SetHUDOffset(actorID, MMO.HUD_TYPE_ANIM, hudAnimIndex, p)
  239. return hudTitleH, GetAnr1(hudAnim, animID)
  240. end
  241. -- 移除旧的
  242. if hudAnim then
  243. global.HUDManager:RemoveHUD(actorID, MMO.HUD_TYPE_ANIM, hudAnimIndex)
  244. end
  245. -- 添加新的
  246. hudAnim = global.HUDManager:CreateHUD(actorID, MMO.HUD_TYPE_ANIM, hudAnimIndex, p, animID, isBehide)
  247. hudAnim:setName(hudAnimName)
  248. global.HUDManager:SetHUDOffset(actorID, MMO.HUD_TYPE_ANIM, hudAnimIndex, p)
  249. hudAnim.isBox996 = isBox996
  250. hudAnim:setTag(GetTag(actor:GetType()))
  251. hudAnim:setVisible(visible)
  252. return hudTitleH, GetAnr2(animID)
  253. else
  254. -- 先移除旧的特效
  255. global.HUDManager:RemoveHUD(actorID, MMO.HUD_TYPE_ANIM, hudAnimIndex)
  256. local picPath = effSrc
  257. if sLen(picPath) < 1 or tonumber(picPath) == -1 then
  258. global.HUDManager:RemoveHUD(actorID, MMO.HUD_TYPE_SPRITE, hudSpriteIndex)
  259. return 0
  260. end
  261. local hudSprite = global.HUDManager:GetHUD(actorID, MMO.HUD_TYPE_SPRITE, hudSpriteIndex, isBehide)
  262. if not hudSprite then
  263. hudSprite = global.HUDManager:CreateHUD(actorID, MMO.HUD_TYPE_SPRITE, hudSpriteIndex, p, 0, isBehide)
  264. hudSprite:setAnchorPoint({x = 0.5, y = 0.5})
  265. else
  266. global.HUDManager:SetHUDOffset(actorID, MMO.HUD_TYPE_SPRITE, hudSpriteIndex, p)
  267. end
  268. local filePath = string.format("res/Topwear/%s.png", picPath)
  269. hudSprite:setTexture(filePath)
  270. hudSprite.isBox996 = isBox996
  271. hudSprite:setTag(GetTag(actor:GetType()))
  272. hudSprite:setVisible(visible)
  273. return hudSprite:getContentSize().height, {x = 0.5, y = 0.5}
  274. end
  275. return 0
  276. end
  277. return RefreshActorHUDTitleCommand