import { Component, UITransform, Vec3, view, _decorator } from 'cc';
import { utilTools } from '../../../utils/utilTools';
const { ccclass, property } = _decorator;

@ccclass('FlutterEffect')
export class FlutterEffect extends Component {

    // 指定速度，不指定将会随机
    speed: number = 0;
    speedArr: any = [40, 50, 60];

    tempPos: Vec3 = new Vec3();

    onLoad() {

    }

    start() {
        let visibleSize = view.getVisibleSize();
        let chs = this.node.children;
        for (let i in chs) {
            let node = chs[i];
            if (this.speed > 0) {
                // 指定速度
                node["speed"] = this.speed;
            } else {
                // 随机分配速度
                node["speed"] = utilTools.getRandomItemByArr(this.speedArr);
            }
            // 移动目标位置
            node["posX"] = visibleSize.width * 0.5 + node.getComponent(UITransform).width * 0.5;

        }
    }

    update(dt: number) {
        let chs = this.node.children;
        for (let i in chs) {
            let node = chs[i];
            node.getPosition(this.tempPos);
            if (this.tempPos.x > node["posX"]) {
                this.tempPos.x = -node["posX"];
            }
            this.tempPos.x += (dt * node["speed"]);
            node.setPosition(this.tempPos);
        }
    }
}

