import { GameTools } from "../GameUtils/GameTools";
import Vector3 = Laya.Vector3;
import { Transform3DHelper } from "../GameUtils/NodeUtil";
import { DispatcherManager, StageState } from "../GameUtils/EventManager";
import { AssetManager } from "../Resources/AssetManager";

export class RoleMoveBehav extends Laya.Script3D{
    constructor(){
        super();
    }
    private m_player:Laya.Sprite3D;
    private start:boolean = false;
    private Downhit:Laya.HitResult = new Laya.HitResult();
    private FowardHit:Laya.HitResult = new Laya.HitResult();
    private isdead:boolean = false;

    onAwake(){
        this.m_player = this.owner as Laya.Sprite3D;
        DispatcherManager.StageOn(StageState.Start,this,this.Start)
    }
    Start(){
        this.start = true;
    }
    onUpdate(){
        if(!this.start)return;
        if(this.isdead){
            this.ChangeDeadState();
            return;
        }
        if(this.DownRayCheck){

        }
        else{
            this.isdead = true;
        }
        if(this.FowardRayCheck){

        }
        let foward = Transform3DHelper.getForward(this.m_player.transform);
        this.Move(foward,0.2);
    }
    curpos:Vector3 = new Vector3();
    offest:Vector3 = new Vector3();
    curspeed = 0;
    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;
    }

     /**移动射线判断*/
     protected MoveRayJupde()
     {
        var isDownHit = false;
         var hitResult:Laya.HitResult = new Laya.HitResult();
 
         var boxCollider:Laya.BoxColliderShape = new Laya.BoxColliderShape(0.2, 0.2, 0.2);
 
         var from:Vector3 = new Vector3(this.m_player.transform.position.x,this.m_player.transform.position.y+0.5,this.m_player.transform.position.z);
         var dir=new Vector3(0,-100,0);
         var to:Vector3 = new Vector3(0,0,0);
         Vector3.add(from,dir,to);
         
         if (AssetManager.Instance.mainscene.physicsSimulation.shapeCast(boxCollider, from, to, hitResult))
         {
             if(hitResult!=null)
             {
                if (this.Downhit.collider.collisionGroup == 1) {
                    isDownHit = true;
                }
             }
         }
 
         return isDownHit;
     }

    /**竖直方向射线检测*/
    get DownRayCheck():boolean{
        // var isDownHit = false;
        // var startPos:Vector3 = new Vector3(this.m_player.transform.position.x,this.m_player.transform.position.y+1,this.m_player.transform.position.z);
        // var direction:Vector3 = new Vector3(0,-1,0);
        // var DownRay:Laya.Ray = new Laya.Ray(startPos,direction);
        // var _shape
        // if(AssetManager.Instance.mainscene.physicsSimulation.rayCast(DownRay,this.Downhit,100)){
        //     if(this.Downhit!=null)
        //     {
        //         console.log("111111111111",this.Downhit.collider.owner.name)
        //     }
        //     if (this.Downhit.collider.collisionGroup == 1) {
        //         isDownHit = true;
        //     }
        // }
        // return isDownHit;
        var isDownHit = false;
        var boxCollider:Laya.BoxColliderShape = new Laya.BoxColliderShape(0.2, 0.2, 0.2);
        var from:Vector3 = new Vector3(this.m_player.transform.position.x,this.m_player.transform.position.y+0.5,this.m_player.transform.position.z);
        var dir=new Vector3(0,-100,0);
        var to:Vector3 = new Vector3(0,0,0);
        Vector3.add(from,dir,to);
        if (AssetManager.Instance.mainscene.physicsSimulation.shapeCast(boxCollider, from, to, this.Downhit))
        {
            if(this.Downhit!=null)
            {
                console.log("111111111111",this.Downhit.collider.owner.name)
               if (this.Downhit.collider.collisionGroup == 1 || this.Downhit.collider.collisionGroup == 100) {
                   isDownHit = true;
               }
            }
        }
        return isDownHit;
     }
     ForwardTarget:Laya.PhysicsComponent;
     /** 水平方向射线检测*/
     get FowardRayCheck():boolean{
         var isFowardHit = false;
         var startPos:Vector3 = new Vector3(this.m_player.transform.position.x,this.m_player.transform.position.y+0.5,this.m_player.transform.position.z+0.2);
        let foward = Transform3DHelper.getForward(this.m_player.transform);
        var FowardRay:Laya.Ray = new Laya.Ray(startPos,foward);
         if(AssetManager.Instance.mainscene.physicsSimulation.rayCast(FowardRay,this.FowardHit,1)){
             if(this.FowardHit){
                 console.log(this.FowardHit.collider.owner.name);
                 console.log("前方有物体");
             }
             if(this.FowardHit.collider.collisionGroup == 100){
                 console.log("射线打到npc了");
                 this.ForwardTarget = this.FowardHit.collider;
                 isFowardHit = true;
             }
         }
         return isFowardHit;
     }
     ChangeDeadState(){
        this.destroy();
     }
}