HNScenceManager.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import GameConstant from 'GameConstant'
  2. let UIHelper = require("UIHelper");
  3. let Global = require("Global");
  4. let BaseDefine = require("BaseDefine");
  5. let BYSceneManager = require("BYSceneManager");
  6. let GameUpdate = require("GameUpdate");
  7. let BundleLoaderManager = require("BundleLoaderManager");
  8. let _ScenceManagerInstance = null;
  9. import GameManagerBase from 'GameManagerBase'
  10. //退出游戏,需要保留的内存资源
  11. let filter_assets = [
  12. //捕鱼
  13. ]
  14. let homeSceneLoadDir = [
  15. ];
  16. let filter_home_assets = [
  17. ];
  18. class HNScenceManager {
  19. constructor() {
  20. this._rootComponent = null;
  21. this._currGameNode = null;
  22. this._currGameComponent = null;
  23. this.recvMemoryRes = [];
  24. }
  25. CreateGame() {
  26. let gameComponent = null;
  27. if (!this._currGameNode) {
  28. return null;
  29. }
  30. let nameComponent = "GameBase";
  31. if (nameComponent.length > 0) {
  32. gameComponent = this._currGameNode.getComponent(nameComponent);
  33. }
  34. this._currGameComponent = gameComponent;
  35. if (gameComponent) {
  36. this.inGameScence();
  37. }
  38. return gameComponent;
  39. }
  40. setRootComponent(rootComponent) {
  41. this._rootComponent = rootComponent;
  42. }
  43. getRootComponent() {
  44. return this._rootComponent;
  45. }
  46. setGameComponent(gameComponent) { //BYSCENE
  47. this._currGameComponent = gameComponent;
  48. }
  49. getGameComponent() {
  50. return this._currGameComponent;
  51. }
  52. inGameScence() {
  53. let rootComponent = this.getRootComponent();
  54. if (rootComponent) {
  55. rootComponent.inGameScence();
  56. }
  57. }
  58. releaseHomeAssets() { //BYSCENE
  59. // if (BaseDefine.GAME_PLATFORM != BaseDefine.WEB_H5_PLATFORM) {
  60. // if (homeSceneLoadDir.length > 0) {
  61. // for (let i = 0; i < homeSceneLoadDir.length; ++i) {
  62. // cc.assetManager.releaseAsset(homeSceneLoadDir[i]);
  63. // }
  64. // }
  65. // Global.print("---releaseHomeAssets");
  66. // //cc.assetManager.releaseAll();
  67. // //cc.sys.garbageCollect();
  68. // }
  69. }
  70. releaseAssets(arrRes) {
  71. if (BaseDefine.GAME_PLATFORM != BaseDefine.WEB_H5_PLATFORM) {
  72. //内存回收(目前只针对捕鱼游戏)
  73. if (arrRes.length > 0) {
  74. for (let i = 0; i < arrRes.length; ++i) {
  75. let deps = cc.assetManager.getDependsRecursively(arrRes[i]);
  76. //排除共享资源
  77. for (let j = 0; j < filter_assets.length; ++j) {
  78. let index = deps.indexOf(filter_assets[j]);
  79. if (index !== -1)
  80. deps.splice(index, 1);
  81. }
  82. cc.assetManager.releaseAsset(deps);
  83. cc.assetManager.releaseAsset(arrRes[i]);
  84. }
  85. }
  86. Global.print("---releaseAssets");
  87. //cc.assetManager.releaseAll();
  88. //cc.sys.garbageCollect();
  89. }
  90. }
  91. getPrefabsByKind(wKindID) {
  92. let arrRes = [];
  93. let bundleData = this.getMainPrefabsByKind(wKindID);
  94. if (bundleData != '') {
  95. arrRes.push(bundleData);
  96. }
  97. return arrRes;
  98. }
  99. getMainPrefabsByKind(wKindID) {
  100. let bundleData = {
  101. [GameConstant.KIND_ID_BRAB]: {
  102. bundle: "BRAB",
  103. importBundle: [],
  104. gamePrefabPath: ["filter-BRAB/BRABGameScence", "GamePrefabs/Base/TP"]
  105. }, [GameConstant.KIND_ID_BRTP]: {
  106. bundle: "BRTP",
  107. importBundle: [],
  108. gamePrefabPath: ["filter-BRTP/BRTPGameSence"]
  109. }
  110. };
  111. return bundleData[wKindID];
  112. }
  113. releaseBYAssets() {
  114. // BYSCENE
  115. // if (cc.sys.os == cc.sys.OS_ANDROID || cc.sys.os == cc.sys.OS_IOS) {
  116. //内存回收(目前只针对捕鱼游戏)
  117. /*for(let i = 0; i < filter_assets.length;i++)
  118. {
  119. cc.loader.setAutoRelease(filter_assets[i],false);
  120. }
  121. if (this.recvMemoryRes.length > 0) {
  122. for (let i = 0 ; i < this.recvMemoryRes.length; ++i) {
  123. let deps = cc.loader.getDependsRecursively(this.recvMemoryRes[i]);
  124. cc.loader.release(deps);
  125. cc.loader.release(this.recvMemoryRes[i]);
  126. }
  127. }*/
  128. //jsb
  129. //Global.print("cc.textureCache.dumpCachedTextureInfo():: " + cc.textureCache.dumpCachedTextureInfo());
  130. //cc.loader.releaseResDir("GameTextures");
  131. //cc.loader.releaseAll();
  132. // cc.sys.garbageCollect();
  133. // //恢复渲染画质
  134. // //cc.Texture2D.setDefaultAlphaPixelFormat(0);
  135. // }
  136. }
  137. recvMemory() {
  138. // 内存回收
  139. // this.recvMemoryRes = [];
  140. // let arrRes = this.getPrefabsByKind(wKindID);
  141. // if (BaseDefine.GAME_PLATFORM != BaseDefine.WEB_H5_PLATFORM) {
  142. // cc.assetManager.releaseAll();
  143. // }
  144. }
  145. loadGameBaseData(wKindID, callBack) {
  146. this.callBack = callBack;
  147. window.testFunction = (bundle, url) => {
  148. if (url.length <= 0) {
  149. BYSceneManager.Instance().loadGameBaseData(GameConstant.KIND_ID_BY, bundle);
  150. } else {
  151. this.loadBundlePrefab(bundle, url)
  152. }
  153. }
  154. this.recvMemory();
  155. BundleLoaderManager.Instance().loadBundleGame(wKindID, callBack, this._currGameNode);
  156. }
  157. loadBundlePrefab(bundle, url) {
  158. for (let i = 0; i < url.length; i += 1) {
  159. url[i] = "resourcesPrefab/" + url[i];
  160. }
  161. UIHelper.showWaitNode("connectServer")
  162. UIHelper.nodeMAP = new Map();
  163. bundle.load(url, cc.Prefab, () => {
  164. // this.loadBundleProgress();
  165. }, (err, prefab) => {
  166. Global.print("创建游戏节点 strPrefabs = " + prefab);
  167. UIHelper.hideWaitNode();
  168. UIHelper.hideAMask();
  169. let gameNode = null;
  170. if (Array.isArray(prefab)) {
  171. gameNode = cc.instantiate(prefab[0]);
  172. let gameNode2 = cc.instantiate(prefab[1]);
  173. gameNode2.zIndex = -1;
  174. gameNode2.parent = gameNode;
  175. } else {
  176. gameNode = cc.instantiate(prefab);
  177. }
  178. let runScene = cc.director.getScene().getChildByName("GameScence");
  179. runScene.addChild(gameNode);
  180. gameNode.setPosition(cc.v2(cc.winSize / 2, cc.winSize.height / 2));
  181. if (this._currGameNode != undefined && this._currGameNode != null) {
  182. this._currGameNode.removeFromParent(true);
  183. this._currGameNode = null;
  184. }
  185. if (cc.vv && cc.vv.audioMgr) {
  186. cc.vv.audioMgr.stopBackMusic();
  187. }
  188. this._currGameNode = gameNode;
  189. if (this.callBack) {
  190. this.callBack(gameNode);
  191. }
  192. cc.vv.Revenue = cc.vv.globalUserInfo.getRevenue()//记录进游戏时候福卡数量
  193. // if (!cc.vv.config.ReviewApk) {
  194. // cc.vv.GetHallScript().onBtnSelectRoomOut();
  195. // }
  196. if (window.LoadGameTime) {
  197. let intervalTime = (Date.now() - window.LoadGameTime) / 1000; // 单位 s(秒)
  198. if (intervalTime <= 3) {
  199. cc.userOperate.sendRecord("enter-Game Time0-3s");
  200. } else if (intervalTime <= 5) {
  201. cc.userOperate.sendRecord("enter-Game Time3-5s");
  202. } else if (intervalTime <= 10) {
  203. cc.userOperate.sendRecord("enter-Game Time5-10s");
  204. } else if (intervalTime <= 15) {
  205. cc.userOperate.sendRecord("enter-Game Time10-15s");
  206. } else {
  207. cc.userOperate.sendRecord("enter-Game Time15s+");
  208. }
  209. window.LoadGameTime = null;
  210. }
  211. })
  212. }
  213. gameBackScence() {
  214. cc.vv.audioMgr.stopBackMusic();
  215. cc.vv.audioMgr.stopAllEffects();
  216. if (this._currGameNode != undefined && this._currGameNode != null) {
  217. this._currGameNode.removeFromParent(true);
  218. this._currGameNode = null;
  219. let runScene = cc.director.getScene();
  220. runScene.getChildByName("Canvas").active = true;
  221. //返回大厅界面
  222. cc.vv.GetHallScript().gameToHall();
  223. }
  224. this._currGameComponent = null;
  225. let rootComponent = this.getRootComponent();
  226. if (rootComponent) {
  227. rootComponent.gameBackScence();
  228. }
  229. }
  230. };
  231. HNScenceManager.Instance = function () {
  232. if (!_ScenceManagerInstance) {
  233. _ScenceManagerInstance = new HNScenceManager();
  234. }
  235. return _ScenceManagerInstance;
  236. }
  237. module.exports = HNScenceManager;