import { _decorator, Component, Node, tween, Vec3 } from 'cc'; import { EventDispatcher } from 'db://assets/core_tgx/easy_ui_framework/EventDispatcher'; import { GameEvent } from '../Enum/GameEvent'; const { ccclass, property } = _decorator; @ccclass('AimTargetComponent') export class AimTargetComponent extends Component { private _originalPos: Vec3 = new Vec3(); private _isShaking = false; protected onLoad(): void { EventDispatcher.instance.on(GameEvent.EVENT_PLAY_GUN_ANIMATION,this.shakeEffect,this); } start() { Vec3.copy(this._originalPos, this.node.position); } //抖动效果 shakeEffect() { if (this._isShaking) return; this._isShaking = true; const shakeDistance = 20; // 抖动幅度 const duration = 0.4; // 总持续时间0.5秒 const shakeTimes = 4; // 抖动次数 // 创建随机方向抖动 const getRandomOffset = () => (Math.random() * 2 - 1) * shakeDistance; const shakeTween = tween(this.node); for (let i = 0; i < shakeTimes; i++) { shakeTween.to(duration/shakeTimes, { position: new Vec3( this._originalPos.x + getRandomOffset(), this._originalPos.y + getRandomOffset(), this._originalPos.z ) }); } // 最后回到原点 shakeTween.to(duration/shakeTimes, { position: this._originalPos }) .call(() => { this._isShaking = false; EventDispatcher.instance.emit(GameEvent.EVENT_CAMERA_HIDE_AIM); }) .start(); } update(deltaTime: number) { } }