TauntComponent.ts 3.0 KB

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