import { GameTools } from "../GameUtils/GameTools";
import { PlayerManager } from "./PlayerManager";
import { DispatcherManager, StageState } from "../GameUtils/EventManager";

export class CameraManager{
    private static ins:CameraManager;
    constructor(){
        CameraManager.ins = this;
        DispatcherManager.StageOn(StageState.Start,this,this.CameraFollow);
        DispatcherManager.StageOn(StageState.Win,this,this.CameraCancelFollow);
        DispatcherManager.StageOn(StageState.Lose,this,this.CameraCancelFollow);
    }
    public static get Instance():CameraManager{
        if(CameraManager.ins == null){
            return new CameraManager();
        }
        return CameraManager.ins;
    }
    public camera:Laya.Camera;
    Init(){
        // this.camera.transform.position = 
    }
    /**相机跟随*/
    CameraFollow(){
        this.hights = 0;
        this.wights = 0;
        Laya.Vector3.subtract(PlayerManager.Instance.PlayerNode.transform.position,this.camera.transform.position,this.poschazhi);
        Laya.timer.frameLoop(1,this,this.GenSui);
    }
    poschazhi = new Laya.Vector3();
    gengsuipos = new Laya.Vector3();
    lllpos = new Laya.Vector3();
    hights = 0;
    wights = 0;
   
    /**相机跟随方法 */
    GenSui(){
        Laya.Vector3.subtract(PlayerManager.Instance.PlayerNode.transform.position,this.poschazhi,this.gengsuipos);
        this.hights = GameTools.Instance.lerp(this.hights,1,0.03);
        this.wights = GameTools.Instance.lerp(this.wights,0.8,0.02);
        this.gengsuipos = new Laya.Vector3(this.gengsuipos.x,this.gengsuipos.y,this.gengsuipos.z );
        Laya.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 Laya.Vector3(this.gengsuipos.x,this.lllpos.y,this.lllpos.z);
        this.camera.transform.position = this.lllpos;
        var qqqwe = new Laya.Quaternion();
        var eyePos = new Laya.Vector3(this.gengsuipos.x,this.gengsuipos.y - 0.8,this.gengsuipos.z);
        Laya.Quaternion.lookAt(eyePos,PlayerManager.Instance.PlayerNode.transform.position, new Laya.Vector3(0,1,0), qqqwe);
        qqqwe.invert(qqqwe);
        Laya.Quaternion.slerp(this.camera.transform.rotation,qqqwe,0.1,qqqwe);
        this.camera.transform.rotation = qqqwe;
    }
    CameraCancelFollow(){
       Laya.timer.clear(this,this.GenSui);
    }
}