netMonsterController.lua 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. local netActorController = require("logic/netActorController")
  2. local netMonsterController = class("netMonsterController",netActorController)
  3. netMonsterController.NAME = "netMonsterController"
  4. function netMonsterController:ctor()
  5. netMonsterController.super.ctor(self)
  6. self:RegisterMsgHandler()
  7. end
  8. function netMonsterController:destory()
  9. netMonsterController.super.destory(self)
  10. if netMonsterController.instance then
  11. netMonsterController.instance = nil
  12. end
  13. end
  14. function netMonsterController:Inst()
  15. if not netMonsterController.instance then
  16. netMonsterController.instance = netMonsterController.new()
  17. end
  18. return netMonsterController.instance
  19. end
  20. function netMonsterController:RegisterMsgHandler()
  21. LuaRegisterMsgHandler(global.MsgType.MSG_SC_MONSTER_OWNER, handler( self, self.handle_MSG_SC_MONSTER_OWNER))
  22. end
  23. function netMonsterController:handle_MSG_SC_MONSTER_OWNER(msg)
  24. local msgLen = msg:GetDataLength()
  25. if msgLen == 0 then
  26. return -1
  27. end
  28. local dataString = msg:GetData():ReadString(msgLen)
  29. local slices = string.split(dataString, ",")
  30. local actorID = slices[1]
  31. local ownerID = slices[2] or ""
  32. local ownerName = slices[3] or ""
  33. local actor = global.actorManager:GetActor( actorID )
  34. if not actor or not actor:IsMonster() then
  35. return -1
  36. end
  37. actor:SetOwnerID( ownerID )
  38. actor:SetOwnerName( ownerName )
  39. global.Facade:sendNotification(global.NoticeTable.ActorOwnerChange, {actorID = actorID, actor = actor})
  40. SLBridge:onLUAEvent(LUA_EVENT_ACTOR_OWNER_CHANGE, {actorID = actorID})
  41. return 1
  42. end
  43. function netMonsterController:AddNetMonsterToWorld( param )
  44. local id = param.actorID
  45. local netMonster = global.actorManager:CreateActor( id, global.MMO.ACTOR_MONSTER, param )
  46. if nil == netMonster then
  47. return nil
  48. end
  49. -- bind handler
  50. netMonster:SetGameActorActionHandler( self )
  51. -- HUD for actor
  52. local clothID = param.clothID or 0
  53. netMonster:SetDirection( param.dir or global.MMO.ORIENT_U )
  54. netMonster:SetTypeIdex( clothID )
  55. netMonster:SetAction( param.action or global.MMO.ACTION_IDLE )
  56. netMonster:SetClothID( clothID )
  57. --
  58. local refreshData = {}
  59. refreshData.actor = netMonster
  60. refreshData.actorID = param.actorID
  61. global.Facade:sendNotification(global.NoticeTable.DelayCreateHUDBar, refreshData)
  62. -- feature
  63. global.Facade:sendNotification( global.NoticeTable.DelayDirtyFeature, {actorID = id, actor = netMonster})
  64. return netMonster
  65. end
  66. function netMonsterController:Cleanup( cleanHandler )
  67. netMonsterController.super.Cleanup(self, cleanHandler)
  68. end
  69. function netMonsterController:handleActionBegin( actor, act )
  70. netMonsterController.super.handleActionBegin(self, actor, act)
  71. -- monster audio
  72. local clothID = actor:GetAnimationID()
  73. if act == global.MMO.ACTION_DIE then
  74. -- global.Facade:sendNotification( global.NoticeTable.Audio_Play, {type=global.MMO.SND_TYPE_MON_DIE, index=clothID} )
  75. elseif act == global.MMO.ACTION_ATTACK then
  76. global.Facade:sendNotification( global.NoticeTable.Audio_Play, {type=global.MMO.SND_TYPE_MON_ATTACK, index=clothID} )
  77. elseif act == global.MMO.ACTION_WALK or act == global.MMO.ACTION_RUN then
  78. global.Facade:sendNotification( global.NoticeTable.Audio_Play, {type=global.MMO.SND_TYPE_MON_MOVE, index=clothID} )
  79. elseif act == global.MMO.ACTION_BORN then
  80. global.Facade:sendNotification(global.NoticeTable.Audio_Play, {type=global.MMO.SND_TYPE_MON_BORN, index=clothID})
  81. end
  82. end
  83. function netMonsterController:handleActionCompleted(actor, act)
  84. netMonsterController.super.handleActionCompleted(self, actor, act)
  85. -- 大刀坐标恢复
  86. if actor:GetRaceServer() == global.MMO.ACTOR_SERVER_RACE_KNIEF then
  87. local wPos = global.sceneManager:MapPos2WorldPos( actor:GetMapX(), actor:GetMapY(), true )
  88. local dir = GetActorAttrByID(actor:GetID(), global.MMO.ACTOR_ATTR_DIR)
  89. actor:setPosition( wPos.x, wPos.y )
  90. actor:SetDirection(dir)
  91. end
  92. -- 出生完成,广播
  93. if act == global.MMO.ACTION_BORN then
  94. global.Facade:sendNotification(global.NoticeTable.ActorMonsterBirth, {actorID = actor:GetID(), actor = actor})
  95. elseif act == global.MMO.ACTION_CAVE then
  96. if not actor:IsSleepInCave() then
  97. actor:SetSleepInCave(true)
  98. actor:SetKeyValue(global.MMO.Is_Cave, true)
  99. global.Facade:sendNotification(global.NoticeTable.ActorMonsterCaved, {actorID = actor:GetID(), actor = actor})
  100. end
  101. end
  102. end
  103. function netMonsterController:handle_Revive(actor, jsonData)
  104. netMonsterController.super.handle_Revive(self, actor, jsonData)
  105. if actor:IsWall() and jsonData.Dir then
  106. actor:stopAnimToFrame(jsonData.Dir+1)
  107. actor:updateAnimation()
  108. end
  109. end
  110. function netMonsterController:handle_MSG_SC_PLAYER_SKILL_MAGIC_SUCCESS(actor, msgHdr, jsonData)
  111. -- 死亡状态无法切换
  112. if actor:IsDie() or actor:IsDeath() then
  113. return nil
  114. end
  115. if global.MMO.ACTION_IDLE == actor:GetAction() then
  116. local SkillProxy = global.Facade:retrieveProxy(global.ProxyTable.Skill)
  117. if jsonData.Hit == 1 then
  118. SkillProxy:actorEnterAction(jsonData, true)
  119. SkillProxy:requestSkillPresent(jsonData, 1)
  120. end
  121. SkillProxy:requestSkillPresent(jsonData, 2)
  122. elseif (global.MMO.ACTION_ATTACK == actor:GetAction() or global.MMO.ACTION_SKILL == actor:GetAction() or CheckInSkillAction(actor:GetAction())) and actor.skillStage == 1 and actor.skillID == jsonData.MagicID then
  123. actor.skillStage = 2
  124. local SkillProxy = global.Facade:retrieveProxy(global.ProxyTable.Skill)
  125. SkillProxy:requestSkillPresent(jsonData, 2)
  126. else
  127. -- Another action is running
  128. if self.mNextActionCache[actor:GetID()] == nil then
  129. self.mNextActionCache[actor:GetID()] = {}
  130. end
  131. if jsonData.showSkillMaigc then -- 纯技能表现 不执行技能动作
  132. local SkillProxy = global.Facade:retrieveProxy(global.ProxyTable.Skill)
  133. SkillProxy:requestSkillPresent(jsonData, 3)
  134. else
  135. local isAddAction = true
  136. for i, v in ipairs(self.mNextActionCache[actor:GetID()]) do
  137. if v.act == 5 and v.skillStage == 1 then
  138. isAddAction = false
  139. break
  140. end
  141. end
  142. if isAddAction then
  143. local netAct = {}
  144. netAct.act = 5
  145. netAct.jsonData = jsonData
  146. netAct.msgHdr = msgHdr
  147. netAct.isMagic = true
  148. netAct.skillStage = 2
  149. table.insert(self.mNextActionCache[actor:GetID()], netAct)
  150. end
  151. end
  152. end
  153. end
  154. function netMonsterController:handle_Revive(actor, jsonData)
  155. netMonsterController.super.handle_Revive(self, actor, jsonData)
  156. if actor:IsGate() then
  157. local dir = jsonData.Dir or 0
  158. actor:setGateOpenState(dir >= 3)
  159. actor:SetKeyValue(global.MMO.Is_Gate, true)
  160. elseif actor:IsWall() then
  161. local dir = jsonData.Dir or 0
  162. actor:SetKeyValue(global.MMO.Is_Wall, true)
  163. actor:stopAnimToFrame(dir + 1)
  164. actor:SetAction(global.MMO.ACTION_IDLE)
  165. actor:updateAnimation()
  166. end
  167. end
  168. function netMonsterController:handle_MSG_SC_NET_MONSTER_BORN(actor, msgHdr, msgData)
  169. self.mNextActionCache[actor:GetID()] = nil
  170. local dir = msgHdr.recog
  171. local mapX = msgHdr.param1
  172. local mapY = msgHdr.param2
  173. if actor:IsGate() then
  174. actor:setGateOpenState(dir >= 3)
  175. actor:SetKeyValue(global.MMO.Is_Gate, true)
  176. elseif actor:IsWall() then
  177. actor:SetKeyValue(global.MMO.Is_Wall, true)
  178. actor:stopAnimToFrame(dir + 1)
  179. actor:SetAction(global.MMO.ACTION_IDLE)
  180. actor:updateAnimation()
  181. else
  182. global.actorManager:SetActorMapXY(actor, mapX, mapY)
  183. local worldPos = global.sceneManager:MapPos2WorldPos(mapX, mapY, true)
  184. actor:setPosition(worldPos.x, worldPos.y)
  185. actor:SetDirection(dir)
  186. actor:stopAnimToFrame(nil)
  187. actor:SetStoneMode(false)
  188. actor:SetSleepInCave(false)
  189. actor:SetAction(global.MMO.ACTION_BORN)
  190. actor:SetKeyValue(global.MMO.Is_Cave, false)
  191. global.Facade:sendNotification(global.NoticeTable.ActorMonsterBorn, {actorID = actor:GetID(), actor = actor})
  192. end
  193. end
  194. function netMonsterController:handle_MSG_SC_NET_MONSTER_CAVE(actor, msgHdr, msgData)
  195. self.mNextActionCache[actor:GetID()] = nil
  196. local dir = msgHdr.recog
  197. local mapX = msgHdr.param1
  198. local mapY = msgHdr.param2
  199. global.actorManager:SetActorMapXY(actor, mapX, mapY)
  200. local worldPos = global.sceneManager:MapPos2WorldPos(mapX, mapY, true)
  201. actor:setPosition(worldPos.x, worldPos.y)
  202. actor:SetDirection(dir)
  203. if actor:IsGate() then
  204. actor:setGateOpenState(dir >= 3)
  205. actor:SetAction(global.MMO.ACTION_BORN)
  206. elseif actor:IsFollowDog() then
  207. actor:SetAction(global.MMO.ACTION_CHANGESHAPE)
  208. else
  209. actor:SetAction(global.MMO.ACTION_CAVE)
  210. global.Facade:sendNotification(global.NoticeTable.ActorMonsterCave, {actorID = actor:GetID(), actor = actor})
  211. end
  212. end
  213. return netMonsterController