TauntComponent.ts 3.3 KB

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