import Vector2 = Laya.Vector2;
import Vector3 = Laya.Vector3;
import { GameTools } from "../Tools/GameTools";
import { AssetManager } from "../Tools/AssetManager";
import { Runner } from "./Runner";
export class PlayerControl{
    private static ins:PlayerControl;
    public Player:Laya.Sprite3D;
    public Camera:Laya.Camera;
    //向下检测到的物体
    public DownHit:Laya.HitResult = new Laya.HitResult();
    //是否能移动
    public canmove:boolean = true;
    //玩家速度
    public speed:number = 1;
    //是否为按压状态
    public press:boolean = false;

    _runner:Runner;

    constructor(){
        PlayerControl.ins = this;
    }
    public static get Instance():PlayerControl{
        if(PlayerControl.ins){
            return PlayerControl.ins
        }
        else{
            return new PlayerControl();
        }
    }
    Init(_player:Laya.Sprite3D){
        this.Player = _player;
        this._runner = this.Player.addComponent(Runner) as Runner;
        this.Player.transform.position = new Vector3();
        
        this.Camera = AssetManager.Instance.maincamera;
        this.Camera.transform.position = new Vector3(0,10,-15);
        this.Camera.transform.localRotationEuler = new Vector3(-5,0,0);
        Laya.stage.on(Laya.Event.MOUSE_DOWN,this,this.onMouseDown);
        this.CameraFollow();
    }
    
    mouseDownTime:number;
    startMousePos:Vector3;
    endMousePos:Vector3;
    curMousePos:Vector3;
    curMousePos1:Vector2;
    offset:Vector3 = new Vector3(0,0,0);
    offestx = 0;

    onMouseDown(e: Laya.Event) {
        if(!this._runner.canmove)return;
        this._runner._press = true;
        this.mouseDownTime = Laya.Browser.now();
        this.startMousePos = new Vector3(Laya.MouseManager.instance.mouseX, Laya.MouseManager.instance.mouseY);
        var startpoint = new Laya.Vector2(Laya.MouseManager.instance.mouseX,Laya.MouseManager.instance.mouseY);
        this.curMousePos = this.startMousePos.clone();
        this.curMousePos1 = startpoint.clone();
        Laya.timer.frameLoop(1,this,this.onMouseMove);
        Laya.stage.on(Laya.Event.MOUSE_UP, this, this.onMouseUp);
        // // //移出屏幕视作松开鼠标
        Laya.stage.on(Laya.Event.MOUSE_OUT, this, this.onMouseUp);
    }

    onMouseMove() {
        var MousePos = new Laya.Vector2(Laya.MouseManager.instance.mouseX,Laya.MouseManager.instance.mouseY);
        var offestX = MousePos.x - this.curMousePos1.x;
        this.offestx = GameTools.Instance.lerp(this.offestx,offestX,0.015);
        this._runner.MoveX(-this.offestx);
        var dir = new Vector3(-offestX*0.005,0,0);
        this._runner.TurnDir(dir);
        this.curMousePos1 = new Vector2(MousePos.x,MousePos.y) ;
    }

    onMouseUp() {
        this.offestx = 0;
        Laya.timer.clear(this,this.onMouseMove);
        this._runner._press = false;
        if (!this.startMousePos) return;
        Laya.stage.off(Laya.Event.MOUSE_MOVE, this, this.onMouseMove);
        Laya.stage.off(Laya.Event.MOUSE_UP, this, this.onMouseUp);
        Laya.stage.off(Laya.Event.MOUSE_OUT, this, this.onMouseUp);
        let now = Laya.Browser.now();
        let interval = now - this.mouseDownTime;
        this.endMousePos = new Vector3(Laya.MouseManager.instance.mouseX, Laya.MouseManager.instance.mouseY);
        let dist = Laya.Vector3.distance(this.endMousePos, this.startMousePos);
        //两次射箭之间间隔时间
        // // if (interval < GlobalConfig.Appsetting.Input.FireMinInterval) return;
        if (dist < 50 && interval < 200) {
            //射箭逻辑
            console.log("shoot--");
        }
        this.mouseDownTime = now;
    }

     /**相机跟随*/
     CameraFollow(){
         this.hights = 0;
         this.wights = 0;
         Vector3.subtract(this.Player.transform.position,this.Camera.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.Player.transform.position,this.poschazhi,this.gengsuipos);
         this.hights = GameTools.Instance.lerp(this.hights,1.0,0.03);
         this.wights = GameTools.Instance.lerp(this.wights,0.8,0.02);
         this.gengsuipos = new Vector3(this.gengsuipos.x,this.gengsuipos.y,this.gengsuipos.z - this.wights);
         Vector3.lerp(this.Camera.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.Camera.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.Player.transform.position, new Vector3(0,1,0), qqqwe);
         qqqwe.invert(qqqwe);
         Laya.Quaternion.slerp(this.Camera.transform.rotation,qqqwe,0.1,qqqwe);
         this.Camera.transform.rotation = qqqwe;
     }

     /**竖直方向射线检测*/
    get DownRayCheck():boolean{
        var isDownHit = false;
        var startPos:Vector3 = new Vector3(this.Player.transform.position.x,this.Player.transform.position.y+1.5,this.Player.transform.position.z+0.5);
        var direction:Vector3 = new Vector3(0,-1,0);
        var DownRay:Laya.Ray = new Laya.Ray(startPos,direction);
        if(AssetManager.Instance.mainscene.physicsSimulation.rayCast(DownRay,this.DownHit,100)){
            if (this.DownHit.collider.collisionGroup == 100) {
                isDownHit = true;
            }
        }
        return isDownHit;
     }

}