import { _decorator, Component, Node, Prefab, instantiate, CCObject, Vec3, find } from "cc";
const { ccclass, property } = _decorator;

@ccclass("obstacleStart")
export class obstacleStart extends Component {
    //障碍物预制体
    @property(cc.Prefab)
    obstaclePrefab: Prefab[] = [];

    //计时
    countTimer: number = 0

    // @property(cc.Node)
    // obstacleNew: Node = null;


    //生成障碍物的时间间隔
    startTime: number = 2;


    start() {
        // Your initialization goes here.
        this.produceobstacle();
    }

    //生成障碍物预设体
    public produceobstacle() {
        //    this.obstacleNew=cc.instantiate(this.obstaclePrefab[Math.round(Math.random()*this.obstaclePrefab.length)]);
        //     this.node.addChild(this.obstacleNew);
        //     var nodeX=Math.random()*35-12;
        //     console.log(nodeX+"**/*/*/")
        //     var nodeY=this.node.getPosition().y;
        //     var nodeZ=this.node.getPosition().z;


        //     //this.obstacleNew.position=cc.v3(Math.random()*35-12,nodeY,nodeZ);
        //     this.obstacleNew.setPosition(new Vec3(nodeX,nodeY,nodeZ))


        //     console.log(this.obstacleNew.getPosition().x+"****");

        //     console.log(Math.round(Math.random()*this.obstaclePrefab.length));
        //     console.log(Math.random()*35-12)
        //     console.log("生成")


        var obstacleNew = cc.instantiate(this.obstaclePrefab[Math.round(Math.random() * this.obstaclePrefab.length)]);
        this.node.addChild(obstacleNew);
        var nodeX = Math.random() * 35 - 12;
        var nodeY = this.node.getPosition().y;
        var nodeZ = this.node.getPosition().z;
        obstacleNew.setPosition(new Vec3(nodeX, nodeY, nodeZ))
    }


    update(deltaTime: number) {
        this.countTimer += deltaTime;
        //console.log(this.countTimer)
        if (this.countTimer > this.startTime) {

            this.produceobstacle();
            this.countTimer = 0
        }
    }

    // update (deltaTime: number) {
    //     // Your update function goes here.
    // }
}
