WaitArea.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { _decorator, Component, Node, tween, Vec3 } from 'cc';
  2. const { ccclass, property, executeInEditMode } = _decorator;
  3. @ccclass('WaitArea')
  4. @executeInEditMode
  5. export class WaitArea extends Component {
  6. @property(Node)
  7. waitNodes: Node = null!; // 实际存放杯子的节点
  8. // 直接使用waitNodes的子节点管理
  9. get cups(): Node[] {
  10. // 反转数组实现从右到左排列(最右杯子在数组末尾)
  11. return this.waitNodes.children.slice().reverse();
  12. }
  13. start() {
  14. }
  15. update(deltaTime: number) {
  16. }
  17. // 从右向左排列
  18. arrangeCups() {
  19. const startX = -50;
  20. const spacing = -110;
  21. this.cups.forEach((cup, index) => {
  22. const originalIndex = this.waitNodes.children.indexOf(cup);
  23. cup.setPosition(startX + originalIndex * spacing, 0, 0);
  24. });
  25. // console.log('WaitArea 杯子数量: ', this.cups.length);
  26. }
  27. addCup(cup: Node) {
  28. cup.setParent(this.waitNodes);
  29. this.arrangeCups();
  30. }
  31. takeCup(): Node | null {
  32. if (this.cups.length === 0) return null;
  33. const cup = this.cups.pop()!;
  34. // 仅解除父节点关系,不立即销毁
  35. cup.parent = null;
  36. this.arrangeCups();
  37. return cup;
  38. }
  39. // 添加明确的返回类型
  40. getCups(): Node[] {
  41. return this.waitNodes.children;
  42. }
  43. }