"use strict"; cc._RF.push(module, '8218f2syWJLyZItvFq2KPr+', 'bridge.camera.battle'); // Scripts/zbridge/cmps/bridge.camera.battle.js "use strict"; /****************************************************************** * Copyright(C) 2019 - 2020 Nx Studio * * 视图绑定组件--战斗相机 * * 2018.05.18 ******************************************************************/ cc.Class({ "extends": cc.Component, properties: { camera: { "default": null, type: cc.Camera, displayName: "战斗相机" } }, // 显示 onEnable: function onEnable() { nx.bridge.camBattle = this; }, // 隐藏 onDisable: function onDisable() { nx.bridge.camBattle = null; }, // 归位重置 reset: function reset(_secs, _cb) { if (_secs === void 0) { _secs = 0; } if (_cb === void 0) { _cb = nx.dt.fnEmpty; } this.moveTo(cc.Vec2.ZERO, _secs, _cb); this.zoomTo(1, _secs); }, // ------------------------------------------------------------------------ // 移动&&聚焦 // ------------------------------------------------------------------------ // 计算多目标中心 getCenter: function getCenter(_tars) { if (nx.dt.arrEmpty(_tars)) { return cc.Vec2.ZERO; } var parent = this.camera.node.parent; var local = function local(_node) { var pos = _node.convertToWorldSpaceAR(cc.Vec2.ZERO); return parent.convertToNodeSpaceAR(pos); }; var center = local(_tars[0]); if (_tars.length == 1) { return center; } for (var i = 1; i < _tars.length; ++i) { var t2 = _tars[i]; if (!t2) { continue; } var p2 = local(t2); center.x = (center.x + p2.x) / 2; center.y = (center.y + p2.y) / 2; } return center; }, // 多点目标中心聚焦移动 focusMut: function focusMut(_tars, _secs, _offset, _cb) { if (_offset === void 0) { _offset = cc.Vec2.ZERO; } if (_cb === void 0) { _cb = nx.dt.fnEmpty; } if (nx.dt.arrEmpty(_tars)) { nx.error("$CameraBattle:\u805A\u7126\u76EE\u6807\u4E3A\u7A7A!"); return this; } if (!nx.dt.numPositive(_secs, true)) { nx.error("$CameraBattle:\u805A\u7126\u53C2\u6570\u4E0D\u5168!"); return this; } // 计算中心点 var pos = this.getCenter(_tars); pos.x += _offset.x; pos.y += _offset.y; return this.moveTo(pos, _secs, _cb); }, // 聚焦移动 focus: function focus(_tar, _secs, _offset, _cb) { if (_offset === void 0) { _offset = cc.Vec2.ZERO; } if (_cb === void 0) { _cb = nx.dt.fnEmpty; } if (nx.dt.objEmpty(_tar)) { nx.error("$CameraBattle:\u805A\u7126\u76EE\u6807\u4E3A\u7A7A!"); return this; } return this.focusMut([_tar], _secs, _offset, _cb); }, // 位置移动 moveTo: function moveTo(_pos, _secs, _cb, _key) { if (_cb === void 0) { _cb = nx.dt.fnEmpty; } if (_key === void 0) { _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, function () { nx.dt.fnInvoke(_cb); }); return this; }, // ------------------------------------------------------------------------ // 视野缩放 // ------------------------------------------------------------------------ // 缩放 zoomTo: function zoomTo(_ratio, _secs, _cb) { if (_cb === void 0) { _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 update(_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 shake(_level, _secs, _cb) { if (_cb === void 0) { _cb = nx.dt.fnEmpty; } var 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); } }); cc._RF.pop();