WaitArea.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { _decorator, Component, Node } 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() {
  10. return this.waitNodes.children;
  11. }
  12. start() {
  13. }
  14. update(deltaTime: number) {
  15. }
  16. // 从右向左排列
  17. arrangeCups() {
  18. const startX = -40;
  19. const spacing = -150;
  20. this.cups.forEach((cup, index) => {
  21. cup.setPosition(startX + index * spacing, 0, 0);
  22. });
  23. // console.log('WaitArea 杯子数量: ', this.cups.length);
  24. }
  25. addCup(cup: Node) {
  26. cup.setParent(this.waitNodes);
  27. this.arrangeCups();
  28. }
  29. takeCup(): Node | null {
  30. if (this.cups.length === 0) return null;
  31. const cup = this.cups.pop()!;
  32. cup.removeFromParent();
  33. this.arrangeCups();
  34. return cup;
  35. }
  36. }