LuaGuildLobbyMgr.lua 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. local LuaGuildLobbyMgr = class("LuaGuildLobbyMgr")
  2. local EntityMgr = require("EntityMgr")
  3. local MultiTypeAssetLoadSystem = require("MultiTypeAssetLoadSystem")
  4. local State = {
  5. None = 1,
  6. EnterStart = 2,
  7. EnterLoading = 3,
  8. EnterEnd = 4,
  9. Exit = 5,
  10. }
  11. function LuaGuildLobbyMgr:ctor()
  12. self.curState = State.None
  13. self.entityMgr = EntityMgr:new()
  14. self:RegisterNetEvents()
  15. end
  16. function LuaGuildLobbyMgr:Destroy()
  17. self:UnRegisterNetEvents()
  18. self.curState = nil
  19. self.entityMgr:Dispose()
  20. self.entityMgr = nil
  21. end
  22. function LuaGuildLobbyMgr:RegisterNetEvents()
  23. -- ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_PLAYER_ENTER_MAP_ACK, self.OnEnterMapAck, self)
  24. -- ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_PLAYER_ENTER_NTF, self.OnPlayerEnterMapNtf, self)
  25. -- ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_SYNC_PLAYERS_NTF, self.OnSyncPlayersPosNtf, self)
  26. -- ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_PLAYER_LEAVE_NTF, self.OnPlayerLeaveMapNft, self)
  27. -- ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_GM_SYNC_ALL_PLAYER_NTF, self.OnSyncAllPlayersNtf, self)
  28. end
  29. function LuaGuildLobbyMgr:UnRegisterNetEvents()
  30. -- ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_PLAYER_ENTER_MAP_ACK)
  31. -- ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_PLAYER_ENTER_NTF)
  32. -- ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_SYNC_PLAYERS_NTF)
  33. -- ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_PLAYER_LEAVE_NTF)
  34. -- ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_GM_SYNC_ALL_PLAYER_NTF)
  35. end
  36. function LuaGuildLobbyMgr:SendEnterReq()
  37. -- ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_PLAYER_ENTER_MAP_REQ,{})
  38. end
  39. function LuaGuildLobbyMgr:SendLeaveReq()
  40. local entityType = Enum.EntityType.MinePlayer
  41. local uid = ManagerContainer.DataMgr.UserData:GetUserId()
  42. local pos = self.entityMgr:GetPosition(entityType, uid)
  43. if pos then
  44. local endPos = {x = pos.x, y = pos.z, z = pos.y}
  45. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_PLAYER_LEAVE_MAP_REQ, { pos = endPos })
  46. else
  47. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_PLAYER_LEAVE_MAP_REQ,{})
  48. end
  49. end
  50. function LuaGuildLobbyMgr:SendMoveCmdReq(movePos)
  51. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_PLAYER_MOVE_REQ, {pos = {x = movePos.x,y = movePos.z,z = movePos.y}})
  52. end
  53. function LuaGuildLobbyMgr:OnEnterMapAck(data)
  54. LogError("[Wboy] SC_PLAYER_ENTER_MAP_ACK " .. Inspect(data))
  55. if ManagerContainer.NetManager:IsErrorData(data) then return end
  56. local entityType = Enum.EntityType.MinePlayer
  57. local uid = ManagerContainer.DataMgr.UserData:GetUserId()
  58. local pos = data.pos and Vector3.New(data.pos.x, data.pos.z, data.pos.y) or Vector3.zero
  59. if not self.curState or self.curState == State.None then
  60. self.curState = State.EnterStart
  61. self.entityMgr:EnterMap(entityType, uid, pos)
  62. ManagerContainer.LuaGameMgr:EnterGuildLobby()
  63. elseif self.curState == State.Exit then
  64. LogError("[Wboy] not enter, because exiting")
  65. else
  66. self.entityMgr:EnterMap(entityType, uid, pos)
  67. end
  68. end
  69. function LuaGuildLobbyMgr:OnPlayerEnterMapNtf(data)
  70. LogError("[Wboy] SC_PLAYER_ENTER_NTF " .. Inspect(data))
  71. if not self:IsInGuildLobby() then
  72. return
  73. end
  74. local players = data.players
  75. for i = 1, #players do
  76. local player = players[i]
  77. local uid = player.uid
  78. local pos = player.pos
  79. local entityType = player.u_type
  80. pos = pos and Vector3.New(pos.x, pos.z, pos.y) or Vector3.zero
  81. local entity = self.entityMgr:EnterMap(entityType, uid, pos)
  82. if self.curState == State.EnterEnd then
  83. self.entityMgr:EnterWorld(entity)
  84. end
  85. end
  86. end
  87. function LuaGuildLobbyMgr:OnSyncPlayersPosNtf(data)
  88. LogError("[Wboy] SC_SYNC_PLAYERS_NTF " .. Inspect(data))
  89. if not self:IsInGuildLobby() then
  90. return
  91. end
  92. local players = data.players
  93. for i = 1, #players do
  94. local player = players[i]
  95. local uid = player.uid
  96. local pos = player.pos
  97. local entityType = player.u_type
  98. if pos then
  99. self.entityMgr:SynPosition(entityType, uid, Vector3.New(pos.x, pos.z, pos.y))
  100. end
  101. end
  102. end
  103. function LuaGuildLobbyMgr:OnPlayerLeaveMapNft(data)
  104. LogError("[Wboy] SC_PLAYER_LEAVE_NTF " .. Inspect(data))
  105. if not self:IsInGuildLobby() then
  106. return
  107. end
  108. local players = data.players
  109. for i = 1, #players do
  110. local player = players[i]
  111. local uid = player.uid
  112. local entityType = player.u_type
  113. self.entityMgr:ExitMap(entityType, uid)
  114. end
  115. end
  116. function LuaGuildLobbyMgr:OnSyncAllPlayersNtf()
  117. if not self:IsInGuildLobby() then
  118. return
  119. end
  120. end
  121. function LuaGuildLobbyMgr:IsInGuildLobby()
  122. return self.curState and self.curState ~= State.None and self.curState ~= State.Exit
  123. end
  124. function LuaGuildLobbyMgr:Enter()
  125. self:RegisterEvents()
  126. self.curState = State.EnterLoading
  127. self:_OnLoadProgress(0.2)
  128. -- 创建玩家GameObject容器
  129. self.playerContainer = UnityEngine.GameObject("PlayerContainer").transform
  130. -- 进入前需要预加载的资源
  131. self:_PreloadAssets()
  132. end
  133. function LuaGuildLobbyMgr:Update()
  134. local deltaTime = Time.deltaTime
  135. self.entityMgr:Update(deltaTime)
  136. end
  137. function LuaGuildLobbyMgr:Exit()
  138. self.curState = State.Exit
  139. self:SendLeaveReq()
  140. if self.loadSystem then
  141. self.loadSystem:Dispose()
  142. self.loadSystem = nil
  143. end
  144. self.entityMgr:RemoveAllEntity()
  145. self.playerContainer = nil
  146. self.mainCamera = nil
  147. self.cullingMask = nil
  148. self.curState = State.None
  149. self:UnRegisterEvents()
  150. ManagerContainer.DataMgr.GuildLobbyData:ClearPlayerData()
  151. end
  152. function LuaGuildLobbyMgr:_PreloadAssets()
  153. local loadSystem = self.loadSystem
  154. if loadSystem then
  155. loadSystem:Cancel()
  156. loadSystem:RemoveLoadAllAsset()
  157. else
  158. loadSystem = MultiTypeAssetLoadSystem:new()
  159. loadSystem:SetCompleteCB(self, self._PreloadAssetsComplete, self._LoadProgressChanged)
  160. self.loadSystem = loadSystem
  161. end
  162. loadSystem:AddLoadAsset(Enum.ResourceType.GameObject, 'Assets/Content/Prefabs/Camera', 'DefaultCVC')
  163. -- 预加载主角
  164. local entityDic = self.entityMgr:GetEntitys(Enum.EntityType.MinePlayer)
  165. for uid, entity in pairs(entityDic) do
  166. entity:StatisticsPreloadAssets(loadSystem)
  167. end
  168. loadSystem:Begin()
  169. end
  170. function LuaGuildLobbyMgr:_LoadProgressChanged(progress)
  171. self:_OnLoadProgress(0.2 + 0.8 * progress)
  172. end
  173. function LuaGuildLobbyMgr:_PreloadAssetsComplete()
  174. local modelGo = ManagerContainer.ResMgr:GetGoFromPool("Assets/Content/Prefabs/Camera", "DefaultCVC")
  175. local defaultCVC = modelGo:GetComponent(typeof(Cinemachine.CinemachineVirtualCamera))
  176. self.mainCamera = LuaBattleBridge.InitMainCamera()
  177. self.cullingMask = self.mainCamera.cullingMask
  178. -- 所有的实体对象进入游戏世界
  179. self.entityMgr:AllEntityEnterWorld()
  180. self.curState = State.EnterEnd
  181. self:_OnLoadComplete()
  182. -- 设置摄像机跟随
  183. local entityType = Enum.EntityType.MinePlayer
  184. local uid = ManagerContainer.DataMgr.UserData:GetUserId()
  185. defaultCVC.Follow = self.entityMgr:GetTransform(entityType, uid)
  186. -- 打开UI界面
  187. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIGuildLobby)
  188. end
  189. function LuaGuildLobbyMgr:_OnLoadProgress(progress)
  190. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.EID_Loading_Progress, progress);
  191. end
  192. function LuaGuildLobbyMgr:_OnLoadComplete()
  193. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.EID_Loading_Complete);
  194. end
  195. function LuaGuildLobbyMgr:GetPlayerContainer()
  196. return self.playerContainer
  197. end
  198. function LuaGuildLobbyMgr:GetMainCamera()
  199. return self.mainCamera
  200. end
  201. function LuaGuildLobbyMgr:GetEntityMgr()
  202. return self.entityMgr
  203. end
  204. function LuaGuildLobbyMgr:OnJoystickDrag(dir, state)
  205. local entityType = Enum.EntityType.MinePlayer
  206. local uid = ManagerContainer.DataMgr.UserData:GetUserId()
  207. local entity = self.entityMgr:GetEntity(entityType, uid)
  208. if entity then
  209. if entity.OnJoystickDrag then
  210. entity:OnJoystickDrag(dir, state)
  211. end
  212. end
  213. end
  214. function LuaGuildLobbyMgr:RegisterEvents()
  215. ManagerContainer.LuaEventMgr:RegisterEvent(UIEventNames.FASHION_WEAR_CHANGE, self, self.ForceUpdateMineEntityView)
  216. ManagerContainer.LuaEventMgr:RegisterEvent(UIEventNames.GUILD_LOBBY_PLAYER_INFO_CHANGED, self, self.ForceUpdateOtherEntityView)
  217. end
  218. function LuaGuildLobbyMgr:UnRegisterEvents()
  219. ManagerContainer.LuaEventMgr:UnregisterEvent(UIEventNames.FASHION_WEAR_CHANGE, self, self.ForceUpdateMineEntityView)
  220. ManagerContainer.LuaEventMgr:RegisterEvent(UIEventNames.GUILD_LOBBY_PLAYER_INFO_CHANGED, self, self.ForceUpdateOtherEntityView)
  221. end
  222. function LuaGuildLobbyMgr:ForceUpdateMineEntityView()
  223. local entityType = Enum.EntityType.MinePlayer
  224. local uid = ManagerContainer.DataMgr.UserData:GetUserId()
  225. self.entityMgr:ForceRefreshGenerateView(entityType, uid)
  226. end
  227. function LuaGuildLobbyMgr:ForceUpdateOtherEntityView(uid)
  228. local entityType = Enum.EntityType.OtherPlayer
  229. self.entityMgr:ForceRefreshGenerateView(entityType, uid)
  230. end
  231. return LuaGuildLobbyMgr