/****************************************************************** * Copyright(C) 2019 - 2020 Nx Studio * * 视图绑定组件--战斗相机 * * 2018.05.18 ******************************************************************/ cc.Class( { extends: cc.Component, properties: { camera: { default: null, type: cc.Camera, displayName: "战斗相机" }, }, // 显示 onEnable: function() { nx.bridge.camBattle = this; }, // 隐藏 onDisable: function() { nx.bridge.camBattle = null }, // 归位重置 reset: function( _secs = 0, _cb = nx.dt.fnEmpty ) { this.moveTo( cc.Vec2.ZERO, _secs, _cb ); this.zoomTo( 1, _secs ); }, // ------------------------------------------------------------------------ // 移动&&聚焦 // ------------------------------------------------------------------------ // 计算多目标中心 getCenter: function( _tars ) { if( nx.dt.arrEmpty( _tars ) ) { return cc.Vec2.ZERO; } let parent = this.camera.node.parent; let local = function( _node ) { let pos = _node.convertToWorldSpaceAR( cc.Vec2.ZERO ); return parent.convertToNodeSpaceAR( pos ); }; let center = local( _tars[ 0 ] ); if( _tars.length == 1 ) { return center; } for( let i = 1; i < _tars.length; ++i ) { let t2 = _tars[ i ]; if( !t2 ) { continue; } let p2 = local( t2 ); center.x = ( center.x + p2.x ) / 2; center.y = ( center.y + p2.y ) / 2; } return center; }, // 多点目标中心聚焦移动 focusMut: function( _tars, _secs, _offset = cc.Vec2.ZERO, _cb = nx.dt.fnEmpty ) { if( nx.dt.arrEmpty( _tars ) ) { nx.error( `$CameraBattle:聚焦目标为空!` ); return this; } if( !nx.dt.numPositive( _secs, true ) ) { nx.error( `$CameraBattle:聚焦参数不全!` ); return this; } // 计算中心点 let pos = this.getCenter( _tars ); pos.x += _offset.x; pos.y += _offset.y; return this.moveTo( pos, _secs, _cb ); }, // 聚焦移动 focus: function( _tar, _secs, _offset = cc.Vec2.ZERO, _cb = nx.dt.fnEmpty ) { if( nx.dt.objEmpty( _tar ) ) { nx.error( `$CameraBattle:聚焦目标为空!` ); return this; } return this.focusMut( [ _tar ], _secs, _offset, _cb ); }, // 位置移动 moveTo: function( _pos, _secs, _cb = nx.dt.fnEmpty, _key = 'cubicOut' ) { // 瞬间 if( !nx.dt.numPositive( _secs, false ) ) { this.camera.node.position = _pos; nx.dt.fnInvoke( _cb ); return this; } // 移动 nx.tween.moveToEasing( this.camera, "", _secs, _pos, _key, () => { nx.dt.fnInvoke( _cb ); } ); return this; }, // ------------------------------------------------------------------------ // 视野缩放 // ------------------------------------------------------------------------ // 缩放 zoomTo: function( _ratio, _secs, _cb = nx.dt.fnEmpty ) { this.zooming = false; // 无效缩水 if( !nx.dt.numPositive( _ratio, false ) || Math.abs( _ratio - this.camera.zoomRatio ) <= 0.01 ) { nx.dt.fnInvoke( _cb ); return; } // 瞬间 if( !nx.dt.numPositive( _secs, false ) ) { this.camera.zoomRatio = _ratio; nx.dt.fnInvoke( _cb ); return; } // 缩放参数 this.ratio = _ratio; this.rps = ( _ratio - this.camera.zoomRatio ) / _secs; this.zooming = true; this.cbZoom = _cb; }, // 更新 update: function( _dt ) { if( !this.zooming ) { return; } this.camera.zoomRatio += this.rps * _dt; // 缩停止 if( this.rps < 0 && this.camera.zoomRatio <= this.ratio ) { this.camera.zoomRatio = this.ratio; this.zooming = false; nx.dt.fnInvoke( this.cbZoom ); this.cbZoom = null; return; } // 扩停止 if( this.rps >= 0 && this.camera.zoomRatio >= this.ratio ) { this.camera.zoomRatio = this.ratio; this.zooming = false; nx.dt.fnInvoke( this.cbZoom ); this.cbZoom = null; return; } }, // ------------------------------------------------------------------------ // 相机抖动 // ------------------------------------------------------------------------ shake: function( _level, _secs, _cb = nx.dt.fnEmpty ) { let ofs = cc.v2( 10, 10 ); switch( _level ) { case 1: ofs = cc.v2( 20, 20 ); break; case 2: ofs = cc.v2( 30, 30 ); break; case 3: ofs = cc.v2( 40, 40 ); break; case 4: ofs = cc.v2( 50, 50 ); break; default: break; } nx.tween.shake( this.camera, "", _secs, ofs, _cb ); }, } );