gameStateWorld.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. local gameStateWorld = class('gameStateWorld')
  2. function gameStateWorld:ctor()
  3. end
  4. function gameStateWorld:getStateType()
  5. return global.MMO.GAME_STATE_WORLD
  6. end
  7. --- enter
  8. function gameStateWorld:onEnter()
  9. self:RegisterWorld()
  10. self:SyncCppState()
  11. self:InitWorld()
  12. self:EnterWorld()
  13. return 1
  14. end
  15. function gameStateWorld:RegisterWorld()
  16. local facade = global.Facade
  17. -- command
  18. facade:removeCommand( global.NoticeTable.RegisterWorldController )
  19. facade:removeCommand( global.NoticeTable.RegisterWorldMediator )
  20. facade:removeCommand( global.NoticeTable.RegisterWorldProxy )
  21. -- loading mediator
  22. local LoadingMediator = requireMediator( "loading_layer/LoadingMediator" )
  23. facade:removeMediator(LoadingMediator.NAME)
  24. end
  25. function gameStateWorld:SyncCppState()
  26. local facade = global.Facade
  27. local cppEnv = global.gameEnvironment
  28. local AuthProxy = facade:retrieveProxy(global.ProxyTable.AuthProxy)
  29. local LoginProxy = facade:retrieveProxy(global.ProxyTable.Login)
  30. cppEnv:SetLoginAccount( AuthProxy:GetUID() )
  31. cppEnv:SetLoginServer( LoginProxy:GetSelectedServerId() )
  32. end
  33. function gameStateWorld:InitWorld()
  34. local facade = global.Facade
  35. -- laoding close
  36. facade:sendNotification(global.NoticeTable.Layer_LoadingBar_Close)
  37. -- init resolution size & policy
  38. local normalPolicy = global.DesignPolicy
  39. local designSize = global.DesignSize_Oth
  40. local glview = global.Director:getOpenGLView()
  41. if global.isWinPlayMode and glview then
  42. -- keep camera zoom(FrameSize == ViewPortSize)
  43. designSize = glview:getFrameSize()
  44. end
  45. local resolutionData = {}
  46. resolutionData.rType = normalPolicy
  47. resolutionData.size = designSize
  48. facade:sendNotification(global.NoticeTable.ChangeResolution, resolutionData)
  49. -- init layer facade
  50. local LayerFacadeMediator = requireMediator( "LayerFacadeMediator" )
  51. local layerFacade = facade:retrieveMediator( LayerFacadeMediator.NAME )
  52. layerFacade:InitOnEnterWorld()
  53. global.JsonProtoHelper:init()
  54. global.BehaviorController:InitOnEnterWorld()
  55. global.userInputController:InitOnEnterWorld()
  56. global.actorManager:InitOnEnterWorld()
  57. end
  58. function gameStateWorld:EnterWorld()
  59. local facade = global.Facade
  60. -- notify server
  61. local loginProxy = facade:retrieveProxy( global.ProxyTable.Login )
  62. loginProxy:EnterWorld()
  63. -- register msg handler
  64. local noticeProxy = facade:retrieveProxy( global.ProxyTable.NoticeProxy )
  65. noticeProxy:RegisterMsgHandlerAfterEnterWorld()
  66. -- register msg handler
  67. global.SUIManager:RegisterMsgHandlerAfterEnterWorld()
  68. local ChatProxy = global.Facade:retrieveProxy(global.ProxyTable.Chat)
  69. ChatProxy:RegisterMsgHandlerAfterEnterWorld()
  70. end
  71. function gameStateWorld:onEnd()--重启
  72. self:onExit()
  73. end
  74. --- exit world
  75. function gameStateWorld:onExit()
  76. self:CleanWorld()
  77. global.Facade:sendNotification( global.NoticeTable.ReleaseMemory)--清理完再释放
  78. self:LeaveWorld()
  79. self:Destory()
  80. return 1
  81. end
  82. function gameStateWorld:LeaveWorld()
  83. local facade = global.Facade
  84. global.userInputController:ResetKeyboard()
  85. global.userInputController:ResetTouch()
  86. facade:sendNotification(global.NoticeTable.Layer_Notice_Close, SLBridge:CheckLuaEventCleaned())
  87. facade:sendNotification(global.NoticeTable.Layer_CommonTips_Close)
  88. --
  89. global.LuaBridgeCtl:SetModulesSwitch( global.MMO.Modules_Index_Enable_Check_Heart_Beat, 0 )
  90. -- unregister world
  91. facade:sendNotification( global.NoticeTable.UnRegisterOther )
  92. facade:sendNotification( global.NoticeTable.UnRegisterWorldProxy )
  93. facade:sendNotification( global.NoticeTable.UnRegisterWorldMediator )
  94. facade:sendNotification( global.NoticeTable.UnRegisterWorldController )
  95. --清理收货和试玩状态
  96. global.IsReceiveRole = false
  97. global.IsVisitor = false
  98. -- clear cocos2d event
  99. -- clear cocos2d scheduler, but system scheduler( ActionManager )
  100. -- clear cocos2d action
  101. -- clear cocos2d audio
  102. local director = cc.Director:getInstance()
  103. local currScene = global.Director:getRunningScene()
  104. director:getEventDispatcher():removeEventListenersForType(1)
  105. director:getEventDispatcher():removeEventListenersForType(2)
  106. director:getEventDispatcher():removeEventListenersForType(3)
  107. director:getEventDispatcher():removeEventListenersForType(4)
  108. director:getEventDispatcher():removeEventListenersForType(5)
  109. director:getEventDispatcher():removeEventListenersForTarget(currScene, true)
  110. director:getScheduler():unscheduleAllWithMinPriority( cc.PRIORITY_NON_SYSTEM_MIN )
  111. director:getActionManager():removeAllActions()
  112. cc.AsyncTaskPool:getInstance():stopTasks(cc.AsyncTaskPool.TaskType.TASK_IO)
  113. cc.SimpleAudioEngine:destroyInstance()
  114. ccexp.AudioEngine:uncacheAll()
  115. global.HttpDownloader:reset()
  116. local AuthProxy = global.Facade:retrieveProxy(global.ProxyTable.AuthProxy)
  117. AuthProxy:resetDownloader()
  118. -- unregister script handle
  119. if currScene then
  120. local function disableEvents(node)
  121. if tolua.type(node) == "cc.TableView" then
  122. else
  123. node:disableNodeEvents()
  124. end
  125. local children = node:getChildren()
  126. for k, v in pairs(children) do
  127. disableEvents(v)
  128. end
  129. end
  130. disableEvents(currScene)
  131. end
  132. -- replace scene
  133. local scene = cc.Scene:create()
  134. local currScene = global.Director:getRunningScene()
  135. if not currScene then
  136. global.Director:runWithScene( scene )
  137. else
  138. global.Director:replaceScene( scene )
  139. end
  140. local LayerFacadeMediator = requireMediator( "LayerFacadeMediator" )
  141. local lFMediator = facade:retrieveMediator(LayerFacadeMediator.NAME)
  142. lFMediator:InitOnEnterActive(scene)
  143. director:getTextureCache():removeUnusedTextures()
  144. cc.SpriteFrameCache:getInstance():removeUnusedSpriteFrames()
  145. -- clear net msg
  146. local netClient = global.gameEnvironment:GetNetClient()
  147. netClient:CleanAllMessageHandler()
  148. LuaNetworkController:Inst():UnregisterAllLuaHandler()
  149. end
  150. function gameStateWorld:CleanWorld()
  151. -- logic
  152. global.BuffManager:Cleanup()
  153. global.ActorEffectManager:Cleanup()
  154. global.gameMapController:Cleanup()
  155. global.netNpcController:Cleanup()
  156. global.netMonsterController:Cleanup()
  157. global.netPlayerController:Cleanup()
  158. global.gamePlayerController:Cleanup()
  159. global.dropItemController:Cleanup()
  160. global.actorRefreshController:Cleanup()
  161. global.actorInViewController:Cleanup()
  162. -- actor
  163. global.actorManager:Cleanup()
  164. global.HUDManager:Cleanup()
  165. -- scene
  166. global.dropItemManager:Cleanup()
  167. global.monsterManager:Cleanup()
  168. global.npcManager:Cleanup()
  169. global.playerManager:Cleanup()
  170. global.sceneGraphCtl:Cleanup()
  171. global.sceneManager:Cleanup()
  172. global.sceneEffectManager:Cleanup()
  173. -- skill
  174. global.skillManager:Cleanup()
  175. global.GameActorSkillController:Cleanup()
  176. -- anim cleanup
  177. global.FrameAnimManager:unloadAllUnusedAnimData()
  178. -- clean
  179. global.ResDownloader:CleanupDownloadCount()
  180. -- spine batch
  181. if sp and sp.SkeletonBatch then
  182. sp.SkeletonBatch:destory()
  183. end
  184. end
  185. function gameStateWorld:Destory()
  186. if global.sceneManager then
  187. global.sceneManager:destory()
  188. global.sceneManager = nil
  189. end
  190. if global.mouseEventController then
  191. global.mouseEventController:destory()
  192. global.mouseEventController = nil
  193. end
  194. if global.gamePlayerController then
  195. global.gamePlayerController:destory()
  196. global.gamePlayerController = nil
  197. end
  198. if global.netPlayerController then
  199. global.netPlayerController:destory()
  200. global.netPlayerController = nil
  201. end
  202. if global.netMonsterController then
  203. global.netMonsterController:destory()
  204. global.netMonsterController = nil
  205. end
  206. if global.netNpcController then
  207. global.netNpcController:destory()
  208. global.netNpcController = nil
  209. end
  210. if global.dropItemController then
  211. global.dropItemController:destory()
  212. global.dropItemController = nil
  213. end
  214. if global.gameMapController then
  215. global.gameMapController:destory()
  216. global.gameMapController = nil
  217. end
  218. if global.actorRefreshController then
  219. global.actorRefreshController:destory()
  220. global.actorRefreshController = nil
  221. end
  222. if global.actorInViewController then
  223. global.actorInViewController:destory()
  224. global.actorInViewController = nil
  225. end
  226. if global.GameActorSkillController then
  227. global.GameActorSkillController:destory()
  228. global.GameActorSkillController = nil
  229. end
  230. if global.actorManager then
  231. global.actorManager:destory()
  232. global.actorManager = nil
  233. end
  234. if global.monsterManager then
  235. global.monsterManager:destory()
  236. global.monsterManager = nil
  237. end
  238. if global.playerManager then
  239. global.playerManager:destory()
  240. global.playerManager = nil
  241. end
  242. if global.npcManager then
  243. global.npcManager:destory()
  244. global.npcManager = nil
  245. end
  246. if global.dropItemManager then
  247. global.dropItemManager:destory()
  248. global.dropItemManager = nil
  249. end
  250. if global.sceneEffectManager then
  251. global.sceneEffectManager:destory()
  252. global.sceneEffectManager = nil
  253. end
  254. if global.skillManager then
  255. global.skillManager:destory()
  256. global.skillManager = nil
  257. end
  258. if global.HUDManager then
  259. global.HUDManager:destory()
  260. global.HUDManager = nil
  261. end
  262. if global.HUDHPManager then
  263. global.HUDHPManager:destory()
  264. global.HUDHPManager = nil
  265. end
  266. if global.BuffManager then
  267. global.BuffManager:destory()
  268. global.BuffManager = nil
  269. end
  270. if global.DObstacleController then
  271. global.DObstacleController:destory()
  272. global.DObstacleController = nil
  273. end
  274. if global.sceneGraphCtl then
  275. global.sceneGraphCtl:destory()
  276. global.sceneGraphCtl = nil
  277. end
  278. if global.gameWorldController then
  279. global.gameWorldController:destory()
  280. global.gameWorldController = nil
  281. end
  282. if global.PathFindController then
  283. global.PathFindController:destory()
  284. global.PathFindController = nil
  285. end
  286. if global.BehaviorController then
  287. global.BehaviorController:destory()
  288. global.BehaviorController = nil
  289. end
  290. if global.ActorEffectManager then
  291. global.ActorEffectManager:destory()
  292. global.ActorEffectManager = nil
  293. end
  294. global.WidgetCacheManager = nil
  295. end
  296. return gameStateWorld