game.ejs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. <%- include(cocosTemplate, {}) %>
  6. // Adapt for IOS, swap if opposite
  7. const info = wx.getSystemInfoSync();
  8. if (canvas){
  9. var _w = canvas.width;
  10. var _h = canvas.height;
  11. if (info.screenWidth < info.screenHeight) {
  12. if (canvas.width > canvas.height) {
  13. _w = canvas.height;
  14. _h = canvas.width;
  15. }
  16. } else {
  17. if (canvas.width < canvas.height) {
  18. _w = canvas.height;
  19. _h = canvas.width;
  20. }
  21. }
  22. canvas.width = _w;
  23. canvas.height = _h;
  24. }
  25. // Adjust initial canvas size
  26. if (canvas && window.devicePixelRatio >= 2) {canvas.width *= info.devicePixelRatio; canvas.height *= info.devicePixelRatio;}
  27. const importMap = require("<%= importMapFile%>").default;
  28. System.warmup({
  29. importMap,
  30. importMapUrl: '<%= importMapFile%>',
  31. defaultHandler: (urlNoSchema) => {
  32. require('.' + urlNoSchema);
  33. },
  34. handlers: {
  35. 'plugin:': (urlNoSchema) => {
  36. requirePlugin(urlNoSchema);
  37. },
  38. 'project:': (urlNoSchema) => {
  39. require(urlNoSchema);
  40. },
  41. },
  42. });
  43. firstScreen.start('<%= alpha %>', '<%= antialias %>', '<%= useWebgl2 %>').then(() => {
  44. return System.import('<%= applicationJs %>');
  45. }).then((module) => {
  46. return firstScreen.setProgress(0.2).then(() => Promise.resolve(module));
  47. }).then(({ Application }) => {
  48. return new Application();
  49. }).then((application) => {
  50. return firstScreen.setProgress(0.4).then(() => Promise.resolve(application));
  51. }).then((application) => {
  52. return onApplicationCreated(application);
  53. }).catch((err) => {
  54. console.error(err);
  55. });
  56. function onApplicationCreated(application) {
  57. return System.import('cc').then((module) => {
  58. return firstScreen.setProgress(0.6).then(() => Promise.resolve(module));
  59. }).then((cc) => {
  60. require('./engine-adapter');
  61. return application.init(cc);
  62. }).then(() => {
  63. return firstScreen.end().then(() => application.start());
  64. });
  65. }
  66. } // init app
  67. // NOTE: on WeChat Android end, we can only get the correct screen size at the second tick of game.
  68. var sysInfo = wx.getSystemInfoSync();
  69. if (sysInfo.platform.toLocaleLowerCase() === 'android') {
  70. GameGlobal.requestAnimationFrame (__initApp);
  71. } else {
  72. __initApp();
  73. }