AimTargetComponent.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { _decorator, Component, Node, tween, Vec3 } from 'cc';
  2. import { EventDispatcher } from 'db://assets/core_tgx/easy_ui_framework/EventDispatcher';
  3. import { GameEvent } from '../Enum/GameEvent';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('AimTargetComponent')
  6. export class AimTargetComponent extends Component {
  7. private _originalPos: Vec3 = new Vec3();
  8. private _isShaking = false;
  9. protected onLoad(): void {
  10. EventDispatcher.instance.on(GameEvent.EVENT_PLAY_GUN_ANIMATION,this.shakeEffect,this);
  11. }
  12. start() {
  13. Vec3.copy(this._originalPos, this.node.position);
  14. }
  15. //抖动效果
  16. shakeEffect() {
  17. if (this._isShaking) return;
  18. this._isShaking = true;
  19. const shakeDistance = 20; // 抖动幅度
  20. const duration = 0.4; // 总持续时间0.5秒
  21. const shakeTimes = 4; // 抖动次数
  22. // 创建随机方向抖动
  23. const getRandomOffset = () => (Math.random() * 2 - 1) * shakeDistance;
  24. const shakeTween = tween(this.node);
  25. for (let i = 0; i < shakeTimes; i++) {
  26. shakeTween.to(duration/shakeTimes, {
  27. position: new Vec3(
  28. this._originalPos.x + getRandomOffset(),
  29. this._originalPos.y + getRandomOffset(),
  30. this._originalPos.z
  31. )
  32. });
  33. }
  34. // 最后回到原点
  35. shakeTween.to(duration/shakeTimes, { position: this._originalPos })
  36. .call(() => {
  37. this._isShaking = false;
  38. EventDispatcher.instance.emit(GameEvent.EVENT_CAMERA_HIDE_AIM);
  39. })
  40. .start();
  41. }
  42. update(deltaTime: number) {
  43. }
  44. }