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

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

    @property(Node)
    outNodes: Node = null!;

    // 直接使用outNodes的子节点管理
    get cups() {
        return this.outNodes.children;
    }

    start() {

    }

    update(deltaTime: number) {

    }

    // 排列杯子
    arrangeCups() {
        const startX = 40; //水杯的宽度一半
        const spacing = 80;
        this.cups.forEach((cup, index) => {
            cup.setPosition(startX + index * spacing, 0, 0);
        });
    }

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

    removeCup(cup: Node) {
        cup.removeFromParent();
        this.arrangeCups();
    }

    getCups() {
        return this.cups;
    }

    // 添加带动画排列方法
    arrangeCupsWithAnimation() {
        const startX = -200; // 从左侧开始
        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();
            }
        });
    }
}


