
import Sprite3D = Laya.Sprite3D;
import Vector3 = Laya.Vector3;
import Transform3D = Laya.Transform3D;
import {GameTool} from "../Util/GameTool";
import { MathTool } from "../Util/MathTool";
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 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 = MathTool.VecZero;
        this.nowR = MathTool.VecZero;
        this.dis = MathTool.VecZero;
        this.temp = MathTool.VecZero;
    }

    private dis: Vector3;
    Init() {
        let t = CameraManager.Instance.startPosTra;
        this._tran.position = t.position.clone();
        this._tran.localRotationEuler = MathTool.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;
    }

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

    

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

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

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

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

    /******************************************************************** */
    private _scene:Laya.Scene3D;
    onComplete(){
        //添加地面
        var grid:Laya.Sprite3D = this._scene.addChild(Laya.Loader.getRes("res/threeDimen/staticModel/grid/plane.lh")) as Laya.Sprite3D;
        //地面接收阴影
        (grid.getChildAt(0) as Laya.MeshSprite3D).meshRenderer.receiveShadow = true;
        //设置该精灵的蒙版为5(所属图层)
        (grid.getChildAt(0) as Laya.MeshSprite3D).layer = 5;
        
		//添加静态猴子
		var staticLayaMonkey = new Laya.MeshSprite3D(Laya.Loader.getRes("res/threeDimen/skinModel/LayaMonkey/Assets/LayaMonkey/LayaMonkey-LayaMonkey.lm"));
    	this._scene.addChild(staticLayaMonkey);
        //设置静态猴子的材质
        staticLayaMonkey.meshRenderer.material = Laya.Loader.getRes("res/threeDimen/skinModel/LayaMonkey/Assets/LayaMonkey/Materials/T_Diffuse.lmat");
        //设置静态猴子的蒙版为1(所属图层)
        staticLayaMonkey.layer = 1;
        staticLayaMonkey.transform.position = new Laya.Vector3(0, 0, 0.5);
        staticLayaMonkey.transform.localScale = new Laya.Vector3(0.3, 0.3, 0.3);
        staticLayaMonkey.transform.rotation = new Laya.Quaternion(0.7071068, 0, 0, -0.7071067);
        //产生阴影
        staticLayaMonkey.meshRenderer.castShadow = true;
        
        //克隆sprite3d
        var layaMonkey_clone1 = Laya.Sprite3D.instantiate(staticLayaMonkey, this._scene, false, new Laya.Vector3(0.0, 0, 0.5));
        var layaMonkey_clone2 = Laya.Sprite3D.instantiate(staticLayaMonkey, this._scene, false, new Laya.Vector3(0.0, 0, 0.5));
        var layaMonkey_clone3 = Laya.Sprite3D.instantiate(staticLayaMonkey, this._scene, false, new Laya.Vector3(0.0, 0, 0.5));
        
        //设置蒙版(所属图层)
        layaMonkey_clone1.layer = 2;
        layaMonkey_clone2.layer = 3;
        layaMonkey_clone3.layer = 0;
        //平移
        layaMonkey_clone1.transform.translate(new Laya.Vector3(1.5, 0, 0.0));
        layaMonkey_clone2.transform.translate(new Laya.Vector3( -1.5, 0, 0.0));
        layaMonkey_clone3.transform.translate(new Laya.Vector3( 2.5, 0, 0.0));
        //旋转
        layaMonkey_clone2.transform.rotate(new Laya.Vector3(0, 60, 0), false, false);
        //缩放
        var scale = new Laya.Vector3(0.1, 0.1, 0.1);
        layaMonkey_clone3.transform.localScale = scale;
    }
    /******************************************************************** */

    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;
    }
    private Shake() {
        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;
    }

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

    /******************************************************************** */
    private scene:Laya.Scene3D;
    private camera:Laya.Camera;
    private layaMonkey3D:Laya.Sprite3D;
    private layaMonkey2D:Laya.Image;
    private _position:Laya.Vector3 = new Laya.Vector3();
    private _outPos:Laya.Vector3 = new Laya.Vector3();
    private scaleDelta:number = 0;
    private scale:Laya.Vector3 = new Laya.Vector3(0.1,0.1,0.1);
    private animate(): void {
        this._position.x = Math.sin(this.scaleDelta += 0.01);
        this.layaMonkey3D.transform.position = this._position;
        this.layaMonkey3D.transform.scale = this.scale;
        //转换坐标
        this.camera.viewport.project(this.layaMonkey3D.transform.position, this.camera.projectionViewMatrix, this._outPos);
        //赋值给2D
        this.layaMonkey2D.pos(this._outPos.x / Laya.stage.clientScaleX, this._outPos.y / Laya.stage.clientScaleY);
    }
    /******************************************************************** */

    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;
    }

}