import { _decorator, Component, Node, tween, Vec3, view } from 'cc';
const { ccclass, property, executeInEditMode } = _decorator;

@ccclass('WaitArea')
@executeInEditMode
export class WaitArea extends Component {

    @property(Node)
    waitNodes: Node = null!;  // 实际存放杯子的节点

    // 直接使用waitNodes的子节点管理
    get cups(): Node[] {
        // 反转数组实现从右到左排列（最右杯子在数组末尾）
        return this.waitNodes.children.slice().reverse();
    }

    start() {
        // 获取设计分辨率和实际窗口尺寸
        const designSize = view.getDesignResolutionSize();
        const visibleSize = view.getVisibleSize();

        // 计算高度的缩放比例
        const scaleY = visibleSize.height / designSize.height;
        const originalY = this.node.worldPosition.y;

        const designSizeY = designSize.height * (scaleY - 1);
        const visibleSizeY = visibleSize.height * (scaleY - 1);
        this.node.worldPosition = new Vec3(this.node.worldPosition.x, originalY + (visibleSizeY - designSizeY), 0);
    }

    update(deltaTime: number) {

    }

    // 从右向左排列
    arrangeCups() {
        const startX = -50;
        const spacing = -110;

        this.cups.forEach((cup, index) => {
            const originalIndex = this.waitNodes.children.indexOf(cup);
            cup.setPosition(startX + originalIndex * spacing, 0, 0);
        });

        // console.log('WaitArea 杯子数量: ', this.cups.length);
    }

    addCup(cup: Node) {
        cup.setParent(this.waitNodes);
        this.arrangeCups();
    }

    takeCup(): Node | null {
        if (this.cups.length === 0) return null;
        const cup = this.cups.pop()!;
        // 仅解除父节点关系，不立即销毁
        cup.parent = null;
        this.arrangeCups();
        return cup;
    }

    // 添加明确的返回类型
    getCups(): Node[] {
        return this.waitNodes.children;
    }
}


