/******************************************************************************* * * Nx模块基类 * * 模块嵌套管理 * * 2021.12.10 ******************************************************************************/ const NxObject = require( "nx.object" ); var MODBase = cc.Class( { extends: NxObject, name: "MODBase", // 初始化 initialize: function( _args ) { // USPER this._super( _args ); // 子MOD列表 this.lstMods = []; return true; }, // 销毁 uninitialize: function() { // 子MOD销毁 this.modClean(); // USPER return this._super(); }, /*** * ********************************************************************* * 流程通告 * ********************************************************************* */ // 游戏数据准备(启动时) doPrepare: function( _cb, _args ) { nx.debug( "[Mod]doPrepare:" + this._jsfile ); var count = this.lstMods.length; if( count == 0 ) { nx.dt.fnInvoke( _cb ); return; } var check = function() { --count; if( count == 0 ) { nx.dt.fnInvoke( _cb ); } } this.lstMods.forEach( _mod => { _mod.doPrepare( check, _args ); } ); }, // 通告: 进入大厅后,发送准备前 onEnter: function() { // 子模板事件通告 this.lstMods.forEach( _mod => { _mod.onEnter(); } ); }, // 通告: 登入 onLogin: function() { // 子模板事件通告 this.lstMods.forEach( _mod => { _mod.onLogin(); } ); }, // 通告: 登出 onLogout: function() { // 子模板事件通告 this.lstMods.forEach( _mod => { _mod.onLogout(); } ); }, // 通告: 切后台 onBackground: function() { // 子模板事件通告 this.lstMods.forEach( _mod => { _mod.onBackground(); } ); }, // 通告: 切前台 onFrontend: function( _ms ) { // 子模板事件通告 this.lstMods.forEach( _mod => { _mod.onFrontend( _ms ); } ); }, /*** * ********************************************************************* * 存档相关 * ********************************************************************* */ // 序列化: 初始数据 onSerializeRaws: function( _records ) { // 子模板序列化 this.lstMods.forEach( _mod => { _mod.onSerializeRaws( _records ); } ); }, // 序列化: 保存 onSerialize: function( _records ) { // 子模板序列化 this.lstMods.forEach( _mod => { _mod.onSerialize( _records ); } ); }, // 反序列化: 读取 onDeserialize: function( _records ) { // 子模板序列化 this.lstMods.forEach( _mod => { _mod.onDeserialize( _records ); } ); }, /*** * ********************************************************************* * 子模块操作 * ********************************************************************* */ // 创建子MOD modCreate: function( _name, _jsfile, _args ) { // 参数合法检查 if( nx.dt.strEmpty( _name ) || nx.dt.strEmpty( _jsfile ) ) { nx.error( "[Mod]创建MOD失败!" ); return null; } // 重复创建检查 let mod = this.modQuery( _name, false ); if( mod ) { nx.warn( "[Mod]重复创建MOD:", _name ); return mod; } // 创建 mod = nx.factory.create( _jsfile, _args || {} ); if( !mod ) { nx.warn( "[Mod]创建MOD失败:", _name, _jsfile ); return null; } mod._modKey = _name; this.lstMods.push( mod ); nx.debug( "[Mod]创建子MOD:", _name, _jsfile ); return mod; }, // 添加子MOD /** * 两种参数格式: * 1: 字符串数组[ ModName, ModName, ... ] * 2: 信息数组[ [ ModName, ModFile, ModArgs ], ... ] */ modCreateBatch: function( _list ) { // 空返回 if( nx.dt.arrEmpty( _list ) ) { return; } // 统计队列 let infos = []; _list.forEach( _ifo => { // 字符串数组 if( nx.dt.strNEmpty( _ifo ) ) { infos.push( { name: _ifo, file: _ifo, args: {} } ); } // 信息数组 else { infos.push( { name: _ifo[ 0 ], file: _ifo[ 1 ], args: _ifo[ 2 ], } ); } } ); // 逐个创建 infos.forEach( _ifo => { this.modCreate( _ifo.name, _ifo.file, _ifo.args ); } ); }, // 销毁子MOD modDelete: function( _name ) { nx.dt.arrDelete( this.lstMods, _mod => { if( _mod && _mod._modKey == _name ) { nx.factory.remove( _mod ); nx.debug( "[Mod]销毁子MOD:", _name ); return true; } return false; }, true ); }, // 销毁子MOD modDeleteBatch: function( _list ) { // 空返回 if( nx.dt.arrEmpty( _list ) ) { return; } _list.forEach( _name => { this.modDelete( _name ); } ); }, // 清理子MOD modClean: function() { this.lstMods.forEach( _mod => { nx.factory.remove( _mod ); } ); this.lstMods = []; }, // 查询 modQuery: function( _name, _notify = true ) { for( let i in this.lstMods ) { let mod = this.lstMods[ i ]; if( mod && mod._modKey == _name ) { return mod; } } if( _notify ) { nx.error( "[Mod]查询子MOD失败:", _name || "无效名" ); } return null; }, // 模块列表输出 modDumpTree: function() { // 输出 let _dump = function( _mod, _hs = '' ) { console.log( _hs + _mod.nxUUID + ':' + _mod._modKey ); if( _mod.lstMods && _mod.lstMods.length > 0 ) { _mod.lstMods.forEach( mod => { _dump( mod, _hs + '\t' ); } ); } }; _dump( this ); }, } ); module.exports = MODBase;