import Vector2 = Laya.Vector2;
import Vector3 = Laya.Vector3;
import {LoadView} from "../View/LoadView";
import { DispatcherControl, DispatcherState } from "../Tools/DispatcherControl";
import { GameTools } from "../Tools/GameTools";
    export class CameraControl{
    private static ins:CameraControl;
    public maincamera:Laya.Camera;
    constructor(){
        CameraControl.ins = this;
        DispatcherControl.DispatcherOn(DispatcherState.LoadComplete,this,this.Init);
    }
    public static get Instance():CameraControl{
        if(CameraControl.ins){
            return CameraControl.ins
        }
        else{
            return new CameraControl();
        }
    }
    Init(){
        this.maincamera = LoadView.Instance.maincamera;
        this.maincamera.transform.position = new Laya.Vector3(19.5,100,16.7);
        Laya.stage.on(Laya.Event.MOUSE_DOWN,this,this.TouchEnter);
        Laya.stage.on(Laya.Event.MOUSE_UP,this,this.OnTouchOut);
        Laya.stage.on(Laya.Event.MOUSE_OUT,this,this.OnTouchOut);
    }

    open:boolean = false;   
    mousepos = new Vector2(0,0);
    /**鼠标进入*/ 
    TouchEnter()
    {   
        this.mousepos = new Vector2(Laya.MouseManager.instance.mouseX,Laya.MouseManager.instance.mouseY);
        Laya.timer.clear(this,this.OnTouchMove);
        Laya.timer.frameLoop(1,this,this.OnTouchMove);
    }

    /**鼠标离开*/
    OnTouchOut()
    {   
        Laya.timer.clear(this,this.OnTouchMove);
     }
    curtouchsmoothness:number = 0.02;
    offx = 0;

    /**鼠标移动*/
    OnTouchMove(){
        var MousePos = new Vector2(Laya.MouseManager.instance.mouseX,Laya.MouseManager.instance.mouseY);
        var offestX = MousePos.x - this.mousepos.x;
        offestX = offestX > 100?100:offestX;
        offestX = offestX < -100?-100:offestX;
        this.offx = GameTools.Instance.lerp(this.offx,offestX,this.curtouchsmoothness);
        var movespeed = this.offx*0.07; 
        // this.curPlayer.transform.translate(new Vector3(-movespeed,0,0));
        // this.curPlayer.transform.position = this.curPlayer.transform.position.x < -4.5?new Vector3(-4.5,this.curPlayer.transform.position.y,this.curPlayer.transform.position.z):this.curPlayer.transform.position;
        // this.curPlayer.transform.position = this.curPlayer.transform.position.x > 4.5?new Vector3(4.5,this.curPlayer.transform.position.y,this.curPlayer.transform.position.z):this.curPlayer.transform.position;
        var dir = new Vector3(-offestX*0.001,0,0);
        this.mousepos = MousePos;
    }


    /**相机跟随*/
    CameraFollow(){
       // this.animator.play("cycling");
        this.hights = 0;
        this.wights = 0;
        Vector3.subtract(this.curPlayer.transform.position,this.maincamera.transform.position,this.poschazhi);
        Laya.timer.frameLoop(1,this,this.GenSui);
    }
    poschazhi = new Vector3();
    gengsuipos = new Vector3();
    lllpos = new Vector3();
    hights = 0;
    wights = 0;
   
    /**相机跟随方法 */
    GenSui(){
        Vector3.subtract(this.curPlayer.transform.position,this.poschazhi,this.gengsuipos);
        this.hights = Tools.Instance.lerp(this.hights,1.0,0.03);
        this.wights = Tools.Instance.lerp(this.wights,0.8,0.02);
        this.gengsuipos = new Vector3(this.gengsuipos.x,this.gengsuipos.y + this.hights,this.gengsuipos.z - this.wights);
        Vector3.lerp(this.curCamera.transform.position,this.gengsuipos,0.25,this.lllpos);
        this.lllpos = new Vector3(this.gengsuipos.x,this.gengsuipos.y,this.lllpos.z);
        this.lllpos = new Vector3(this.gengsuipos.x,this.lllpos.y,this.lllpos.z);
        this.curCamera.transform.position = this.lllpos;
        var qqqwe = new Laya.Quaternion();
        var eyePos = new Vector3(this.gengsuipos.x,this.gengsuipos.y - 0.8,this.gengsuipos.z);
        Laya.Quaternion.lookAt(eyePos,this.curPlayer.transform.position, new Vector3(0,1,0), qqqwe);
        qqqwe.invert(qqqwe);
        Laya.Quaternion.slerp(this.curCamera.transform.rotation,qqqwe,0.1,qqqwe);
        this.curCamera.transform.rotation = qqqwe;
        
    }
    
}