import Vector3 = Laya.Vector3;
import Sprite3D = Laya.Sprite3D;
import { GameTools } from "../Tools/GameTools";
import { EventManager, EventState, StageState } from "../Tools/EventManager";
import { AssetManager } from "../Tools/AssetManager";
//人物状态
export enum CharacterState{Run,Idle,Die,Win,Shoot}
export class Runner extends Laya.Script3D{
    private static ins:Runner;
    //玩家
    m_player:Sprite3D;
    //玩家的动画控制器
    m_animator:Laya.Animator;
    //马的动画控制器
    m_horseanimator:Laya.Animator;
    //人物状态
    m_characterstate:CharacterState;
    public canmove:boolean = false;
    //人物速度
    private _speed:number = 1;
    //是否处于按压状态
    public _press:boolean = false;

    //下方射线打到的物体
    private DownHit:Laya.HitResult = new Laya.HitResult();
    //前方射线打到的物体
    private FowardHit:Laya.HitResult = new Laya.HitResult();

    constructor(){
        super();
    }
    public static get Instance():Runner{
        if(Runner.ins){
            return Runner.ins
        }
        else{
            return new Runner();
        }
    }
    onAwake(){
        this.m_player = this.owner as Sprite3D;
        this.m_animator = this.owner.getComponent(Laya.Animator) as Laya.Animator;
        this.m_horseanimator = this.owner.getChildAt(1).getComponent(Laya.Animator) as Laya.Animator;
        this.ChangeState(CharacterState.Idle);

        EventManager.StageOn(StageState.Start,this,this.Begin);
    }
    Begin(){
        this.canmove = true;
        this.ChangeState(CharacterState.Run);
    }
    curspeed = 0;
    onUpdate(){
        if(this.canmove){
            if(this._press){
                this._speed = 2;
            }
            this.Move(new Vector3(0,0,1),this._speed);
        }
        this.RayCheck();
    }
    MoveX(_speed:number){
        _speed*=0.07;
        //var dir = new Vector3(_speed,0,0);
        //Vector3.add(this.m_sprite.transform.position,dir,this.m_sprite.transform.position);
        this.m_player.transform.translate(new Vector3(_speed,0,0));
        // this.m_player.transform.position = this.m_player.transform.position.x < -4.5?new Vector3(-4.5,this.m_player.transform.position.y,this.m_player.transform.position.z):this.m_player.transform.position;
        // this.m_player.transform.position = this.m_player.transform.position.x > 4.5?new Vector3(4.5,this.m_player.transform.position.y,this.m_player.transform.position.z):this.m_player.transform.position;
        //this.m_sprite.transform.position = this.curpos;
    }
    currotV = new Vector3();
    currot = new Laya.Quaternion;
    NormalizeSpeed:Vector3 = new Vector3();
    curpos:Vector3 = new Vector3();
    offest:Vector3 = new Vector3();

    TurnDir(_dir:Vector3)
    {
        if(_dir.x != 0)
        {
            Vector3.lerp(this.currotV,_dir,0.35,_dir);
            this.currotV = _dir;
            Laya.Quaternion.rotationLookAt(_dir,new Vector3(0,1,0),this.currot);
            Laya.Quaternion.slerp(this.m_player.transform.rotation,this.currot,0.025,this.currot);
            this.currot = new Laya.Quaternion(0,this.currot.y,0);
            this.m_player.transform.rotation = this.currot;
        }
        else
        {
            Laya.Quaternion.slerp(this.m_player.transform.rotation,new Laya.Quaternion(0,0,0,0),0.1,this.currot);
            this.currot = new Laya.Quaternion(0,this.currot.y,0);
            this.m_player.transform.rotation = this.currot;
        }
    }
    Move(_dir:Vector3,_speed:number){
        var sp = GameTools.Instance.lerp(this.curspeed,_speed,0.1);
        this.curspeed = sp;
        Vector3.scale(_dir,this.curspeed,this.offest);
        Vector3.add(this.m_player.transform.position,this.offest,this.curpos);
        Vector3.lerp(this.m_player.transform.position,this.curpos,0.15,this.curpos);
        
        this.curpos = new Vector3(this.curpos.x,this.curpos.y,this.curpos.z);
        this.m_player.transform.position = this.curpos;
    }
    ChangeState(_state:CharacterState){
        if(this.m_characterstate == _state)return
        this.m_characterstate = _state;
        switch (this.m_characterstate) {
            case CharacterState.Idle:
                this.Idle();
                break;
            case CharacterState.Run:
                this.Run();
                break
            case CharacterState.Die:
                this.Die();
                break;
            case CharacterState.Win:
                this.Win();
                break;
            case CharacterState.Shoot:
                this.Shoot();
                break;
        }
    }
    Idle(){
        this.m_animator.play("Idle");
        this.m_horseanimator.play("Horse_Idle");
    }
    Run(){
        this.m_animator.play("Idle");
        this.m_horseanimator.play("Horse_Run");
    }
    Die(){

    }
    Win(){

    }
    Shoot(){
        this.m_animator.play("aimshoot");
        this.m_animator.play("Horse_Run");
    }
    RayCheck(){
        if(this.DownRayCheck){

        }
        if(this.FowardRayCheck){
            this.CrashBarrier();
        }
    }
    //撞到障碍物
    CrashBarrier(){
        var target = this.FowardHit.collider.owner as Laya.Sprite3D;
        if(target.name == ""){

        }
        else if(target.name == ""){

        }
        else if(target.name == ""){

        }
        else if(target.name == ""){

        }
    }
    
    /**竖直方向射线检测*/
    get DownRayCheck():boolean{
        var isDownHit = false;
        var startPos:Vector3 = new Vector3(this.m_player.transform.position.x,this.m_player.transform.position.y+1.5,this.m_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;
    }
    /**水平方向射线检测*/
    get FowardRayCheck():boolean{
        var isFowardHit = false;
        var startPos:Vector3 = new Vector3(this.m_player.transform.position.x,this.m_player.transform.position.y+1.5,this.m_player.transform.position.z+0.5);
        var direction:Vector3 = new Vector3(0,0,1);
        var DownRay:Laya.Ray = new Laya.Ray(startPos,direction);
        if(AssetManager.Instance.mainscene.physicsSimulation.rayCast(DownRay,this.FowardHit,1)){
            if (this.FowardHit.collider.collisionGroup == 100) {
                isFowardHit = true;
            }
        }
        return isFowardHit;
    }
}