const { ccclass, property } = cc._decorator;

@ccclass
export default class WanderTweenAnimation extends cc.Component {

    @property(cc.Node)
    wanderTweenNode: cc.Node = null;

    @property(cc.Vec3)
    wanderPoint: cc.Vec3[] = [];

    @property(cc.Node)
    wanderFrameList: cc.Node[] = [];

    @property()
    speed: number = 30;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    // start() {}

    protected onEnable(): void {
        if (!this.wanderTweenNode) {
            this.wanderTweenNode = this.node;
        }

        for (const iterator of this.wanderFrameList) {
            iterator.active = false;
        }

        let atween = cc.tween(this.wanderTweenNode)
        for (let index = 0; index < this.wanderPoint.length; index++) {
            let nextIndex = (index >= this.wanderPoint.length - 1) ? 0 : index + 1;
            let delayTime_ = this.wanderPoint[nextIndex].sub(this.wanderPoint[index]).len() / this.speed / 10;
            let startPoint = this.wanderPoint[index],
                endPoint = this.wanderPoint[nextIndex],
                showNodo = this.wanderFrameList[index];
            atween.set({ position: startPoint })
                .call(() => {
                    showNodo.active = true;
                })
                .to(delayTime_, { position: endPoint })
                .call(() => {
                    showNodo.active = false;
                })
        }
        atween.union()
            .repeatForever()
            .start();

        this.node.attr({
            start: () => {
                atween.start();
            },
            stop: () => {
                atween.stop();
            }
        });

    }



    // update (dt) {}
}