Files
cocos4/templates/wechatprogram/game.ejs
T
mmyduckx 38c0ed4f95 add wechatprogram platform support (#14580)
* add wechatprogram platform support
2023-03-15 14:13:24 +08:00

84 lines
3.0 KiB
Plaintext

globalThis.__wxRequire = require; // FIX: require cannot work in separate engine
var GameGlobal = getApp().GameGlobal;
Page({
onReady() {
const query = wx.createSelectorQuery()
query.select('#myCanvas').node().exec((res) => {
// require('./miniapp-adapter');
const canvas = res[0].node;
let width = canvas.width;
let height = canvas.height;
const adapter = require('./miniapp-adapter');
adapter.adapt(canvas, width, height);
require('./web-adapter');
const firstScreen = require('./first-screen');
<%- include(cocosTemplate, {}) %>
// Adjust initial canvas size
if (canvas && GameGlobal.devicePixelRatio >= 2) {
canvas.width *= 2; canvas.height *= 2;
}
const importMap = require("<%= importMapFile%>").default;
const System = GameGlobal.System;
System.warmup({
importMap,
importMapUrl: '<%= importMapFile%>',
defaultHandler: (urlNoSchema) => {
require('.' + urlNoSchema);
},
handlers: {
'plugin:': (urlNoSchema) => {
requirePlugin(urlNoSchema);
},
'project:': (urlNoSchema) => {
require(urlNoSchema);
},
},
});
firstScreen.start('<%= alpha %>', '<%= antialias %>', '<%= useWebgl2 %>').then(() => {
return System.import('<%= applicationJs %>');
}).then((module) => {
return firstScreen.setProgress(0.2).then(() => Promise.resolve(module));
}).then(({ Application }) => {
return new Application();
}).then((application) => {
return firstScreen.setProgress(0.4).then(() => Promise.resolve(application));
}).then((application) => {
return onApplicationCreated(application);
}).catch((err) => {
console.error(err);
});
function onApplicationCreated(application) {
return System.import('cc').then((module) => {
return firstScreen.setProgress(0.6).then(() => Promise.resolve(module));
}).then((cc) => {
require('./engine-adapter');
return application.init(cc);
}).then(() => {
return firstScreen.end().then(() => application.start());
});
}
})
},
touchStart(event) {
GameGlobal._touchStart && GameGlobal._touchStart(event);
},
touchEnd(event) {
GameGlobal._touchEnd && GameGlobal._touchEnd(event);
},
touchMove(event) {
GameGlobal._touchMove && GameGlobal._touchMove(event);
},
touchCancel(event) {
GameGlobal._touchCancel && GameGlobal._touchCancel(event);
}
})