TempCups.ts 2.6 KB

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