
import Sprite3D = Laya.Sprite3D;
import Vector3 = Laya.Vector3;
import Transform3D = Laya.Transform3D;
import Game_Tool from "../Util/Game_Tool";
import { Main } from "../../Main";
import { GameController } from "./GameController";
export default class CameraBehavior extends Laya.Script3D {

    //public target: Sprite3D;
    public _tran: Transform3D;
    private selfObj: Laya.Camera;


    public origPos: Vector3 = new Vector3(0, 12.88, -3.48);
    private speed: number = 0;
    //  private p_c_dis: number = 0;
    constructor() {
        super();
    }


    onAwake() {
        this.selfObj = this.owner as Laya.Camera;
        this._tran = this.selfObj.transform;
        let origSize = this.selfObj.fieldOfView;

        let origratio = 0.5;//    原始屏幕的宽高比
        let nowratio = Main.nowratio;

        let nowSize = Main.nowratio * origSize / origratio;
        this.selfObj.fieldOfView = nowSize;
    }

    onUpdate() {
        if (GameController.Instance.gamestart) this._tran.localPositionZ += this.speed;
    }

    onDestroy() {

    }


    public startShake = false;  //camera是否开始震动
    public seconds = 0;    //震动持续秒数
    public started = false;    //是否已经开始震动
    public quake = 0.2;       //震动系数

    private camPOS: Laya.Vector3 //camera的起始位置
    public ShakeFor(a, b) {

        this.camPOS = this.selfObj.transform.position.clone();
        this.seconds = a;
        this.started = true;
        this.startShake = true;
        this.quake = b;

    }
    onLateUpdate() {
        if (this.startShake) {
            this.Quake();
        }
        if (this.started) {
            this.started = false;
            Laya.timer.once(this.seconds * 1000, this, () => {
                this.startShake = false;
                this.selfObj.transform.position = this.camPOS;

            })
        }
    }
    private Quake() {
        var x = Game_Tool.random_1to1() * this.quake;
        var y = Game_Tool.random_1to1() * this.quake;
        var z = Game_Tool.random_1to1() * this.quake;
        var temp = new Vector3(this.camPOS.x + Math.random() * this.quake, this.camPOS.y + Math.random() * this.quake, this.camPOS.z + Math.random() * this.quake);
        this.selfObj.transform.position = temp;
    }
}