244 lines
5.6 KiB
JavaScript
244 lines
5.6 KiB
JavaScript
|
|
/*******************************************************************************
|
|
*
|
|
* Nx框架
|
|
*
|
|
* 产生全局服务入口window.nx
|
|
*
|
|
* 2021.12.10
|
|
******************************************************************************/
|
|
|
|
const ERR = require( "error" );
|
|
const NxDefine = require( "nx.define" );
|
|
const NxMod = require( "nx.mod" );
|
|
|
|
var NxEntry = cc.Class( {
|
|
|
|
extends: NxMod,
|
|
|
|
name: "NxEntry",
|
|
|
|
// 初始化
|
|
initialize: function( _args ) {
|
|
|
|
// 初始化
|
|
if( !this._super( _args ) ) {
|
|
cc.error( "初始化NX框架失败!" );
|
|
return false;
|
|
}
|
|
|
|
nx.frame = this;
|
|
|
|
// 系统初始化
|
|
this._initSystemVars();
|
|
this.initGameSpeed();
|
|
|
|
// 模块载入
|
|
nx.factory.createService( "nx.gui" );
|
|
nx.factory.createService( "nx.res" );
|
|
nx.factory.createService( "nx.tween" );
|
|
nx.factory.createService( "nx.storage" );
|
|
nx.factory.createService( "nx.assets" );
|
|
nx.factory.createService( "nx.event" );
|
|
nx.factory.createService( "nx.misc" );
|
|
nx.factory.createService( "nx.text" );
|
|
nx.factory.createService( "nx.view.mgr" );
|
|
nx.factory.createService( "nx.plugin.manager" );
|
|
|
|
// 视图创建
|
|
nx.plugin.add( this, [ "view" ] );
|
|
this.vbuild( NxDefine.ViewFrame );
|
|
|
|
// nx.factory.createService( "nx.cmd.manager" );
|
|
nx.factory.createService( "nx.funs" );
|
|
nx.factory.createService( "nx.audio" );
|
|
nx.factory.createService( "nx.timers" );
|
|
nx.factory.createService( "nx.web.gate" );
|
|
nx.factory.createService( "nx.jsr" );
|
|
// nx.factory.createService( "nxJsb" );
|
|
|
|
// SDK实例化
|
|
this.initSDK();
|
|
|
|
// 视图参数初始化
|
|
this._initViewData();
|
|
|
|
// 通用命令注册
|
|
this.instCmds();
|
|
|
|
return true;
|
|
},
|
|
|
|
// 销毁
|
|
uninitialize: function() {
|
|
|
|
// 通用命令注销
|
|
this.uninstCmds();
|
|
|
|
// 窗体尺寸变化监听解除
|
|
cc.view.setResizeCallback( null );
|
|
|
|
nx.frame = null;
|
|
|
|
// USPER
|
|
return this._super();
|
|
},
|
|
|
|
// 系统初始化
|
|
_initSystemVars: function() {
|
|
|
|
// 设备初始化
|
|
// cc.game.setFrameRate( CFG.FPS );
|
|
// cc.game.showFPS = false;
|
|
},
|
|
|
|
// 视图参数初始化
|
|
_initViewData: function() {
|
|
|
|
// 窗体尺寸变化监听
|
|
cc.view.setResizeCallback( this._freshFrameSize.bind( this ) );
|
|
this._freshFrameSize();
|
|
|
|
},
|
|
|
|
// 刷新窗体尺寸
|
|
_freshFrameSize: function() {
|
|
|
|
let fs = cc.view.getFrameSize();
|
|
let ds = cc.view.getDesignResolutionSize();
|
|
|
|
this.vset( "IsLandscape", ds.width >= ds.height );
|
|
|
|
let hfr = fs.height / fs.width;
|
|
let hdr = ds.height / ds.width;
|
|
this.vset( "HeightRate", hdr / hfr );
|
|
|
|
// 最大宽高比
|
|
// 主要用于背景图等比填充
|
|
this.vset( "DesignWHRate", ds.width / ds.height );
|
|
this.vset( "RuntimeWHRate", fs.width / fs.height );
|
|
this.vset( "MaxWHRate", 1560 / 720 );
|
|
|
|
},
|
|
|
|
/***
|
|
* *********************************************************************
|
|
* 命令相关
|
|
* *********************************************************************
|
|
*/
|
|
|
|
// 通用命令安装
|
|
instCmds: function() {
|
|
|
|
if( !nx.cmd ) {
|
|
return;
|
|
}
|
|
|
|
// // 创建窗口
|
|
// nx.cmd.regCmd( "open_window", ( _cb, _name, _p1, _p2 ) => {
|
|
|
|
// nx.createWindow( _name, ( _e, _p ) => {
|
|
// nx.dt.fnInvoke( _cb, _e, _p );
|
|
// }, { p1: _p1, p2: _p2 } );
|
|
|
|
// }, "创建指定窗口 [窗体名][参数1][参数2]" );
|
|
|
|
// // 关闭窗口
|
|
// nx.cmd.regCmd( "close_window", ( _cb, _name ) => {
|
|
|
|
// nx.closeWindow( _name );
|
|
// nx.dt.fnInvoke( _cb );
|
|
|
|
// }, "关闭指定窗口 [窗体名]" );
|
|
|
|
// 发送消息
|
|
nx.cmd.regCmd( "send_event", ( _cb, _event, _p1, _p2 ) => {
|
|
|
|
let ret = nx.event.send( _event, { p1: _p1, p2: _p2 } );
|
|
nx.dt.fnInvoke( _cb, ret ? null : nx.genError( ERR.COM.InvalidEvent ) );
|
|
|
|
}, "发送消息 [事件名][参数1][参数2]" );
|
|
|
|
},
|
|
|
|
// 通用命令卸载
|
|
uninstCmds: function() {
|
|
|
|
if( nx.cmd ) {
|
|
// nx.cmd.unregCmd( "open_window" );
|
|
// nx.cmd.unregCmd( "close_window" );
|
|
nx.cmd.unregCmd( "send_event" );
|
|
}
|
|
},
|
|
|
|
/***
|
|
* *********************************************************************
|
|
* SDK相关
|
|
* *********************************************************************
|
|
*/
|
|
|
|
// SDK实例化
|
|
initSDK: function() {
|
|
|
|
// 根据当前平台创建SDK接口
|
|
let sdk = "nx.sdk.web";
|
|
if( cc.sys.isNative ) {
|
|
switch( cc.sys.os ) {
|
|
case cc.sys.OS_IOS: sdk = "nx.sdk.ios"; break;
|
|
// case cc.sys.OS_OSX: sdk = "nx.sdk.ios"; break;
|
|
case cc.sys.OS_ANDROID: sdk = "nx.sdk.android"; break;
|
|
default: break;
|
|
}
|
|
}
|
|
nx.info( "[SDK系统]" + cc.sys.os + ",[当前SDK]" + sdk );
|
|
nx.sdk = nx.factory.createService( sdk );
|
|
|
|
},
|
|
|
|
/***
|
|
* *********************************************************************
|
|
* 游戏加速
|
|
* *********************************************************************
|
|
*/
|
|
|
|
// 游戏加速初始化
|
|
initGameSpeed: function() {
|
|
|
|
if( !this.gspeed ) {
|
|
this.setGameSpeed( 1 );
|
|
}
|
|
|
|
// Director方法覆盖
|
|
cc.Director.prototype.calculateDeltaTime = function( now ) {
|
|
|
|
if( !now ) now = performance.now();
|
|
|
|
// avoid delta time from being negative
|
|
// negative deltaTime would be caused by the precision of now's value, for details please see: https://developer.mozilla.org/zh-CN/docs/Web/API/window/requestAnimationFrame
|
|
this._deltaTime = now > this._lastUpdate ? ( now - this._lastUpdate ) / 1000 : 0;
|
|
if( CC_DEBUG && ( this._deltaTime > 1 ) ) {
|
|
this._deltaTime = 1 / 60.0;
|
|
}
|
|
if( window.nx && nx.frame ){
|
|
this._deltaTime *= nx.frame.gspeed;
|
|
}
|
|
|
|
this._lastUpdate = now;
|
|
}
|
|
|
|
},
|
|
|
|
// 设置速度
|
|
setGameSpeed: function( _speed ) {
|
|
this.gspeed = Math.max( 1, _speed );
|
|
},
|
|
|
|
// 获取速度
|
|
getGameSpeed: function() {
|
|
return this.gspeed;
|
|
},
|
|
|
|
} );
|
|
|
|
module.exports = NxEntry;
|