HomeNotices.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { AppKernel } from '@scripts/app/AppKernel';
  2. import { INoticeData } from '@scripts/store/mainStore/data';
  3. import { formatMaskNickName, formatSCNum } from '@scripts/utils/Helper';
  4. import { _decorator, Component, Input, input, instantiate, KeyCode, Label, Node, NodePool, tween, Tween, UITransform, Vec3 } from 'cc';
  5. const { ccclass, property } = _decorator;
  6. const MIN_MESSAGE_GAP = 50;
  7. const SCROLL_SPEED = 120;
  8. interface INoticeNode {
  9. label: Label,
  10. trans: UITransform,
  11. }
  12. @ccclass('HomeNotices')
  13. export class HomeNotices extends Component {
  14. @property(Node)
  15. itemNode: Node = null;
  16. @property(UITransform)
  17. content: UITransform = null;
  18. private readonly pool = new NodePool('HomeNotices');
  19. private readonly activeItems: Node[] = [];
  20. private readonly noticeNodes = new WeakMap<Node, INoticeNode>();
  21. protected onLoad(): void {
  22. if (this.itemNode) this.itemNode.active = false;
  23. this.node.active = false;
  24. // input.on(Input.EventType.KEY_DOWN, (event) => {
  25. // if (event.keyCode === KeyCode.KEY_W) {
  26. // let data: INoticeData = {
  27. // nickName: `User${Math.floor(Math.random() * 8999 + 1000)}`,
  28. // value: Math.random() * 10000
  29. // }
  30. // const mainStore = AppKernel.stores.mainStore;
  31. // mainStore['_noticesList'].push(data);
  32. // this.startNotify();
  33. // }
  34. // })
  35. }
  36. protected onDestroy(): void {
  37. for (let index = 0; index < this.activeItems.length; index += 1) {
  38. Tween.stopAllByTarget(this.activeItems[index]);
  39. }
  40. this.activeItems.length = 0;
  41. }
  42. startNotify(): void {
  43. if (this.activeItems.length > 0) {
  44. const last = this.activeItems[this.activeItems.length - 1];
  45. if (last.x >= this.content.width) return;
  46. }
  47. const mainStore = AppKernel.stores.mainStore;
  48. const data = mainStore.getNoticeData();
  49. if (!data) {
  50. // 队列已空且没有正在移动的消息时,隐藏整个公告区域。
  51. if (this.activeItems.length === 0) this.node.active = false;
  52. return;
  53. }
  54. this.node.active = true;
  55. this.onNotify(data);
  56. }
  57. onNotify(data: INoticeData): void {
  58. if (!this.itemNode || !this.content) return;
  59. const msg = `${formatMaskNickName(data.nickName)} won SC ${formatSCNum(data.value)}`;
  60. const item = this.pool.get() || instantiate(this.itemNode);
  61. const noticeNode = this.getNoticeNode(item);
  62. const lblNotices = noticeNode.label;
  63. const itemTransform = noticeNode.trans;
  64. Tween.stopAllByTarget(item);
  65. item.setParent(this.content.node);
  66. item.active = true;
  67. lblNotices.string = msg;
  68. lblNotices.updateRenderData(true);
  69. const itemWidth = itemTransform.width * Math.abs(item.scale.x);
  70. if (itemWidth <= 0) {
  71. this.recycle(item);
  72. return;
  73. }
  74. const startX = this.getStartX(this.content.width);
  75. const endX = -itemWidth;
  76. item.setPosition(startX, 0, item.position.z);
  77. this.activeItems.push(item);
  78. tween(item)
  79. .to((startX - endX) / SCROLL_SPEED, {
  80. position: new Vec3(endX, 0, item.position.z),
  81. }, { easing: 'linear' })
  82. .call(() => {
  83. this.recycle(item);
  84. this.startNotify();
  85. })
  86. .start();
  87. }
  88. private getNoticeNode(item: Node) {
  89. let notices = this.noticeNodes.get(item);
  90. if (!notices) {
  91. notices = {
  92. label: item.getComponent(Label),
  93. trans: item.getComponent(UITransform),
  94. }
  95. this.noticeNodes.set(item, notices);
  96. }
  97. return notices;
  98. }
  99. private getStartX(contentWidth: number): number {
  100. let startX = contentWidth;
  101. for (let index = 0; index < this.activeItems.length; index += 1) {
  102. const item = this.activeItems[index];
  103. if (!item.isValid) continue;
  104. const transform = this.noticeNodes.get(item)?.trans;
  105. if (!transform) continue;
  106. const rightX = item.position.x + transform.width * Math.abs(item.scale.x);
  107. startX = Math.max(startX, rightX + MIN_MESSAGE_GAP);
  108. }
  109. return startX;
  110. }
  111. private recycle(item: Node): void {
  112. Tween.stopAllByTarget(item);
  113. const index = this.activeItems.indexOf(item);
  114. if (index !== -1) this.activeItems.splice(index, 1);
  115. if (!item.isValid) return;
  116. item.active = false;
  117. item.removeFromParent();
  118. this.pool.put(item);
  119. }
  120. }