const {ccclass, property} = cc._decorator;

@ccclass
export class CameraUtils extends cc.Component {
    /* class member could be defined like this */
    // dummy = '';

    /* use `property` decorator if your want the member to be serializable */
    // @property
    // serializableDummy = 0;
    private min:cc.Vec3;
    private max:cc.Vec3;
    private out:cc.Vec3=new cc.Vec3();

    private startTime:number;
    private endTime:number;
    private startPos:cc.Vec3=new cc.Vec3();

    private shakeing:boolean;

    private camera:cc.Camera;
    /**
     * 抖动
     * @param time  持续时间
     * @param min   最小值
     * @param max   最大值
     */
    shake(time:number,min:cc.Vec3,max:cc.Vec3):void{
        this.min=min;
        this.max=max;
        this.startTime=cc.sys.now();
        this.endTime=this.startTime+time;
        this.shakeing=true;
        this.startPos.set(this.node.position);
        this.camera=this.node.getComponent(cc.Camera);
    }

    start () {
        // Your initialization goes here.
    }

    update (deltaTime: number) {
        if(this.shakeing){
            let currentTime:number=cc.sys.now();
            if(currentTime<this.endTime){
                this.randomV3(this.min,this.max,this.out);
                this.out.x+=this.startPos.x;
                this.out.y+=this.startPos.y;
                this.out.z+=this.startPos.z;
                this.node.position=this.out;
            }else{
                this.node.position=this.startPos;
            }
        }
    }

    private randomV3(min:cc.Vec3,max:cc.Vec3,out:cc.Vec3):void{
        let dx:number=max.x-min.x;
        let dy:number=max.y-min.y;
        let dz:number=max.z-min.z;
        out.x=min.x+Math.random()*dx;
        out.y=min.y+Math.random()*dy;
        out.z=min.z+Math.random()*dz;
    }
}
