Files
2026-05-23 22:10:14 +08:00

183 lines
4.6 KiB
JavaScript

/******************************************************************
*
* 家园场景角色
*
******************************************************************/
const NxSpine = require( "nx.fx.spine" );
const HomeMod = require( "home.mod" );
cc.Class( {
extends: cc.Component,
properties: {
spRole: { default: null, type: NxSpine },
nodReward: { default: null, type: cc.Node },
},
// 载入
onLoad: function() {
// 调试关闭
nx.gui.setActive( this, "dbg", false );
nx.gui.setActive( this, "role/shadow", false );
// 模型放大50%
this.spRole.node.scale = 1.5;
this.nodReward.scale = 0.75;
},
// 重置
reset: function() {
this.memKey = "";
this.reward = null;
this.spRole.stop( true );
this.nodReward.active = false;
nx.gui.setActive( this, "role/shadow", false );
this.unscheduleAllCallbacks();
},
// 新设置
setMember: function( _info, _reward ) {
// 置空
if( nx.dt.objEmpty( _info ) ) {
this.reset();
return;
}
// 角色
let key = _info.partner_bid + "_" + _info.init_star;
if( this.memKey != key ) {
// 资源配置
let cfgs = gdata( "partner_data", "data_partner_star", key );
if( !cfgs ) {
nx.error( "无效家园成员:", key );
this.reset();
return;
}
// 动画
this.memKey = key;
let path = PathTool.getSpinePath( cfgs.res_id, cfgs.res_id, false );
this.spRole.load( path, ( _e ) => {
if( !_e ) {
this.spRole.action( "stand2", true );
nx.gui.setActive( this, "role/shadow", true );
this.readyMove();
} else {
this.spRole.stop( true );
}
} );
}
// 奖励
nx.gui.setActive( this.nodReward, "", false );
this.scheduleOnce( () => {
this.showReward( _reward );
}, Math.random() / 2 );
},
// 显示奖励
showReward: function( _reward ) {
// 已经有奖励了
if( nx.dt.objEmpty( _reward ) ) {
return;
}
this.reward = _reward;
nx.gui.setActive( this.nodReward, "", true );
let path = PathTool.querySmallIconPath( this.reward.icon );
nx.gui.setSpriteFrame( this.nodReward, "icon", path, ( _ret ) => {
if( !_ret ) {
path = PathTool.querySmallIconPath( 0 );
nx.gui.setSpriteFrame( this.nodReward, "icon", path );
}
} );
// 悬浮展示
let cmp = nx.gui.getComponent( this.nodReward, "", "nx.actor.suspending", true );
if( cmp ) {
cmp.start( 2, 10 );
}
},
// 点击奖励
onTouchReward: function() {
// 无奖励
if( nx.dt.objEmpty( this.reward ) ) {
return;
}
HomeMod.getInstance().reqGetReward( this.reward.reward_id, ( _ret, _data ) => {
this.reward = null;
nx.gui.setActive( this.nodReward, "", false );
if( !_ret ) {
nx.tbox( _data );
return;
}
} );
},
// ----------------------------------------------------------------------
// 原地移动
// ----------------------------------------------------------------------
// 准备移动
readyMove: function() {
let dbg = nx.gui.find( this, "dbg" );
if( !dbg ) {
return;
}
if( this.memKey == "" ) {
return;
}
if( !this.moveRange ) {
this.moveRange = cc.v2( -dbg.width / 2, dbg.width / 2 );
}
// 延迟随机移动
this.scheduleOnce( ()=> {
this.randomMove();
}, nx.dt.randomRange( 5, 15 ) );
},
// 随机移动
randomMove: function() {
let x = nx.dt.randomRange( this.moveRange.x, this.moveRange.y );
let cur = this.spRole.node.position;
let tar = cc.v2( x, 0 );
let secs = tar.sub(cur).len() / 100;
let scale = Math.abs( this.spRole.node.scaleX );
this.spRole.node.scaleX = scale * ( ( tar.x < cur.x ) ? -1 : 1 );
this.spRole.action( "float", true );
nx.tween.moveTo( this.spRole, "", secs, cc.v2( x, 0 ), () => {
this.spRole.action( "stand2", true );
this.readyMove();
});
},
} );