TauntComponent.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { _decorator, Component, Label, Node, tween, UITransform, Vec3, view } from 'cc';
  2. import { UserManager } from '../Manager/UserMgr';
  3. import { EventDispatcher } from 'db://assets/core_tgx/easy_ui_framework/EventDispatcher';
  4. import { GameEvent } from '../Enum/GameEvent';
  5. import { AliensAudioMgr } from '../Manager/AliensAudioMgr';
  6. const { ccclass, property } = _decorator;
  7. //嘲讽文本
  8. const tauntTxt = [
  9. 'Your level is also too poor',
  10. 'You loser',
  11. 'It would have been over long ago if someone else came'
  12. ]
  13. @ccclass('TauntComponent')
  14. export class TauntComponent extends Component {
  15. @property(Label)
  16. tauntLabel: Label = null!;
  17. private _originPos:Vec3 = null!;
  18. private _isAnimating: boolean = false;
  19. //嘲讽时间间隔
  20. tauntIntervalTime: number = 0;
  21. start() {
  22. const tauntTime = UserManager.instance.userModel.tauntIntervalTime
  23. this.tauntIntervalTime = tauntTime;
  24. // this.tauntIntervalTime = 5;//测试
  25. this._originPos = this.node.position.clone();
  26. EventDispatcher.instance.on(GameEvent.EVENT_GAME_COUNTDOWN_START,this.startTauntSchedule,this)
  27. }
  28. private startTauntSchedule() {
  29. this.schedule(this.playTauntAnimation, this.tauntIntervalTime);
  30. }
  31. //嘲讽动画
  32. taunt() {
  33. AliensAudioMgr.playOneShot(AliensAudioMgr.getMusicIdName(6), 1.0);
  34. if(this._isAnimating) return;
  35. this._isAnimating = true;
  36. // 设置随机嘲讽文本
  37. this.tauntLabel.string = this.getRandomTauntTxt();
  38. // 计算屏幕左侧位置
  39. const screenLeft = -view.getVisibleSize().width / 2;
  40. const width = this.node.getComponent(UITransform).width;
  41. const targetPos = new Vec3(screenLeft + width / 2, this._originPos.y, this._originPos.z);
  42. // 动画到屏幕左侧(1秒)
  43. tween(this.node.position)
  44. .to(1, targetPos, {
  45. easing: 'quadOut',
  46. onUpdate: (target: Vec3) => {
  47. this.node.position = target;
  48. }
  49. })
  50. .call(() => {
  51. // 停留1秒后再执行返回动画
  52. this.scheduleOnce(() => {
  53. tween(this.node.position)
  54. .to(2, this._originPos, {
  55. easing: 'quadIn',
  56. onUpdate: (target: Vec3) => {
  57. this.node.position = target;
  58. },
  59. onComplete: () => {
  60. this._isAnimating = false;
  61. }
  62. })
  63. .start();
  64. }, 1);
  65. })
  66. .start();
  67. }
  68. // 定时播放嘲讽动画
  69. private playTauntAnimation() {
  70. this.taunt();
  71. }
  72. //随机获取嘲讽文本
  73. getRandomTauntTxt() {
  74. const index = Math.floor(Math.random() * tauntTxt.length);
  75. return tauntTxt[index];
  76. }
  77. protected onDestroy(): void {
  78. this.unschedule(this.playTauntAnimation);
  79. EventDispatcher.instance.off(GameEvent.EVENT_GAME_COUNTDOWN_START,this.startTauntSchedule,this)
  80. }
  81. }