BuffEntityAvatar.lua 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. local BuffEntityAnim = require("buff/BuffEntityAnim")
  2. local BuffEntityAvatar = class("BuffEntityAvatar",BuffEntityAnim)
  3. local player_bound_box = cc.rect( 0, 0, 48, 64 )
  4. local monster_bound_box = cc.rect( 0, 0, 60, 84 )
  5. function BuffEntityAvatar:ctor(data)
  6. BuffEntityAvatar.super.ctor(self,data)
  7. local BuffProxy = global.Facade:retrieveProxy(global.ProxyTable.Buff)
  8. self._cfgColor = BuffProxy:GetBuffColorByID( self._buffID )
  9. self._cfgSacle = BuffProxy:GetBuffScaleByID( self._buffID )
  10. self._cfgAvatar = BuffProxy:GetBuffAvatarByID( self._buffID )
  11. self._avatarScales = {}
  12. end
  13. function BuffEntityAvatar:OnEnter()
  14. BuffEntityAvatar.super.OnEnter(self)
  15. self:FeatureActor()
  16. end
  17. function BuffEntityAvatar:OnExit()
  18. BuffEntityAvatar.super.OnExit(self)
  19. self:UnFeatureActor()
  20. end
  21. function BuffEntityAvatar:updatePosition()
  22. BuffEntityAvatar.super.updatePosition(self)
  23. end
  24. function BuffEntityAvatar:FeatureActor()
  25. local actor = global.actorManager:GetActor(self._actorID)
  26. if not actor then
  27. return
  28. end
  29. if self._cfgAvatar then
  30. if actor:IsMonster() and self._cfgAvatar.avatarType == 1 and self._cfgAvatar.avatarID then
  31. self._originClothID = actor:GetAnimationID()
  32. actor:SetClothID(self._cfgAvatar.avatarID)
  33. actor:dirtyFeature()
  34. elseif actor:IsPlayer() and not actor:GetHorseMasterID() and not actor:IsHoreseCopilot() then
  35. if global.BuffManager:CheckRmvBuffAvatar(actor:GetID(), self._cfgAvatar) then
  36. actor:SetMonsterFeatureID(0)
  37. actor:updateMonster(0)
  38. end
  39. if self._cfgAvatar.avatarType == 1 then
  40. actor:SetKeyValue("IS_MON_AVATAR_ONLY_WALK", true)
  41. end
  42. actor:SetMonsterFeatureID( self._cfgAvatar.avatarID )
  43. actor:dirtyFeature()
  44. end
  45. end
  46. self._cfgColor = global.BuffManager:CheckBuffColorByID(self._actorID, self._buffID)
  47. self._cfgSacle = global.BuffManager:CheckBuffScale(self._actorID, self._buffID)
  48. self:OnRefreshFeature( self._cfgColor, self._cfgSacle )
  49. local defaultHeight = player_bound_box.height
  50. if actor:IsMonster() then
  51. defaultHeight = monster_bound_box.height
  52. end
  53. -- 调整hud位置
  54. local hudTop = defaultHeight * (self._cfgSacle-1)
  55. local actorpos = actor:getPosition()
  56. local defaultHudTop = actor.GetHUDTop and actor:GetHUDTop() or 0
  57. actor:SetKeyValue(global.MMO.BUFF_SACLE_OFFSET, {x=0, y=hudTop})
  58. global.HUDManager:setPosition(self._actorID, actorpos.x, actorpos.y + defaultHudTop)
  59. end
  60. function BuffEntityAvatar:UnFeatureActor()
  61. local actor = global.actorManager:GetActor(self._actorID)
  62. if not actor then
  63. return
  64. end
  65. actor:SetKeyValue("IS_MON_AVATAR_ONLY_WALK", nil)
  66. local cfgAvatar = global.BuffManager:CheckBuffAvatar(self._actorID,self._buffID)
  67. if actor:IsMonster() then
  68. if cfgAvatar and cfgAvatar.avatarType == 1 and cfgAvatar.avatarID then
  69. actor:SetClothID(cfgAvatar.avatarID)
  70. actor:dirtyFeature()
  71. elseif self._originClothID then
  72. actor:SetClothID(self._originClothID)
  73. actor:dirtyFeature()
  74. self._originClothID = nil
  75. end
  76. elseif actor:IsPlayer() and not actor:GetHorseMasterID() and not actor:IsHoreseCopilot() then
  77. local id = cfgAvatar and cfgAvatar.avatarID
  78. if global.BuffManager:CheckRmvBuffAvatar(actor:GetID(), cfgAvatar) or not id then
  79. actor:SetMonsterFeatureID(0)
  80. actor:updateMonster(0)
  81. end
  82. if id and id > 0 then
  83. if cfgAvatar.avatarType == 1 then
  84. actor:SetKeyValue("IS_MON_AVATAR_ONLY_WALK", true)
  85. end
  86. actor:SetMonsterFeatureID( id )
  87. actor:dirtyFeature()
  88. end
  89. end
  90. self._avatarScales = {}
  91. self._cfgColor = global.BuffManager:CheckBuffColorByID(self._actorID, self._buffID)
  92. self._cfgSacle = global.BuffManager:CheckBuffScale(self._actorID, self._buffID)
  93. self:OnRefreshFeature( self._cfgColor, self._cfgSacle, true )
  94. -- 调整hud位置
  95. local hudTop = actor:GetHUDTop() * (self._cfgSacle-1)
  96. local actorpos = actor:getPosition()
  97. local defaultHudTop = actor.GetHUDTop and actor:GetHUDTop() or 0
  98. actor:SetKeyValue(global.MMO.BUFF_SACLE_OFFSET, {x=0, y=hudTop})
  99. global.HUDManager:setPosition(self._actorID, actorpos.x, actorpos.y + defaultHudTop)
  100. end
  101. function BuffEntityAvatar:OnRefreshFeature( color, scale, isUnFeat )
  102. if not color and not scale then
  103. return
  104. end
  105. local actor = global.actorManager:GetActor(self._actorID)
  106. if not actor then
  107. return
  108. end
  109. if actor:IsPlayer() and (actor:GetHorseMasterID() or actor:IsHoreseCopilot()) then
  110. scale = 1
  111. end
  112. local function featureColor( anim )
  113. if not color then
  114. return
  115. end
  116. local p = global.GLProgramCache:getGLProgram("ShaderPositionTextureColor_noMVP")
  117. anim:setGLProgram(p)
  118. anim:setColor( color )
  119. end
  120. local function featureScale( anim )
  121. if not scale then
  122. return
  123. end
  124. local animID = anim:GetID()
  125. local enlarge = anim:GetEnlarge()
  126. local avatarScale = enlarge * scale
  127. if self._avatarScales[animID] == avatarScale then
  128. return
  129. end
  130. local aScale = anim:getScale()
  131. self._avatarScales[animID] = avatarScale
  132. anim:setScale( avatarScale )
  133. local avatarOffset = global.MMO.PLAYER_AVATAR_OFFSET
  134. local offx = (scale * enlarge - aScale) * avatarOffset.x
  135. local offy = (scale * enlarge - aScale) * avatarOffset.y
  136. local pos = anim:getTurePosition()
  137. anim:setPosition(pos.x + offx, pos.y + offy)
  138. end
  139. local function featureShader( anim )
  140. featureColor( anim )
  141. featureScale( anim )
  142. end
  143. local anim = actor:GetAnimation()
  144. if anim then
  145. featureShader(anim)
  146. end
  147. if actor.GetAnimationShield then
  148. anim = actor:GetAnimationShield()
  149. if anim then
  150. featureShader(anim)
  151. end
  152. end
  153. if actor.GetAnimationShieldEff then
  154. anim = actor:GetAnimationShieldEff()
  155. if anim then
  156. featureShader(anim)
  157. end
  158. end
  159. if actor.GetAnimationWings then
  160. anim = actor:GetAnimationWings()
  161. if anim then
  162. featureShader(anim)
  163. end
  164. end
  165. if actor.GetAnimationMount then
  166. anim = actor:GetAnimationMount()
  167. if anim then
  168. featureShader(anim)
  169. end
  170. end
  171. if actor.GetAnimationHair then
  172. anim = actor:GetAnimationHair()
  173. if anim then
  174. featureShader(anim)
  175. end
  176. end
  177. if actor.GetAnimationWeapon then
  178. anim = actor:GetAnimationWeapon()
  179. if anim then
  180. featureShader(anim)
  181. end
  182. end
  183. if actor.GetAnimationWeaponEff then
  184. anim = actor:GetAnimationWeaponEff()
  185. if anim then
  186. featureShader(anim)
  187. end
  188. end
  189. if actor.GetAnimationLeftWeapon then
  190. anim = actor:GetAnimationLeftWeapon()
  191. if anim then
  192. featureShader(anim)
  193. end
  194. end
  195. if actor.GetAnimationLeftWeaponEff then
  196. anim = actor:GetAnimationLeftWeaponEff()
  197. if anim then
  198. featureShader(anim)
  199. end
  200. end
  201. if actor.GetAnimationMonster then
  202. anim = actor:GetAnimationMonster()
  203. if anim then
  204. featureShader(anim)
  205. end
  206. end
  207. end
  208. function BuffEntityAvatar:UpdatePresent()
  209. self:OnRefreshFeature( self._cfgColor, self._cfgSacle )
  210. end
  211. return BuffEntityAvatar