TempCups.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { _decorator, Component, Node, tween, UITransform, Vec3, view } from 'cc';
  2. import { TempCup } from './TempCup';
  3. import { GameEvent } from '../Enum/GameEvent';
  4. import { EventDispatcher } from 'db://assets/core_tgx/easy_ui_framework/EventDispatcher';
  5. import { tgxUITips, tgxUIWaiting } from 'db://assets/core_tgx/tgx';
  6. const { ccclass, property } = _decorator;
  7. @ccclass('TempCups')
  8. export class TempCups extends Component {
  9. start() {
  10. this.registerEvent();
  11. }
  12. protected onDestroy(): void {
  13. EventDispatcher.instance.off(GameEvent.EVENT_MOVE_OUT, this.onMoveOut, this);
  14. }
  15. private registerEvent(): void {
  16. EventDispatcher.instance.on(GameEvent.EVENT_MOVE_OUT, this.onMoveOut, this);
  17. }
  18. private async onMoveOut(): Promise<void> {
  19. console.log('执行移出操作');
  20. // 获取前三个子节点(最左侧的三个杯子)
  21. const cupsToProcess = this.node.children.slice(0, 3)
  22. .map(node => node.getComponent(TempCup))
  23. .filter(cup => cup && cup.isFull);
  24. if (cupsToProcess.length === 0) {
  25. tgxUITips.show('There are no removable cups!');
  26. return;
  27. }
  28. // 并行执行所有动画
  29. const tasks = cupsToProcess.map(cup => {
  30. return new Promise<void>((resolve) => {
  31. // 保存原始位置
  32. const originalPos = cup.node.position.clone();
  33. const screenWidth = view.getVisibleSize().width;
  34. const targetX = -screenWidth - cup.node.getComponent(UITransform)!.width * 1.5;
  35. const targetY = cup.node.position.y + 200;
  36. // 创建移动动画
  37. tween(cup.node)
  38. .to(0.5, { position: new Vec3(targetX, targetY, 0) }, { easing: 'sineIn' })
  39. .call(() => {
  40. cup.node.position = originalPos; // 将位置重置到动画前保存的原始位置
  41. cup.reset(); // 重置水杯状态
  42. resolve();
  43. })
  44. .start();
  45. });
  46. });
  47. await Promise.all(tasks);
  48. console.log('所有暂存杯处理完成');
  49. }
  50. /** 查找暂存区空杯*/
  51. findAvailableTempCup(): TempCup | null {
  52. // 添加空值检查和类型过滤
  53. return this.node.children
  54. .map(node => node.getComponent(TempCup))
  55. .find(cup => cup && !cup.isFull && !cup.iconAd) || null;
  56. }
  57. // 新增获取已填充的暂存杯
  58. public getFilledCups(): TempCup[] {
  59. return this.node.children
  60. .map(node => node.getComponent(TempCup)!)
  61. .filter(cup => !cup.isEmpty());
  62. }
  63. }