/****************************************************************** * Copyright(C) 2019 - 2020 Nx Studio * * 数据绑定 --- 道具图标+数量模板 * * ******************************************************************/ const BridgeComponent = require( "bridge.component" ); const RoleController = require( "role_controller" ); const BackPackController = require( "backpack_controller" ); const BridgeJumper = require( "bridge.jumper" ); cc.Class( { extends: BridgeComponent, properties: { itemId: { default: 0, displayName: "道具编号", }, ndIcon: { default: null, type: cc.Node, displayName: "图标", }, ndCount: { default: null, type: cc.Node, displayName: "数量", }, useSmall: { default: true, displayName: "使用小图标", }, shortMoney: { default: false, displayName: "货币简化支持", }, autoJump: { default: true, displayName: "自动跳转", }, }, // 编辑器特性 editor: { // 允许当前组件在编辑器模式下运行 executeInEditMode: false, // 当本组件添加到节点上后,禁止同类型(含子类)的组件再添加到同一个节点,防止逻辑发生冲突 disallowMultiple: true, // menu 用来将当前组件添加到组件菜单中,方便用户查找 menu: "老版桥接/资产道具信息", }, // 载入 onLoad: function() { this._super(); // 初始刷新 if( nx.dt.numPositive( this.itemId, false ) && !nx.dt.fnGood( this.cbChangeNotify ) ) { this.setID( this.itemId ); } // 开关点击 this.openTouch( this.autoJump ); }, // 销毁 onDestroy: function() { // 开关点击 this.openTouch( false ); this.setID( 0 ); this._super(); }, // 设置新ID setID: function( _id, _cbChange = null ) { // 改变通告 this.cbChangeNotify = _cbChange; // 释放 if( this.itemId > 0 ) { this.itemId = 0; this.unbindGEvents(); if( this.binder && this.handler ) { this.binder.unbind( this.handler ); this.handler = null; this.binder = null; } } if( !window.nx ){ return; } // 新绑定 this.itemId = parseInt( _id ); this.config = gitemdata( this.itemId ); // 无效绑定 if( nx.dt.objEmpty( this.config ) ) { this.itemId = 0; this.config = null; // nx.gui.setSpriteFrame( this.ndIcon, "", null ); // nx.gui.setString( this.ndCount, "", "" ); return; } // 图标 if( this.ndIcon ) { if( this.useSmall ) { nx.bridge.setIconS( this.ndIcon, "", this.config.icon ); } else { nx.bridge.setIcon( this.ndIcon, "", this.config.icon ); } } // 辨别绑定方式 this.bindPK = ""; let key = game.configs.item_data.data_assets_id2label[ this.itemId ]; if( nx.dt.strNEmpty( key ) ) { this.bindPK = key; } // 分别绑定 if( nx.dt.strEmpty( this.bindPK ) ) { this.bindBagItem(); } else { this.bindRoleProp(); } }, // 绑定角色属性 bindRoleProp: function() { this.binder = RoleController.getInstance().getRoleVo(); if( !this.binder ) { nx.error( "[Binder]当前无主角,属性绑定失败!" ); return; } let eid = EventId.UPDATE_ROLE_ATTRIBUTE; this.handler = this.binder.bind( eid, this.onPropUpdate, this ); if( !this.handler ) { nx.error( "[Binder]属性监听失败!" ); return; } // 手动更新一次 this.onPropUpdate( this.bindPK, this.binder[ this.bindPK ] ); }, // 绑定背包道具 bindBagItem: function() { this.unbindGEvents(); if( !nx.dt.numPositive( this.itemId, false ) ) { return; } // 背包监听 this.bindGEvent( EventId.ADD_GOODS, this.onBagItemUpdate.bind( this ) ); this.bindGEvent( EventId.DELETE_GOODS, this.onBagItemUpdate.bind( this ) ); this.bindGEvent( EventId.MODIFY_GOODS_NUM, this.onBagItemUpdate.bind( this ) ); // 手动更新一次 this.updateItemCount(); }, // 属性更新 onPropUpdate: function( _key, _value ) { if( _key != this.bindPK ) { return; } let val = '' + _value; if( this.shortMoney ) { val = nx.dt.shortCount( _value ); } nx.gui.setString( this.ndCount, "", val ); // 改变通告 nx.dt.fnInvoke( this.cbChangeNotify, _key, _value ); }, // 道具数量更新 updateItemCount: function() { const BC = BackPackController.getInstance(); let count = BC.getModel().getBackPackItemNumByBid( this.itemId ); let val = '' + count; if( this.shortMoney ) { val = nx.dt.shortCount( count ); } nx.gui.setString( this.ndCount, "", val ); // 改变通告 nx.dt.fnInvoke( this.cbChangeNotify, this.itemId, count ); }, // 道具更新 onBagItemUpdate: function( _bagCode, _itemList ) { for( let k in _itemList ) { let tm = _itemList[ k ]; if( tm && tm.base_id == this.itemId ) { this.updateItemCount(); break; } } }, // 获取跳转 onTouchJumpSource: function(event) { if(event.type == 'touchend') BridgeJumper.jumpGetItem( this.itemId ); }, // 开关点击 openTouch: function( _open = false ) { this.node.off( "touchend", this.onTouchJumpSource, this ); if( _open ) { this.node.on( "touchend", this.onTouchJumpSource, this ); } }, } );