gameActorNpc.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. local gameActorMoveable = require( "actor/gameActorMoveable")
  2. local gameActorNpc = class('gameActorNpc', gameActorMoveable)
  3. local mmo = global.MMO
  4. local mapActAnim = {
  5. [mmo.ACTION_IDLE] = mmo.ANIM_IDLE,
  6. [mmo.ACTION_BORN] = mmo.ANIM_BORN,
  7. }
  8. local fixAnimDirection = {
  9. [59] = 0,
  10. }
  11. function gameActorNpc:ctor()
  12. gameActorNpc.super.ctor(self)
  13. end
  14. function gameActorNpc:init()
  15. gameActorNpc.super.init(self)
  16. self.mClothID = nil
  17. self.mFeatureDirty = false
  18. self.mUseMonsterAnim = nil
  19. self.mAnimAct = global.MMO.ANIM_IDLE
  20. self.mIsAnimNeedToUpdate = false
  21. self.mStopAnimFrameIndex = nil
  22. self.mBubbleName = nil
  23. end
  24. function gameActorNpc:destory()
  25. gameActorNpc.super.destory(self)
  26. -- remove hud
  27. global.HUDManager:RemoveActorAllHUD( self.mID )
  28. end
  29. function gameActorNpc:Tick(dt)
  30. if self.mFeatureDirty then
  31. self.mFeatureDirty = false
  32. self:checkFeature()
  33. end
  34. if self.mIsAnimNeedToUpdate then
  35. self:updateAnimation()
  36. self.mIsAnimNeedToUpdate = false
  37. end
  38. end
  39. function gameActorNpc:setPosition(x, y)
  40. gameActorNpc.super.setPosition(self, x, y)
  41. -- refresh zOrder
  42. self:GetNode():setLocalZOrder( math.floor(-y) )
  43. -- update hud pos
  44. local hudTop = self:GetHUDTop()
  45. global.HUDManager:setPosition( self.mID, x, y + hudTop )
  46. -- update buff pos
  47. global.BuffManager:setPosition(self:GetID(), x, y)
  48. global.ActorEffectManager:setPosition(self:GetID(), x, y)
  49. end
  50. function gameActorNpc:SetAction(newAction, isTerminate)
  51. local actAnim = mapActAnim[newAction] or global.MMO.ANIM_IDLE
  52. self:SetAnimAct( actAnim )
  53. self:dirtyAnimFlag()
  54. end
  55. function gameActorNpc:SetAnimAct(newAnim)
  56. if self.mAnimAct ~= newAnim then
  57. self.mAnimAct = newAnim
  58. -- in die anim, zOrder is lowest
  59. if newAnim == global.MMO.ANIM_DIE then
  60. self:GetNode():setLocalZOrder( -self:GetNode():getPositionY() + self:GetAdditionZorder() + global.MMO.DieAddZorder )
  61. end
  62. self:dirtyAnimFlag()
  63. end
  64. end
  65. function gameActorNpc:SetDirection( dir )
  66. gameActorNpc.super.SetDirection(self, dir)
  67. end
  68. function gameActorNpc:onSequAnimLoadCompleted(npcID, animID, seqAnim)
  69. -- There is a strange bug that *this* pointer will be invalid in sometime.
  70. local _this = global.actorManager:GetActor( npcID )
  71. if nil == _this then
  72. return
  73. end
  74. -- Refresh npc name label hud top
  75. local pos = _this:getPosition()
  76. local hudTop = _this:GetHUDTop()
  77. global.HUDManager:setPosition( _this.mID, pos.x, pos.y + hudTop )
  78. end
  79. function gameActorNpc:InitParam( param )
  80. self.mCurrActorNode = cc.Node:create()
  81. self.mCurrMountNode = cc.Node:create()
  82. self.mCurrActorNode:addChild(self.mCurrMountNode)
  83. self.mCurrMountNode:setTag(mmo.NPC_MOUNT_TAG)
  84. self.mCurrMountNode:setPosition(mmo.PLAYER_AVATAR_OFFSET)
  85. return 1
  86. end
  87. function gameActorNpc:SetClothID(clothID)
  88. self.mClothID = clothID
  89. self.mUseMonsterAnim = false
  90. if clothID and clothID > global.MMO.NPC_MONSTER_ID_PART then
  91. clothID = clothID % global.MMO.NPC_MONSTER_ID_PART
  92. self.mClothID = clothID
  93. self.mUseMonsterAnim = true
  94. end
  95. end
  96. function gameActorNpc:dirtyFeature()
  97. self.mFeatureDirty = true
  98. end
  99. function gameActorNpc:checkFeature()
  100. self:updateCloth()
  101. end
  102. function gameActorNpc:updateCloth()
  103. local function loadCompleted(animID, seqAnim)
  104. self:onSequAnimLoadCompleted(self:GetID(), animID, seqAnim)
  105. end
  106. local anim = nil
  107. if not self.mUseMonsterAnim then
  108. anim = global.FrameAnimManager:CreateActorNpcAnim( self.mClothID, loadCompleted )
  109. else
  110. anim = global.FrameAnimManager:CreateActorMonsterAnim( self.mClothID, self.mAnimAct, loadCompleted )
  111. end
  112. if self.mAnimation then
  113. self.mAnimation:removeFromParent()
  114. self.mAnimation = nil
  115. end
  116. self.mAnimation = anim
  117. self.mAnimation:setPosition( global.MMO.PLAYER_AVATAR_OFFSET )
  118. self.mCurrActorNode:addChild( anim )
  119. -- Refresh npc name label hud top
  120. local pos = self:getPosition()
  121. global.HUDManager:setPosition( self:GetID(), pos.x, pos.y + self:GetHUDTop() )
  122. self:dirtyAnimFlag()
  123. end
  124. function gameActorNpc:updateAnimation()
  125. local animDir = self:getFixAnimDir() or self.mOrient
  126. if self.mAnimation then
  127. self.mAnimation:Play( self.mAnimAct, animDir, true )
  128. end
  129. if self.mStopAnimFrameIndex then
  130. self:StopAllAnimation( self.mStopAnimFrameIndex )
  131. end
  132. end
  133. function gameActorNpc:getFixAnimDir()
  134. if fixAnimDirection[self.mClothID] then
  135. if type(fixAnimDirection[self.mClothID]) == "table" then
  136. return fixAnimDirection[self.mClothID][self.mAnimAct]
  137. else
  138. return fixAnimDirection[self.mClothID]
  139. end
  140. end
  141. return nil
  142. end
  143. function gameActorNpc:dirtyAnimFlag()
  144. if self.mStopAnimFrameIndex then
  145. return
  146. end
  147. self.mIsAnimNeedToUpdate = true
  148. end
  149. function gameActorNpc:stopAnimToFrame( frameIndex )
  150. self.mStopAnimFrameIndex = frameIndex
  151. end
  152. function gameActorNpc:StopAllAnimation( frameIndex )
  153. local animDir = self:getFixAnimDir() or self.mOrient
  154. if self.mAnimation then
  155. self.mAnimation:Stop( frameIndex, self.mAnimAct, animDir )
  156. end
  157. end
  158. function gameActorNpc:GetType()
  159. return global.MMO.ACTOR_NPC
  160. end
  161. function gameActorNpc:GetAnimationID()
  162. return self.mClothID
  163. end
  164. function gameActorNpc:GetHUDTop()
  165. if not self.mAnimation then
  166. return 20
  167. end
  168. return self.mAnimation:GetHUDTop() or 20
  169. end
  170. function gameActorNpc:GetBubbleName()
  171. return self.mBubbleName
  172. end
  173. function gameActorNpc:SetBubbleName( value )
  174. self.mBubbleName = value
  175. end
  176. return gameActorNpc