WaitArea.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 = -40;
  20. const spacing = -80;
  21. this.cups.forEach((cup, index) => {
  22. // 注意这里使用原始子节点顺序排列
  23. const originalIndex = this.waitNodes.children.indexOf(cup);
  24. cup.setPosition(startX + originalIndex * spacing, 0, 0);
  25. });
  26. // console.log('WaitArea 杯子数量: ', this.cups.length);
  27. }
  28. addCup(cup: Node) {
  29. cup.setParent(this.waitNodes);
  30. this.arrangeCups();
  31. }
  32. takeCup(): Node | null {
  33. if (this.cups.length === 0) return null;
  34. const cup = this.cups.pop()!;
  35. // 仅解除父节点关系,不立即销毁
  36. cup.parent = null;
  37. this.arrangeCups();
  38. return cup;
  39. }
  40. // 添加明确的返回类型
  41. getCups(): Node[] {
  42. return this.waitNodes.children;
  43. }
  44. }