import Vector2 = Laya.Vector2;
import Vector3 = Laya.Vector3;
import { GameTools } from "../Tools/GameTools";
import { AssetManager } from "../Tools/AssetManager";
export class PlayerControl{
    private static ins:PlayerControl;
    public Player:Laya.Sprite3D;
    public Camera:Laya.Camera;
    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.Player.transform.position = new Vector3();
        
        this.Camera = AssetManager.Instance.maincamera;
        this.Camera.transform.position = new Vector3(0,15,-10);
        this.Camera.transform.localRotationEuler = new Vector3(-25,0,0);

        this.MouseListen();
        this.CameraFollow();
    }
    MouseListen(){
        Laya.stage.on(Laya.Event.MOUSE_DOWN,this,this.OnTouchEnter);
        Laya.stage.on(Laya.Event.MOUSE_UP,this,this.OnTouchOut);
        Laya.stage.on(Laya.Event.MOUSE_OUT,this,this.OnTouchOut);
    }
    mousepos = new Vector2(0,0);
    offx = 0;
    curtouchsmoothness = 0.02;
    OnTouchEnter(){
        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);
    }
    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.Player.transform.translate(new Vector3(-movespeed,0,0));
        this.Player.transform.position = this.Player.transform.position.x < -4.5?new Vector3(-4.5,this.Player.transform.position.y,this.Player.transform.position.z):this.Player.transform.position;
        this.Player.transform.position = this.Player.transform.position.x > 4.5?new Vector3(4.5,this.Player.transform.position.y,this.Player.transform.position.z):this.Player.transform.position;
        var dir = new Vector3(-offestX*0.001,0,0);
        this.mousepos = MousePos;
    }
     /**相机跟随*/
     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;
     }
    

}