WaitArea.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { _decorator, Component, Node, tween, Vec3, view } 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. const designSize = view.getDesignResolutionSize();
  16. const visibleSize = view.getVisibleSize();
  17. // 计算高度的缩放比例
  18. const scaleY = visibleSize.height / designSize.height;
  19. const originalY = this.node.worldPosition.y;
  20. const designSizeY = designSize.height * (scaleY - 1);
  21. const visibleSizeY = visibleSize.height * (scaleY - 1);
  22. this.node.worldPosition = new Vec3(this.node.worldPosition.x, originalY + (visibleSizeY - designSizeY), 0);
  23. }
  24. update(deltaTime: number) {
  25. }
  26. // 从右向左排列
  27. arrangeCups() {
  28. const startX = -50;
  29. const spacing = -110;
  30. this.cups.forEach((cup, index) => {
  31. const originalIndex = this.waitNodes.children.indexOf(cup);
  32. cup.setPosition(startX + originalIndex * spacing, 0, 0);
  33. });
  34. // console.log('WaitArea 杯子数量: ', this.cups.length);
  35. }
  36. addCup(cup: Node) {
  37. cup.setParent(this.waitNodes);
  38. this.arrangeCups();
  39. }
  40. takeCup(): Node | null {
  41. if (this.cups.length === 0) return null;
  42. const cup = this.cups.pop()!;
  43. // 仅解除父节点关系,不立即销毁
  44. cup.parent = null;
  45. this.arrangeCups();
  46. return cup;
  47. }
  48. // 添加明确的返回类型
  49. getCups(): Node[] {
  50. return this.waitNodes.children;
  51. }
  52. }