mirror of
https://github.com/smallmain/cocos-enhance-kit.git
synced 2025-12-08 14:38:44 +00:00
[adapters] 增加小游戏适配部分源码
This commit is contained in:
100
adapters/platforms/bytedance/wrapper/sub-context-adapter.js
Normal file
100
adapters/platforms/bytedance/wrapper/sub-context-adapter.js
Normal file
@@ -0,0 +1,100 @@
|
||||
// Ensure recieving message from main context before engine being inited
|
||||
let isEngineReady = false;
|
||||
const game = cc.game;
|
||||
game.once(game.EVENT_ENGINE_INITED, function () {
|
||||
isEngineReady = true;
|
||||
});
|
||||
|
||||
var viewportInMain = {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 0,
|
||||
height: 0
|
||||
};
|
||||
|
||||
// Touch conversion
|
||||
cc.view.convertToLocationInView = function (tx, ty, relatedPos, out) {
|
||||
var result = out || cc.v2();
|
||||
var x = this._devicePixelRatio * (tx - relatedPos.left);
|
||||
var y = this._devicePixelRatio * (relatedPos.top + relatedPos.height - ty);
|
||||
// Move to real viewport area
|
||||
x = (x - viewportInMain.x) * this._viewportRect.width / viewportInMain.width;
|
||||
y = (y - viewportInMain.y) * this._viewportRect.height / viewportInMain.height;
|
||||
if (this._isRotated) {
|
||||
result.x = this._viewportRect.width - y;
|
||||
result.y = x;
|
||||
}
|
||||
else {
|
||||
result.x = x;
|
||||
result.y = y;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
// In sub context, run main loop after subContextView component get enabled.
|
||||
game._prepareFinished = function (cb) {
|
||||
this._prepared = true;
|
||||
|
||||
// Init engine
|
||||
this._initEngine();
|
||||
cc.assetManager.builtins.init(() => {
|
||||
// Log engine version
|
||||
console.log('Cocos Creator v' + cc.ENGINE_VERSION);
|
||||
|
||||
this._setAnimFrame();
|
||||
|
||||
this.emit(this.EVENT_GAME_INITED);
|
||||
|
||||
if (cb) cb();
|
||||
});
|
||||
};
|
||||
|
||||
tt.onMessage(function (data) {
|
||||
if (data.fromEngine) {
|
||||
if (data.event === 'boot') {
|
||||
game._banRunningMainLoop = false;
|
||||
if (game._firstSceneLaunched) {
|
||||
game._runMainLoop();
|
||||
}
|
||||
}
|
||||
else if (data.event === 'viewport') {
|
||||
viewportInMain.x = data.x;
|
||||
viewportInMain.y = data.y;
|
||||
viewportInMain.width = data.width;
|
||||
viewportInMain.height = data.height;
|
||||
}
|
||||
else if (data.event === 'resize') {
|
||||
window.dispatchEvent({type: 'resize'});
|
||||
}
|
||||
else if (isEngineReady) {
|
||||
if (data.event === 'mainLoop') {
|
||||
if (data.value) {
|
||||
game.resume();
|
||||
}
|
||||
else {
|
||||
game.pause();
|
||||
}
|
||||
}
|
||||
else if (data.event === 'frameRate') {
|
||||
game.setFrameRate(data.value);
|
||||
}
|
||||
else if (data.event === 'step') {
|
||||
game.step();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Canvas component adaptation
|
||||
|
||||
cc.Canvas.prototype.update = function () {
|
||||
if (this._width !== game.canvas.width || this._height !== game.canvas.height) {
|
||||
this.applySettings();
|
||||
}
|
||||
};
|
||||
let originalApplySettings = cc.Canvas.prototype.applySettings;
|
||||
cc.Canvas.prototype.applySettings = function () {
|
||||
originalApplySettings.call(this);
|
||||
this._width = game.canvas.width;
|
||||
this._height = game.canvas.height;
|
||||
};
|
||||
Reference in New Issue
Block a user