game.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. // Adjust initial canvas size
  29. if (canvas && window.devicePixelRatio >= 2) {canvas.width *= info.devicePixelRatio; canvas.height *= info.devicePixelRatio;}
  30. const importMap = require("src/import-map.js").default;
  31. System.warmup({
  32. importMap,
  33. importMapUrl: 'src/import-map.js',
  34. defaultHandler: (urlNoSchema) => {
  35. require('.' + urlNoSchema);
  36. },
  37. handlers: {
  38. 'plugin:': (urlNoSchema) => {
  39. requirePlugin(urlNoSchema);
  40. },
  41. 'project:': (urlNoSchema) => {
  42. require(urlNoSchema);
  43. },
  44. },
  45. });
  46. firstScreen.start('default', 'default', 'false').then(() => {
  47. return System.import('./application.js');
  48. }).then((module) => {
  49. return firstScreen.setProgress(0.2).then(() => Promise.resolve(module));
  50. }).then(({ Application }) => {
  51. return new Application();
  52. }).then((application) => {
  53. return firstScreen.setProgress(0.4).then(() => Promise.resolve(application));
  54. }).then((application) => {
  55. return onApplicationCreated(application);
  56. }).catch((err) => {
  57. console.error(err);
  58. });
  59. function onApplicationCreated(application) {
  60. return System.import('cc').then((module) => {
  61. return firstScreen.setProgress(0.6).then(() => Promise.resolve(module));
  62. }).then((cc) => {
  63. require('./engine-adapter');
  64. return application.init(cc);
  65. }).then(() => {
  66. return firstScreen.end().then(() => 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. }