import { _decorator, Component, dragonBones, v3 } from 'cc'; import { DBData } from './DBData'; const { ccclass, requireComponent, property } = _decorator; /** * @description 龙骨动画渲染器 * @author 王锡铜、袁浩 */ @ccclass('DBRender') @requireComponent(DBData) export class DBRender extends Component { @property({ tooltip: "动态骨骼名称" }) public dynamicBoneName: string = "chest"; @property({ tooltip: "淡入时间(秒)" }) public fadeInTime: number = 0.05; @property({ tooltip: "播放次数。 [-1: 使用动画数据默认值, 0: 无限循环播放, [1~N]: 循环播放 N 次]" }) public playTimes: number = 0; private DBDisplay: dragonBones.ArmatureDisplay; private armature: dragonBones.Armature; private dbData: DBData; private _nowAnimation: string = ""; /**可能需要动态变化的骨骼(例如头部需要看向敌人) */ private _dynamicBone: dragonBones.Bone = null; onLoad() { this.DBDisplay = this.getComponent(dragonBones.ArmatureDisplay); if (!this.DBDisplay) { console.error(this.node.name + "引用了DBRender组价,但是没有找到dragonBones.ArmatureDisplay组件!!by:DBRender"); return; } this.armature = this.DBDisplay.armature(); this.dbData = this.getComponent(DBData); this._dynamicBone = this.armature.getBone(this.dynamicBoneName); // let replaceSolt = this.armature.getSlot("weapon_l"); // let targetSlotName = "weapon_r"; // let displayName = this.replaceArmatureDisplay.armature().getSlot(targetSlotName).display.displayData.name; // let factory = dragonBones.CCFactory.getInstance(); // factory.replaceSlotDisplay(this.replaceArmatureDisplay.getArmatureKey(), this.replaceArmatureDisplay.armatureName, targetSlotName, displayName, replaceSolt); } update() { if (this._nowAnimation != this.dbData.curAnimation) { this._nowAnimation = this.dbData.curAnimation; this.armature.animation.fadeIn(this._nowAnimation, this.fadeInTime, this.playTimes); } if (this.node.scale.x != this.dbData.direction) this.node.scale = v3(this.dbData.direction, this.node.scale.y, this.node.scale.z); (this.dbData.targetPos && this._dynamicBone) && this.lookAt(); } private lookAt(): void { let radian = 0; let offsetY = this._dynamicBone.global.y * this.node.getScale().y; if (this.dbData.direction > 0) { radian = Math.atan2(this.dbData.targetPos.y - this.node.position.y - offsetY, this.dbData.targetPos.x - this.node.position.x); } else { radian = Math.PI - Math.atan2(this.dbData.targetPos.y - this.node.position.y - offsetY, this.dbData.targetPos.x - this.node.position.x); if (radian > Math.PI) { radian -= Math.PI * 2; } } this._dynamicBone.offset.rotation = radian; } }