main.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. var settings = window._CCSettings;
  2. function boot () {
  3. var onStart = function () {
  4. window._CCSettings = undefined;
  5. cc.loader.downloader._subpackages = settings.subpackages;
  6. cc.view.enableRetina(true);
  7. cc.view.resizeWithBrowserSize(true);
  8. //在launchScene面前添加底下这句话。这样子游戏运行的时候就可以判断为QQ小游戏啦。
  9. cc.sys.platform = cc.sys.QQ_PLAY;
  10. var launchScene = settings.launchScene;
  11. // load scene
  12. cc.director.loadScene(launchScene, null,
  13. function () {
  14. cc.view.setDesignResolutionSize(750, 1334, 4);
  15. cc.loader.onProgress = null;
  16. console.log('Success to load scene: ' + launchScene);
  17. }
  18. );
  19. };
  20. loadJsListModules(settings.jsList).then(function () {
  21. (boot.systemGlobal || System)['import']('virtual:///prerequisite-imports:main').then(function () {
  22. cc.game.run(onStart);
  23. }).catch(function (error) {
  24. console.error("Load project module error: \n" + error);
  25. });
  26. });
  27. };
  28. window.boot = boot;
  29. // Generate options to init cc.game
  30. function initOptions () {
  31. var uuids = settings.uuids;
  32. var rawAssets = settings.rawAssets;
  33. var assetTypes = settings.assetTypes;
  34. var realRawAssets = settings.rawAssets = {};
  35. for (var mount in rawAssets) {
  36. var entries = rawAssets[mount];
  37. var realEntries = realRawAssets[mount] = {};
  38. for (var id in entries) {
  39. var entry = entries[id];
  40. var type = entry[1];
  41. // retrieve minified raw asset
  42. if (typeof type === 'number') {
  43. entry[1] = assetTypes[type];
  44. }
  45. // retrieve uuid
  46. realEntries[uuids[id] || id] = entry;
  47. }
  48. }
  49. var scenes = settings.scenes;
  50. for (var i = 0; i < scenes.length; ++i) {
  51. var scene = scenes[i];
  52. if (typeof scene.uuid === 'number') {
  53. scene.uuid = uuids[scene.uuid];
  54. }
  55. }
  56. var packedAssets = settings.packedAssets;
  57. for (var packId in packedAssets) {
  58. var packedIds = packedAssets[packId];
  59. for (var j = 0; j < packedIds.length; ++j) {
  60. if (typeof packedIds[j] === 'number') {
  61. packedIds[j] = uuids[packedIds[j]];
  62. }
  63. }
  64. }
  65. var subpackages = settings.subpackages;
  66. for (var subId in subpackages) {
  67. var uuidArray = subpackages[subId].uuids;
  68. if (uuidArray) {
  69. for (var k = 0, l = uuidArray.length; k < l; k++) {
  70. if (typeof uuidArray[k] === 'number') {
  71. uuidArray[k] = uuids[uuidArray[k]];
  72. }
  73. }
  74. }
  75. }
  76. // asset library options
  77. const assetOptions = {
  78. libraryPath: 'res/import',
  79. rawAssetsBase: 'res/raw-',
  80. rawAssets: settings.rawAssets,
  81. packedAssets: settings.packedAssets,
  82. md5AssetsMap: settings.md5AssetsMap,
  83. subPackages: settings.subpackages
  84. };
  85. const options = {
  86. scenes: settings.scenes,
  87. debugMode: settings.debug ? 1 : 3, // cc.debug.DebugMode.INFO : cc.debug.DebugMode.ERROR,
  88. showFPS: !false && settings.debug,
  89. frameRate: 60,
  90. groupList: settings.groupList,
  91. collisionMatrix: settings.collisionMatrix,
  92. renderPipeline: settings.renderPipeline,
  93. adapter: prepare.findCanvas('GameCanvas'),
  94. assetOptions,
  95. customJointTextureLayouts: settings.customJointTextureLayouts || [],
  96. };
  97. return options;
  98. }
  99. // Load all project scripts (built by creator)
  100. function loadJsListModules(jsList) {
  101. // jsList
  102. var promise = Promise.resolve();
  103. if (jsList) {
  104. jsList.forEach(function (x) {
  105. promise = promise.then(function () {
  106. return prepare.loadIIFE(boot.jsListRoot + '/' + x);
  107. });
  108. });
  109. }
  110. return promise;
  111. }
  112. // Load all custom script bundles. Every bundle may contain one or more named registered SystemJS modules, with no module.
  113. function loadScriptPackages(scriptPackages) {
  114. var loadBundlePromises = [];
  115. if (scriptPackages) {
  116. for (var iScriptPackage = 0; iScriptPackage < scriptPackages.length; ++iScriptPackage) {
  117. loadBundlePromises.push(prepare.loadIIFE(scriptPackages[iScriptPackage]));
  118. }
  119. }
  120. return Promise.all(loadBundlePromises);
  121. }
  122. var prepare = function() {
  123. settings = window._CCSettings;
  124. return Promise.resolve(prepare.engine ? prepare.engine() : void 0).then(function() {
  125. return (boot.systemGlobal || System).import('cc');
  126. }).then(function() {
  127. var options = initOptions();
  128. return new Promise(function (resolve, reject) {
  129. let inited = cc.game.init(options);
  130. inited ? resolve() : reject();
  131. });
  132. }).then(function() {
  133. return loadScriptPackages(settings.scriptPackages);
  134. });
  135. };
  136. // Define how to prepare engine so that 'cc' is valid to import.
  137. prepare.engine = void 0;
  138. // Define how to prepare IIFE modules.
  139. prepare.loadIIFE = void 0;
  140. // Adapter: find canvas
  141. prepare.findCanvas = void 0;
  142. // The root url from which we can load js list.
  143. boot.jsListRoot = 'src';
  144. // System JS global. Default to `globalThis.System`.
  145. boot.systemGlobal = undefined;
  146. boot.prepare = prepare;