367 lines
9.0 KiB
JavaScript
367 lines
9.0 KiB
JavaScript
|
|
// --------------------------------------------------------------------
|
||
|
|
// @author: shiraho@syg.com(必填, 创建模块的人员)
|
||
|
|
// @description:
|
||
|
|
// 主场景的入口
|
||
|
|
// <br/>Create: new Date().toISOString()
|
||
|
|
// --------------------------------------------------------------------
|
||
|
|
|
||
|
|
require( "global" );
|
||
|
|
require( "client" );
|
||
|
|
|
||
|
|
// Nx框架接入
|
||
|
|
const CFG = require( "config" );
|
||
|
|
const BDef = require( "bridge.define" );
|
||
|
|
const NxEntry = require( "nx.entry" );
|
||
|
|
const FFTimer = require( "timer_help" );
|
||
|
|
const FFSDK = require( "ff_sdk" );
|
||
|
|
const RoleController = require( "role_controller" );
|
||
|
|
|
||
|
|
// if( !CC_EDITOR && !cc.sys.isBrowser ) {
|
||
|
|
// if( !CC_EDITOR ) {
|
||
|
|
// window.onerror = function( message, source, lineno, colno, error ) {
|
||
|
|
// console.log( "onerror!" );
|
||
|
|
// let txt = message;
|
||
|
|
// if( error ) {
|
||
|
|
// txt = error.message + "\n" + error.stack;
|
||
|
|
// }
|
||
|
|
|
||
|
|
// var url = "http://twsa.qa.fitfun.net:8310/upload_file.php";
|
||
|
|
// var xhr = new XMLHttpRequest();
|
||
|
|
// xhr.open( "post", url );
|
||
|
|
// xhr.setRequestHeader( "Content-Type", "application/json" );
|
||
|
|
// xhr.send( txt );
|
||
|
|
// alert( txt );
|
||
|
|
// };
|
||
|
|
// }
|
||
|
|
|
||
|
|
if( cc.sys.isNative ) {
|
||
|
|
|
||
|
|
window.__errorHandler = function( errorMessage, file, line, message, error ) {
|
||
|
|
|
||
|
|
if( !window.nx || !nx.logger || !nx.bridge ) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 上传地址无效
|
||
|
|
let url = nx.bridge.vget( "bugURL" );
|
||
|
|
if( nx.dt.strEmpty( url ) ) {
|
||
|
|
console.log( "BUG提交失败,地址为空..." );
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log( "BUG提交中..." + url );
|
||
|
|
nx.logger.bugUpload( url, {
|
||
|
|
errorMessage: errorMessage,
|
||
|
|
file : file || "",
|
||
|
|
line : line || "",
|
||
|
|
message : message || "",
|
||
|
|
error : error || "",
|
||
|
|
});
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
cc.Class( {
|
||
|
|
|
||
|
|
extends: cc.Component,
|
||
|
|
|
||
|
|
properties: {
|
||
|
|
bg: { default: null, type: cc.Node },
|
||
|
|
views: { default: null, type: cc.Node },
|
||
|
|
gfont: { default: null, type: cc.Font },
|
||
|
|
},
|
||
|
|
|
||
|
|
onLoad: function() {
|
||
|
|
|
||
|
|
console.log( "ffengine gameStart.onLoad" );
|
||
|
|
|
||
|
|
// 引擎模块载入
|
||
|
|
NxEntry.install( /*this.nodeScene, this.nodeUI*/ );
|
||
|
|
|
||
|
|
window.game = this;
|
||
|
|
this.configs = {};
|
||
|
|
|
||
|
|
// 重启处理
|
||
|
|
this.doRestart();
|
||
|
|
|
||
|
|
// 前后台切换
|
||
|
|
this.inback = false;
|
||
|
|
this.dtback = cc.sys.now();
|
||
|
|
|
||
|
|
this.isMainScene = true;
|
||
|
|
|
||
|
|
cc.game.on( cc.game.EVENT_HIDE, this.onGameHide, this );
|
||
|
|
cc.game.on( cc.game.EVENT_SHOW, this.onGameShow, this );
|
||
|
|
|
||
|
|
// 返回按键监听
|
||
|
|
cc.systemEvent.on( "keyup", this.onKeyDone, this );
|
||
|
|
|
||
|
|
// 切场景不卸载
|
||
|
|
cc.game.addPersistRootNode( this.node );
|
||
|
|
},
|
||
|
|
|
||
|
|
// 销毁
|
||
|
|
onDestroy: function() {
|
||
|
|
|
||
|
|
// 前后台切换
|
||
|
|
cc.game.off( cc.game.EVENT_HIDE, this.onGameHide, this );
|
||
|
|
cc.game.off( cc.game.EVENT_SHOW, this.onGameShow, this );
|
||
|
|
|
||
|
|
// 返回按键监听解除
|
||
|
|
cc.systemEvent.off( "keyup", this.onKeyDone, this );
|
||
|
|
|
||
|
|
// 引擎卸载
|
||
|
|
NxEntry.uninstall();
|
||
|
|
},
|
||
|
|
|
||
|
|
start: function() {
|
||
|
|
|
||
|
|
// 本地化
|
||
|
|
this.localization();
|
||
|
|
},
|
||
|
|
|
||
|
|
// 本地化
|
||
|
|
localization: function() {
|
||
|
|
|
||
|
|
nx.gui.setActive( this.bg, "", true );
|
||
|
|
|
||
|
|
// 设置语言标识
|
||
|
|
let key = nx.frame.vget( "LocalLanguage" );
|
||
|
|
this.setLanguage( key, ( _ret ) => {
|
||
|
|
this.fetchOSS();
|
||
|
|
} );
|
||
|
|
},
|
||
|
|
|
||
|
|
// 获取配置
|
||
|
|
fetchOSS: function() {
|
||
|
|
|
||
|
|
nx.bridge.remote.fetch( ( _err ) => {
|
||
|
|
|
||
|
|
if( _err ) {
|
||
|
|
nx.mbox( nx.fmtError( _err ), [ 'confirm' ], () => {
|
||
|
|
nx.restart();
|
||
|
|
} )
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 埋点重置
|
||
|
|
if( nx.mTrace ) {
|
||
|
|
nx.mTrace.trace( 1001, "", "", nx.dt.fnEmpty, true );
|
||
|
|
}
|
||
|
|
|
||
|
|
// 热更
|
||
|
|
this.tryHotUpdate();
|
||
|
|
|
||
|
|
} );
|
||
|
|
|
||
|
|
},
|
||
|
|
|
||
|
|
// 热更
|
||
|
|
tryHotUpdate: function() {
|
||
|
|
|
||
|
|
if( !FFSDK.getInstance().isEnableSDK() ) {
|
||
|
|
this.launchDone();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
let mark = nx.storage.get( "FCSkipUpdate", "" );
|
||
|
|
if( mark != "" ) {
|
||
|
|
nx.frame.vset( "HotVersion", mark );
|
||
|
|
nx.storage.set( "FCSkipUpdate", "" );
|
||
|
|
this.launchDone();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
nx.bridge.setGS( BDef.GameStage.HotUpdate );
|
||
|
|
},
|
||
|
|
|
||
|
|
// 启动完毕
|
||
|
|
launchDone: function() {
|
||
|
|
|
||
|
|
let times = nx.storage.getNumber( "FCLauchTimes", 0 );
|
||
|
|
if( nx.dt.numPositive( times, false ) ) {
|
||
|
|
nx.bridge.setGS( BDef.GameStage.Login );
|
||
|
|
} else {
|
||
|
|
nx.bridge.setGS( BDef.GameStage.LaunchAnim );
|
||
|
|
}
|
||
|
|
nx.storage.set( "FCLauchTimes", ++times );
|
||
|
|
|
||
|
|
window.setTimeout( () => {
|
||
|
|
nx.gui.setActive( this.bg, "", false );
|
||
|
|
}, 3000 );
|
||
|
|
},
|
||
|
|
|
||
|
|
// 切入登录
|
||
|
|
enterLogin: function( _restart = false ) {
|
||
|
|
|
||
|
|
if( FFSDK.getInstance().isEnableSDK() ) {
|
||
|
|
FFSDK.getInstance().logOut();
|
||
|
|
}
|
||
|
|
|
||
|
|
if( _restart ) {
|
||
|
|
nx.restart();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
nx.assets.uncacheAssets();
|
||
|
|
client.socket.close( "relogin" );
|
||
|
|
nx.bridge.setGS( BDef.GameStage.Login );
|
||
|
|
},
|
||
|
|
|
||
|
|
// 帧更新
|
||
|
|
update: function( dt ) {
|
||
|
|
|
||
|
|
// 消息处理
|
||
|
|
if( client.socket ) {
|
||
|
|
client.socket.handleMsg();
|
||
|
|
}
|
||
|
|
|
||
|
|
// SDK
|
||
|
|
FFTimer.getInstance().onUpdate( dt );
|
||
|
|
},
|
||
|
|
|
||
|
|
// 帧更新
|
||
|
|
lateUpdate: function() {
|
||
|
|
|
||
|
|
// SDK
|
||
|
|
FFTimer.getInstance().onLateUpdate();
|
||
|
|
},
|
||
|
|
|
||
|
|
// 设置语种
|
||
|
|
setLanguage( _key, _cb ) {
|
||
|
|
|
||
|
|
nx.bridge.setLang( _key );
|
||
|
|
|
||
|
|
// 重载文本库
|
||
|
|
nx.text.ready( ( _err ) => {
|
||
|
|
nx.dt.fnInvoke( _cb );
|
||
|
|
} );
|
||
|
|
|
||
|
|
// 重载字体
|
||
|
|
let path = cc.path.join( "locals", nx.getLocLanguage(), "font" );
|
||
|
|
nx.res.loadFont( path, ( _err, _font ) => {
|
||
|
|
if( !_err ) {
|
||
|
|
this.gfont = _font;
|
||
|
|
nx.frame.vset( "LocalFont", path );
|
||
|
|
}
|
||
|
|
}, cc.Font );
|
||
|
|
|
||
|
|
},
|
||
|
|
|
||
|
|
// 重启处理
|
||
|
|
doRestart: function() {
|
||
|
|
|
||
|
|
// 缓存清理
|
||
|
|
if( nx && nx.assets ) {
|
||
|
|
nx.assets.uncacheAssets( true );
|
||
|
|
}
|
||
|
|
|
||
|
|
// SOCKET断开&&重新初始化一次
|
||
|
|
if( client && client.socket ) {
|
||
|
|
client.socket.close( "restart" );
|
||
|
|
client.socket.init();
|
||
|
|
}
|
||
|
|
|
||
|
|
// 事件监听清空
|
||
|
|
if( gcore && gcore.GlobalEvent ) {
|
||
|
|
gcore.GlobalEvent._evt_idx = 0;
|
||
|
|
gcore.GlobalEvent._evt_list = {};
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
// 切前台
|
||
|
|
onGameShow: function() {
|
||
|
|
|
||
|
|
// 已经在前台,不重复处理
|
||
|
|
if( !this.inback ) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 300秒重启游戏
|
||
|
|
let ms = cc.sys.now() - this.dtback;
|
||
|
|
if( ms > 1000 * 300 ) {
|
||
|
|
nx.restart();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 音乐恢复
|
||
|
|
this.inback = false;
|
||
|
|
nx.audio.resumeAll();
|
||
|
|
},
|
||
|
|
|
||
|
|
// 切后台
|
||
|
|
onGameHide: function() {
|
||
|
|
|
||
|
|
// 已经在后台,不重复处理
|
||
|
|
if( this.inback ) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 记录时间
|
||
|
|
this.inback = true;
|
||
|
|
this.dtback = cc.sys.now();
|
||
|
|
|
||
|
|
// 音乐暂停
|
||
|
|
nx.audio.pauseAll();
|
||
|
|
},
|
||
|
|
|
||
|
|
// 按键捕获
|
||
|
|
onKeyDone: function( _event ) {
|
||
|
|
|
||
|
|
if( cc.macro.KEY.back != _event.keyCode &&
|
||
|
|
cc.macro.KEY.escape != _event.keyCode ) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 否则提示退出游戏
|
||
|
|
nx.mbox( "AskQuitGame", [ "no", "yes" ], ( _key, _box ) => {
|
||
|
|
_box.close();
|
||
|
|
if( _key == "yes" ) {
|
||
|
|
nx.appQuit();
|
||
|
|
}
|
||
|
|
} ,"AskQuitGameTag")
|
||
|
|
|
||
|
|
},
|
||
|
|
|
||
|
|
runMainScene : function () {
|
||
|
|
|
||
|
|
if ( this.isMainScene ) { return; }
|
||
|
|
this.isMainScene = true;
|
||
|
|
console.log( "首次进入游戏------" );
|
||
|
|
// nx.bridge.closePanel("WndBattleScene");
|
||
|
|
|
||
|
|
cc.director.loadScene( "main_scene", () => {
|
||
|
|
|
||
|
|
// 首次进入引导
|
||
|
|
let role = RoleController.getInstance().getRoleVo();
|
||
|
|
if( role &&
|
||
|
|
role.lev <= 1 &&
|
||
|
|
nx.dt.arrEmpty( nx.bridge.plot.records.ids ) ) {
|
||
|
|
nx.debug( "$GAME:进入首次引导场景..." );
|
||
|
|
nx.bridge.createPanel( BDef.StageScene[ BDef.GameStage.First ] );
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 新界面展示
|
||
|
|
nx.bridge.createPanel( BDef.StageScene[ BDef.GameStage.Game ] );
|
||
|
|
this.scheduleOnce(()=>{
|
||
|
|
//界面回溯
|
||
|
|
nx.bridge.resetTag2PanelfromBat();
|
||
|
|
},0.1);
|
||
|
|
} );
|
||
|
|
},
|
||
|
|
|
||
|
|
runBattleScene : function ( _params ) {
|
||
|
|
|
||
|
|
if ( ! this.isMainScene ) { return; }
|
||
|
|
this.isMainScene = false;
|
||
|
|
console.log( "首次进入游戏战斗场景------" );
|
||
|
|
// nx.bridge.createPanel("WndBattleScene", _params );
|
||
|
|
|
||
|
|
cc.director.loadScene( "battle_scene", () => {
|
||
|
|
|
||
|
|
nx.bridge.createPanel("WndBattleScene", _params );
|
||
|
|
} );
|
||
|
|
},
|
||
|
|
|
||
|
|
} );
|