game.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. function __initApp () { // init app
  2. globalThis.__wxRequire = require; // FIX: require cannot work in separate engine
  3. require('./web-adapter');
  4. const firstScreen = require('./first-screen');
  5. // Polyfills bundle.
  6. require("src/polyfills.bundle.js");
  7. // SystemJS support.
  8. require("src/system.bundle.js");
  9. // Adapt for IOS, swap if opposite
  10. const info = wx.getSystemInfoSync();
  11. if (canvas){
  12. var _w = canvas.width;
  13. var _h = canvas.height;
  14. if (info.screenWidth < info.screenHeight) {
  15. if (canvas.width > canvas.height) {
  16. _w = canvas.height;
  17. _h = canvas.width;
  18. }
  19. } else {
  20. if (canvas.width < canvas.height) {
  21. _w = canvas.height;
  22. _h = canvas.width;
  23. }
  24. }
  25. canvas.width = _w;
  26. canvas.height = _h;
  27. }
  28. if (canvas && info.devicePixelRatio >= 2) {canvas.width *= info.devicePixelRatio; canvas.height *= info.devicePixelRatio;}
  29. const importMap = require("src/import-map.js").default;
  30. System.warmup({
  31. importMap,
  32. importMapUrl: 'src/import-map.js',
  33. defaultHandler: (urlNoSchema) => {
  34. require('.' + urlNoSchema);
  35. },
  36. handlers: {
  37. 'plugin:': (urlNoSchema) => {
  38. requirePlugin(urlNoSchema);
  39. },
  40. 'project:': (urlNoSchema) => {
  41. require(urlNoSchema);
  42. },
  43. },
  44. });
  45. firstScreen.start('default', 'default', 'false').then(() => {
  46. return System.import('./application.js');
  47. }).then((module) => {
  48. return firstScreen.setProgress(0.2).then(() => Promise.resolve(module));
  49. }).then(({ Application }) => {
  50. return new Application();
  51. }).then((application) => {
  52. return firstScreen.setProgress(0.4).then(() => Promise.resolve(application));
  53. }).then((application) => {
  54. return onApplicationCreated(application);
  55. }).catch((err) => {
  56. console.error(err);
  57. });
  58. function onApplicationCreated(application) {
  59. return System.import('cc').then((module) => {
  60. return firstScreen.setProgress(0.6).then(() => Promise.resolve(module));
  61. }).then((cc) => {
  62. require('./engine-adapter');
  63. return application.init(cc);
  64. }).then(() => {
  65. return firstScreen.end().then(application.start());
  66. // return application.start();
  67. });
  68. }
  69. } // init app
  70. // NOTE: on WeChat Android end, we can only get the correct screen size at the second tick of game.
  71. var sysInfo = wx.getSystemInfoSync();
  72. if (sysInfo.platform.toLocaleLowerCase() === 'android') {
  73. GameGlobal.requestAnimationFrame (__initApp);
  74. } else {
  75. __initApp();
  76. }