/****************************************************************** * * 大富翁场景 * ******************************************************************/ const BridgeWindow = require( "bridge.window" ); const Mono = require( "monopoly.mod" ); const MonoRole = require( "cmp.monopoly.role" ); const TipsController = require( "tips_controller" ); const RoleController = require( "role_controller" ); const BackPackController = require( "backpack_controller" ); const FxSpine = require( "nx.fx.spine" ); const MainuiController = require( "mainui_controller" ); const MAX_DICE = 6; // 最大步长 const LEN_PATH = 30; // 路径长度 cc.Class( { extends: BridgeWindow, properties: { mapRefs: { default: null, type: cc.Node }, mapRewards: { default: null, type: cc.Node }, mapWalkable: { default: null, type: cc.Node }, nodAuto: { default: null, type: cc.Node }, nodDate: { default: null, type: cc.Node }, nodStart: { default: null, type: cc.Node }, nodRound: { default: null, type: cc.Node }, lstRewards: { default: null, type: cc.Node }, spDice: { default: null, type: FxSpine }, cmpRole: { default: null, type: MonoRole }, auDice: { default: null, type: cc.AudioClip }, auFoot: { default: null, type: cc.AudioClip }, secAuto: { default: 3 }, }, // 显示 onEnable: function() { // 过程归零 this.doing = false; this.footHandle = 0; nx.gui.setActive( this.spDice, "", false ); // 骰子货币 let DATA = game.configs.monopoly_data.data_const.item_bid; this.coinId = DATA.val; // 地图重建 this.rebuildMap(); // 冒险角色 let role = RoleController.getInstance().getRoleVo(); let cfgs = game.configs.looks_data.data_data[role.look_id]; this.cmpRole.setModel( cfgs ? cfgs.model : "", ( _key ) => { this.roleEvent( _key ); } ); // 奖励格子 this.buildRewardGrids(); // 初始化列表 this.initRoundRewards(); // 视图绑定 nx.bridge.vbind( this, [ [ "MonoRewards", this.onFreshRoundRewards.bind( this ) ] ] ); }, // 关闭 onDisable: function() { this.unscheduleAllCallbacks(); // 视图监听解除 nx.bridge.vunbind( this ); }, // 数据重置 rebuildMap: function() { // 初始位置 let cur = nx.bridge.vget( "MonoPos" ); let pos = this.queryPosition( cur ); if( pos ) { this.cmpRole.node.position = pos; } nx.gui.setActive( this.nodDate, "", false ); // 结束时间 this.unscheduleAllCallbacks(); this.schedule( () => { let time = nx.bridge.vget( "MonoEndTime" ); let txt = nx.bridge.time.toNeedTime( time ); nx.gui.setString( this.nodDate, "txt", txt ); } ) }, // 奖励格子 buildRewardGrids: function() { // 格子奖励 let items = []; let DATA = game.configs.monopoly_data.data_get_reward; for( let i in DATA ) { let ifo = DATA[ i ]; if( ifo && ifo.item_bid > 0 && ifo.num > 0 ) { items.push( ifo ); } } if( items.length > 0 ) { let chds = this.mapRewards.children; nx.gui.gocChildren( this.mapRewards, "", items.length, chds[ 0 ] ); for( let i = 0; i < chds.length; ++i ) { let node = chds[ i ]; let info = items[ i ]; node.position = this.queryPosition( info.pos ); node.info = info; let cfg = gitemdata( info.item_bid ); nx.bridge.setIcon( node, "icon", cfg ? cfg.icon : "" ); let cmp = nx.gui.getComponent( node, "icon", "nx.actor.suspending", true ); if( cmp ) { cmp.start( 2, 10 ); } } } }, // ============================================================ // 扔骰子相关 // ============================================================ // 点击投掷 onTouchStart: function() { // 进行中 if( this.doing ) { nx.tbox( "MonoDoing" ); return; } let model = BackPackController.getInstance().getModel(); let count = model.getBackPackItemNumByBid( this.coinId ); if( count < 1 ) { nx.tbox( "SummonItemNotEnough" ); return; } this.doing = true; Mono.getInstance().reqRoll( ( _ret, _data ) => { if( !_ret ) { this.doing = false; nx.tbox( _data ); return; } // 本地道具-1 nx.gui.setString( this.nodStart, "need/num", count - 1 ); // 过程模拟 this.simProgress( _data.num, _data.next_pos ); } ) }, // 投掷过程模拟 simProgress: function( _dice, _pos ) { let self = this; // 移动 let move = function() { let pts = []; let well = false; let cur = nx.bridge.vget( "MonoPos" ); for( let i = 0; i < MAX_DICE; ++i ) { let pos = cur + i + 1; if( pos > LEN_PATH ) { pos = pos % LEN_PATH; } pts.push( self.queryPosition( pos ) ); if( pos == _pos ) { well = true; break; } } // 出错 if( !well ) { nx.error( "Mono:路径模拟失败,无效坐标: %s -> %s", cur, _pos ); self.doing = false; return; } self.cmpRole.pushPaths( pts ); }; // 扔骰子 nx.gui.setActive( this.spDice, "", true ); this.spDice.action( "ret" + _dice, false, ( _event ) => { if( _event == "complete" ) { move(); nx.gui.setActive( this.spDice, "", false ); } } ); // 投掷音效 if( this.auDice ) { cc.audioEngine.playEffect( this.auDice, false ); } this.curDice = _dice; }, // 角色事件 roleEvent: function( _key ) { // 音效关闭 if( this.footHandle > 0 ) { cc.audioEngine.stopEffect( this.footHandle ); this.footHandle = 0; } // 开始 if( _key == "start" ) { if( this.auFoot ) { this.footHandle = cc.audioEngine.playEffect( this.auFoot, true ); } return; } // 结束 let self = this; let success = function( _rewards = [] ) { let lst = []; for( let i = 0; i < _rewards.length; ++i ) { let rt = _rewards[ i ]; if( rt ) { lst.push( { bid: rt.item_bid, num: rt.num } ); } } let open = nx.bridge.vget( "MonoAutoDice" ); let done = function() { if( open ) { self.onTouchStart(); } }; // 无奖励直接判断自动投掷 if( nx.dt.arrEmpty( lst ) ) { done(); return; } let args = {}; if( open ) { args.delay = { secs: self.secAuto, cb: () => { done(); } } } let MC = MainuiController.getInstance(); MC.openGetItemView( true, lst, args ); }; let failed = function() { // 角色归位 let cur = nx.bridge.vget( "MonoPos" ); let pos = self.queryPosition( cur ); if( pos ) { self.cmpRole.node.position = pos; } // 道具数量归位 let count = model.getBackPackItemNumByBid( self.coinId ); nx.gui.setString( self.nodStart, "need/num", count ); }; Mono.getInstance().reqRollMoveEnd( this.curDice, ( _ret, _data ) => { this.doing = false; if( _ret ) { success( _data.reward_list ); return; } // 移动失败 nx.tbox( _data ); failed(); } ); }, // 点击格子奖励 onTouchGridGift: function( _node ) { let info = _node.currentTarget.info; if( info && info.item_bid > 0 ) { const TC = TipsController.getInstance(); if( TC ) { TC.showItemTips( info.item_bid ); } } }, // ============================================================ // 轮次礼物相关 // ============================================================ // 初始化列表 initRoundRewards: function() { let chds = this.lstRewards.children; let cfg = game.configs.monopoly_data.data_get_schedule_reward; for( let i = 0; i < chds.length; ++i ) { let node = chds[ i ]; node.index = ( i + 1 ); let info = cfg[ i + 1 ]; let cmp = nx.gui.getComponent( node, "rewards/nod", "cmp.item.layout" ); if( cmp ){ cmp.rebuild( info.rewards ); } nx.gui.setString( node, "txt", nx.text.format( "MonoRoundF", i + 1 ) ); nx.gui.setActive( node, "on", false ); nx.gui.setActive( node, "got", false ); nx.gui.setActive( node, "rewards", false ); nx.gui.setActive( node, "off", true ); } }, // 刷新列表 onFreshRoundRewards: function() { let rewards = nx.bridge.vget( "MonoRewards" ); if( nx.dt.arrEmpty( rewards ) ) { return; } for( let i in rewards ) { let info = rewards[ i ]; let node = nx.gui.find( this.lstRewards, "item" + info.turn ); if( node ) { nx.gui.setActive( node, "on", info.is_get_reward == 0 ); nx.gui.setActive( node, "touch", info.is_get_reward != 0 ); nx.gui.setActive( node, "got", info.is_get_reward == 1 ); } } }, // 查看圈数 奖励 onLookRoundRewards: function( _idx ) { let nodhide = nx.gui.find( this, "ui/hideitems" ); for (let i = 0; i < this.lstRewards.children.length; i++) { let nod = this.lstRewards.children[i]; let nodReward = nx.gui.find( nod, "rewards" ); if( i != _idx ){ nx.gui.setActive( nodReward, "", false ); } } let nod = nx.gui.find( this.lstRewards.children[_idx], "rewards" ); // let cfg = game.configs.monopoly_data.data_get_schedule_reward; nx.gui.setActive( this.lstRewards.children[_idx], "rewards", !nod.active ); nx.gui.setActive( nodhide, "", nod.active == true ); }, onTouchHideAllDesc: function(){ let nodhide = nx.gui.find( this, "ui/hideitems" ); for (let i = 0; i < this.lstRewards.children.length; i++) { let nod = this.lstRewards.children[i]; let nodReward = nx.gui.find( nod, "rewards" ); nx.gui.setActive( nodReward, "", false ); } nx.gui.setActive( nodhide, "", false ); }, // 点击礼物 onTouchReward: function( _item ) { let idx = _item ? _item.parent.index : 0; if( idx < 1 ) { return; } Mono.getInstance().reqReward( idx, ( _ret, _data ) => { if( !_ret ) { nx.tbox( _data ); return; } } ); }, // 点击购买骰子 onTouchBuyDice: function() { let num = nx.bridge.vget( "MonoBuyNum" ); if( num <= 0 ) { nx.tbox( "MonoBuyNoTime" ); return; } let total = game.configs.monopoly_data.data_const.buy_time.val; let prices = game.configs.monopoly_data.data_const.item_price.val; let times = total - num; if( times < 0 || times >= prices.length ) { nx.tbox( "MonoBuyNoTime" ); return; } // 购买 let buy = function() { Mono.getInstance().reqBuyDice( 1, ( _ret, _data ) => { if( !_ret ) { nx.tbox( _data ); return; } } ) }; let price = prices[ times ]; nx.bridge.createPanel( "WndItemBuy", { item: { bid: this.coinId, num: 1 }, price: { bid: price[ 0 ], num: price[ 1 ] }, cb: ( _ret ) => { if( _ret ) { buy(); } } } ); }, // 自动投掷切换 onTouchAutoTog: function() { Mono.getInstance().togAutoRoll(); }, // 帮助 onTouchHelp: function() { let DATA = game.configs.monopoly_data.data_const.monopoly_rule; if( DATA ) { const TC = TipsController.getInstance(); TC.showTextPanel( "tip", nx.text.getKey( DATA.desc ) ); } }, // 获取指定格子坐标 queryPosition: function( _gpos ) { let node = nx.gui.find( this.mapRefs, "p" + _gpos ); if( !node ) { nx.error( "Mono:无效位置 ", _gpos ); return null; } return node.position; }, } );