import { _decorator, Component, Node, tween, Vec3 } 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;
    }

    start() {

    }

    update(deltaTime: number) {

    }

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

        this.cups.forEach((cup, index) => {
            cup.setPosition(startX + index * 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.removeFromParent();
        this.arrangeCups();
        return cup;
    }

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

    // 添加带动画排列方法
    arrangeCupsWithAnimation() {
        const startX = 400; // 从右侧开始
        const spacing = -120; // 负间距向右移动

        this.cups.forEach((cup, index) => {
            const targetPos = new Vec3(startX + index * spacing, 0, 0);
            if (!cup.position.equals(targetPos)) {
                tween(cup)
                    .to(0.3, { position: targetPos }, { easing: 'sineOut' })
                    .start();
            }
        });
    }
}


