HNScenceManager.js 8.7 KB

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