
import Sprite3D = Laya.Sprite3D;
import Vector3 = Laya.Vector3;
import Transform3D = Laya.Transform3D;
import GameSetting from "../game_module/GameSetting";
import GameTool from "../Util/GameTool";
import { Mathf } from "../Util/Mathf";
import { DispatcherManager, EventType } from "./DispatcherManager";
import MainRoleManager from "../Manager/MainRoleManager";
import PlaneManager from "../Manager/PlaneManager";
import CameraManager from "../Manager/CameraManager";
export enum CameraState {
    game = 1,
    jump
}
export default class Camera extends Laya.Script3D {
    public _tran: Transform3D;
    private selfObj: Laya.Camera;

    constructor() {
        super();
    }

    onAwake() {
        this.selfObj = this.owner as Laya.Camera;
        this._tran = this.selfObj.transform;
        this.nowP = Mathf.VecZero;
        this.nowR = Mathf.VecZero;
        this.dis = Mathf.VecZero;
        this.temp = Mathf.VecZero;
      //  this.selfObj.fieldOfView = 720 / 1280 * (Laya.stage.height) / Laya.stage.width * this.selfObj.fieldOfView;
        //this.selfObj.fieldOfView =  720/1280 * Laya.stage.height / Laya.stage.width*60;
        // this.selfObj.viewport.width=300;
        // this.selfObj.viewport.height=300;
        // this.selfObj.viewport=new Laya.Viewport(0,0,720,1280);
    }

    private dis: Vector3;
    Init() {
        let t = CameraManager.Instance.startPosTra;
        this._tran.position = t.position.clone();
        this._tran.localRotationEuler = Mathf.SpToCam(t.localRotationEuler.clone());
        this.dis.x = MainRoleManager.Instance.playerStartTra.position.x - this._tran.position.x;
        this.dis.y = MainRoleManager.Instance.playerStartTra.position.y - this._tran.position.y;
        this.dis.z = MainRoleManager.Instance.playerStartTra.position.z - this._tran.position.z;
      
    }


    private tarTra: Transform3D;
    private arg: number = 0.1;
    public SetCameraByState(type: CameraState, arg) {
        this.arg = arg;
        switch (type) {
            case CameraState.game:
                this.tarTra = MainRoleManager.Instance.playerTra;
                if (!this.tarTra) return;
                let t = CameraManager.Instance.startPosTra;
                this._tran.position = t.position.clone();
                this._tran.localRotationEuler = Mathf.SpToCam(t.localRotationEuler.clone());
                this.canmovegame = true;
                break;
            case CameraState.jump:
                this.tarTra = PlaneManager.Instance.jumpCamTra;
                if (!this.tarTra) return;
                this.canmovegame = false;
                Laya.timer.frameLoop(1, this, this.JumpCameraMove);

                Laya.timer.once(3000, this, this.StopMove);
                break;
        }
    }
    StopMove() {
        Laya.timer.clear(this, this.JumpCameraMove);
    }

    private canmovegame = false;
    private temp: Vector3;
    MoveGame(z) {
        if (!this.canmovegame) return;
        // this.temp.x = this.tarTra.position.x - this.dis.x;
        // this.temp.y = this.tarTra.position.y - this.dis.y;
        // this.temp.z = this.tarTra.position.z - this.dis.z;
        //  this._tran.position = this.temp.clone();
        this.temp.z = z - this.dis.z;
        this._tran.localPositionZ = this.temp.z;
    }


    private nowR: Vector3;
    private nowP: Vector3;
    private JumpCameraMove() {
        let p, r;
        if (this.arg == 1) {
            p = this.tarTra.position;
            r = Mathf.SpToCam(this.tarTra.localRotationEuler.clone());
        } else {
            p = Mathf.MixVector3(this._tran.position.clone(), this.tarTra.position, this.arg, this.nowP);
            r = Mathf.MixVector3(this._tran.localRotationEuler, Mathf.SpToCam(this.tarTra.localRotationEuler.clone()), this.arg, this.nowR);

        }
        this._tran.position = p;
        this._tran.localRotationEuler = r;
    }



    // private sizeTween: Laya.Tween;
    // SetCameraSize(isAdd) {
    //     let tar;
    //     let t;
    //     if (isAdd) {
    //         t = 500;
    //         tar = this.nowSize + 25;
    //     }
    //     else {
    //         tar = this.nowSize;
    //         t = 1000;
    //     }
    //     this.sizeTween = Laya.Tween.to(this.selfObj, { fieldOfView: tar }, t, null, Laya.Handler.create(this, () => {
    //         console.log("sizeTween com");
    //         this.sizeTween.clear();
    //         //this.dirTween.recover();
    //     }, null, false), null, true, false);
    // }

    onDisable() {

    }
    onDestroy() {
        Laya.timer.clearAll(this);
    }
    public startShake = false;  //camera是否开始震动
    public seconds = 0;    //震动持续秒数
    public started = false;    //是否已经开始震动
    public quake = 0.3;       //震动系数
    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 = GameTool.random_1to1() * this.quake;
        var y = GameTool.random_1to1() * this.quake;
        var z = GameTool.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;
    }

}