import { PlayerManager } from "../Manager/PlayerManager";
import {Vector3Helper} from "../GameUtils/Vector3Helper";
import Vector3 = Laya.Vector3;
import Sprite3D = Laya.Sprite3D;
import Transform3D = Laya.Transform3D;
import Quaternion = Laya.Quaternion;
import { MateManager } from "../Manager/MateManager";
import { Transform3DHelper, NodeUtil } from "../GameUtils/NodeUtil";
import { AssetManager } from "../Resources/AssetManager";
import { GameTools } from "../GameUtils/GameTools";
import { DispatcherManager, StageState, RoleState } from "../GameUtils/EventManager";
import WallBehav from "./WallBehav";
import { ResourcesPath } from "../Resources/ResourcesPath";
import { LevelManager } from "../Manager/LevelManager";
import CoinBehav from "./ConiBehav";
import { GameManager } from "../Manager/GameManager";
import { AudioManager, VoiceType, BGMType } from "../Manager/AudioManager";
import { WXSDK } from "../NetWork/WXSDK";
import { AccountManager } from "../NetWork/AccountManager";
import BossBehav from "./BossBehav";
import { AnimatorManager } from "../Manager/AnimatorManager";
import MateHpBarBehav from "./MateHpBarBehav";

export class MateMove extends Laya.Script3D{
    constructor(){
        super();
        DispatcherManager.StageOn(StageState.Start,this,this.GameStart)
    }
    public _gameobject:Laya.Sprite3D;
    private _transform:Transform3D;
    private _quaternion:Quaternion;
    private _animator:Laya.Animator;
    private _speed:number = 0.2;

    private testspeed:number = 0.5;

    //是否为头
    public Tag_Head:boolean = false;
    //游戏是否开始
    public start:boolean = false;
    //是否已死亡
    public isdead:boolean = false;
    //是否被玩家射线打到
    public geted:boolean = false;
    //到达mate点
    public _arriver:boolean = false;
    //到达终点
    public _destination:boolean = false;
    public curstate:RoleState;

    public GravityTime:number = 0;
    public ChargeSpeed:number = 0;
    public ChargeTime:number = 0;
    
    private Downhit:Laya.HitResult = new Laya.HitResult();
    private FowardHit:Laya.HitResult = new Laya.HitResult();

    private _forward = new Vector3(0,0,0);
    private _up = new Vector3(0,1,0);
    private _up2 = new Vector3(0,-1,0);

    private CanJump:boolean = false;
    private JumpTime:number = 0;
    private JumpSpeed:number = 0.3;
    private JumpSpeedUp:number = 1;

    public health:number;
    public maxhealth:number;

    public onAwake(){
        this._gameobject = this.owner as Laya.Sprite3D;
        this._transform = this._gameobject.transform;
        this._quaternion = new Quaternion();
        this._animator = this._gameobject.getComponent(Laya.Animator) as Laya.Animator;
        this.health = 50;
        this.maxhealth = 50;
    }
    targetPos:Vector3 = new Vector3(0,0,0);
    forW:Vector3 = new Vector3(0,0,0);
    public tarPathArr;

    public onUpdate(){
        if(!this.start)return;
        if(this.isdead){
            this.RoleDie();
            this.ChangeState(RoleState.Die);
            return
        }
        if(this._destination)return;
        if(this.isarrive)return;
        if(this.Tag_Head){
            this.RefreshUI();
            if(this.DownRayCheck){
                if(this.curstate == RoleState.OnLand){
                    this._gameobject.transform.position = new Vector3(this._gameobject.transform.position.x, this.Downhit.point.y, this._gameobject.transform.position.z);
                    if(this.Downhit.collider.owner.parent.name == "speed"){
                        AudioManager.PlayVoice(VoiceType.speedup);
                        this.SpeedUp(0.6,0.1);
                    }
                    else if(this.Downhit.collider.owner.parent.name == "Ramp"){
                        AudioManager.PlayVoice(VoiceType.fly)
                        this.SpeedUp(0.3,0.2);
                        this.Jump();
                    }
                }
                else if (this.curstate == RoleState.Sky) {
                    var distance = this._gameobject.transform.position.y - this.Downhit.point.y;
                    if (distance < 0.5) {
                        var leropos = new Vector3(this._gameobject.transform.position.x, this.Downhit.point.y, this._gameobject.transform.position.z);
                        Vector3.lerp(this._gameobject.transform.position, leropos, 0.3, leropos);
                        this._gameobject.transform.position = leropos;
                        this.ChangeState(RoleState.OnLand);
                    }
                }
            }
            else{
                this.ChangeState(RoleState.Sky);
            }
            if(this.FowardRayCheck){
    
            }
            let foward = Transform3DHelper.getForward(this._gameobject.transform);
            this.Move(foward);
            this.Gravity();
        }
        else{
            if(!this.geted)return;
            if(this._arriver){
                if(this.DownRayCheck){
                    if(this.curstate == RoleState.OnLand){
                        this._gameobject.transform.position = new Vector3(this._gameobject.transform.position.x, this.Downhit.point.y, this._gameobject.transform.position.z);
                    }
                    else if (this.curstate == RoleState.Sky) {
                        var distance = this._gameobject.transform.position.y - this.Downhit.point.y;
                        if (distance < 0.5) {
                            var leropos = new Vector3(this._gameobject.transform.position.x, this.Downhit.point.y, this._gameobject.transform.position.z);
                            Vector3.lerp(this._gameobject.transform.position, leropos, 0.3, leropos);
                            this._gameobject.transform.position = leropos;
                            this.ChangeState(RoleState.OnLand);
                        }
                    }
                }
                else{
                    // this.isdead = true;
                    this.ChangeState(RoleState.Sky);
                }
                this.RoleMoveFun();
                this.Gravity();
            }
            else{
                this.MoveToGroup();
            }
        }
        if(this._gameobject.transform.position.y<-15){
            this.FallDown();
            // this.isdead = true;
            // this.ChangeState(RoleState.Die);
        }
    }
    RefreshUI(){
        let pre = PlayerManager.Instance.RefreshPro();
        let count = this.RoleList.length;
        let data = {};
        data["pre"] = pre;
        data["count"] = count;
        DispatcherManager.StageTrigger(StageState.RefreshPro,[data]);
    }
    public targetmodel:Sprite3D;
    public GameStart(){
        this.start = true;
        if(this.Tag_Head)this.ChangeState(RoleState.OnLand);
        Laya.timer.loop(2,this,this.SetNewMovePoint,[this._transform])
    }
    public Start(model:Sprite3D){
        if(this.geted)return;
        this.targetmodel = model.getChildByName("followpos") as Laya.Sprite3D;
        this.geted = true;
        this.ChangeState(RoleState.OnLand);
    }
    ChangeState(state: RoleState) {
        if (this.curstate == state) return;
        this.curstate = state;
        switch (this.curstate) {
            case RoleState.Die:
                this.DIE();
                break;
            case RoleState.Sky:
                this.SKY();
                break;
            case RoleState.OnLand:
                this.ONLAND();
                break;
            case RoleState.Arriver:
                this.ARRIVER();
                break;
        }
    }
    ONLAND(){
        if(this.Tag_Head){
            this._animator.play("run");
            this._animator.speed = 2;
        }
        else{
            if(this.geted){
                this._animator.play("run");
                this._animator.speed = 2;
            }
            else{
                this._animator.play("idle");
            }
        }
    }
    SKY(){
        WXSDK.Shake();
        this._animator.play("sky");
    }
    ARRIVER(){
        this._destination = true;
        Laya.timer.clear(this,this.SetNewMovePoint);
        this._animator.play("win");
        if(this.Tag_Head){
            GameManager.Instance.GameWin();
            for (let i = 0; i < this.RoleList.length; i++) {
                let item = this.RoleList[i].getComponent(MateMove) as MateMove;
                if(item){
                    item.ARRIVER();
                }
            }
        }
    }
    DIE(){
        Laya.timer.clearAll(this);
        this._gameobject.active = false;
        if(this.Tag_Head && this.RoleList.length == 0){
            GameManager.Instance.GameLose();
        }
        this.destroy();
    }
    FallDown(){
        if(this.Tag_Head){
            Laya.timer.clearAll(this);
            GameManager.Instance.GameLose();
            AudioManager.PlayVoice(VoiceType.lose);
            WXSDK.Shake(false);
            this.DestroyProBar();
            this.destroy();
        }
    }
    //#region ZQ
    public RoleList:Sprite3D[]=[];
    private MovePointList:Vector3[]=[];
    private MoveRotateList:Vector3[]=[];
    private _foward:Vector3 = new Laya.Vector3(0,0,0);
    private _MovetarPos=new Laya.Vector3(0,0,0);

    public SetMoveTarger(_point: Vector3,_rotate:Vector3)  {
        // this._MovetarPos = _point;
        // this._gameobject.transform.position = this._MovetarPos.clone();
        // this._gameobject.transform.rotationEuler = _rotate.clone();
        if(this.pause)return;
        if(_point){
            this._MovetarPos = _point;
            this._gameobject.transform.position = this._MovetarPos.clone();
            this._gameobject.transform.rotationEuler = _rotate.clone();

            this.MovePointList.push(_point);
            if(this.MovePointList.length>100){
                this.MovePointList.shift();
            }

            this.MoveRotateList.push(_rotate);
            if(this.MoveRotateList.length>100){
                this.MoveRotateList.shift();
            }
        }

        
    }

    curpos2:Vector3 = new Vector3();
    offest2:Vector3 = new Vector3();
    curspeed2 = 0;


    private RoleMoveFun()
    {
        if(this.pause)return;
        Quaternion.lookAt(this._MovetarPos, this._transform.position, this._up, this._quaternion);
        this._quaternion.invert(this._quaternion);
        Quaternion.slerp(this._transform.rotation, this._quaternion, 0.1, this._quaternion);
        this._transform.rotation = this._quaternion.clone();
       
       let _rotationEuler= this._transform.rotationEuler.clone();
       _rotationEuler.x=0;
       _rotationEuler.z=0;
       this._transform.rotationEuler=_rotationEuler;
    }

    public SetNewMovePoint(_transform:Laya.Transform3D)
    {
        if (this.Tag_Head)  {
            var _movePos=_transform.position.clone();
            var _point=new Laya.Vector3(_movePos.x,_movePos.y,_movePos.z);
            var _rotate=_transform.rotationEuler.clone();

            this.MovePointList.push(_point);
            if (this.MovePointList.length > 200) {
                this.MovePointList.shift();
            }

            this.MoveRotateList.push(_rotate);
            if (this.MoveRotateList.length > 200) {
                this.MoveRotateList.shift();
            }

            for (let i = 0; i < this.RoleList.length; i++) {
                var _script = this.RoleList[i].getComponent(MateMove) as MateMove;
                if (_script) {
                    var _id=this.MovePointList.length - 1 - (i + 1) * 10;
                    _script.SetMoveTarger(this.MovePointList[_id],this.MoveRotateList[_id]);
                }
            }
        }
    }
    public RoleDie()
    {
        if(this.RoleList.length==0)
        {
            return;
        }
        var _newHand=this.RoleList[0];
        var _script=_newHand.getComponent(MateMove) as MateMove;
        if(_script){
            _script._gameobject.transform.rotationEuler = this._gameobject.transform.rotationEuler.clone();
            _script.Tag_Head=true;
            PlayerManager.Instance.player = _script._gameobject;
            _script.RoleList=[];
            for (let i = 1; i < this.RoleList.length; i++) 
            {
               _script.RoleList.push(this.RoleList[i]);
            }
        }
    }
    //#endregion

    //向队伍尾巴移动
    public MoveToGroup(){
        this.targetPos = this.targetmodel.transform.position;

        let tarPos = this.targetPos;
        let speed = 0.05;
        Vector3.subtract(tarPos, this._transform.position, this._forward);
        Vector3.normalize(this._forward, this._forward);
        this._forward = new Vector3(this._forward.x * speed, this._forward.y * speed, this._forward.z * speed);
        this._transform.translate(this._forward, false);
        if (Vector3.distance(this._transform.position, tarPos) < 0.1) {
            this.ArriverTargetPos();
            return;
        }
        Quaternion.lookAt(tarPos, this._transform.position, this._up, this._quaternion);
        this._quaternion.invert(this._quaternion);
        Quaternion.slerp(this._transform.rotation, this._quaternion, 0.1, this._quaternion);
        this._transform.rotation = this._quaternion.clone();
    }
    curpos:Vector3 = new Vector3();
    offest:Vector3 = new Vector3();
    curspeed = 0;

    Move(_dir: Vector3) {

        if(this.pause)return;
        let speed = this.testspeed;
        if (this.ChargeTime > 0) {
            this.ChargeTime -= 0.01;
            speed = speed + this.ChargeSpeed;
        }
        else  {
            this.ChargeSpeed = GameTools.Instance.lerp(this.ChargeSpeed, 0, 0.1);
            speed = speed + this.ChargeSpeed;
            this.ChargeTime = 0;
        }
        this.curspeed = GameTools.Instance.lerp(this.curspeed, speed, 0.1);
        Vector3.scale(_dir, this.curspeed, this.offest);
        Vector3.add(this._gameobject.transform.position, this.offest, this.curpos);
        Vector3.lerp(this._gameobject.transform.position, this.curpos, 0.15, this.curpos);

        if(this.CanJump){
            this.JumpTime += 0.8;
            var Height = this.JumpSpeed *this.JumpTime*1.9 - (this.JumpTime * this.JumpTime*this.JumpSpeedUp);
            if(Height > 0)
            {
                this.curpos = new Vector3(this.curpos.x,this.curpos.y + Height,this.curpos.z);
            }
            else
            {
                this.CanJump = false;
            }
            this.curpos = new Vector3(this.curpos.x, this.curpos.y+0.8, this.curpos.z);
            this._gameobject.transform.position = this.curpos;
        }
        else{
            this.curpos = new Vector3(this.curpos.x, this.curpos.y, this.curpos.z);
            this._gameobject.transform.position = this.curpos;
        }
        
    }
    public ArriverTargetPos(){
        this._gameobject.transform.position = this.targetmodel.transform.position.clone();
        this._gameobject.transform.rotationEuler = this.targetmodel.transform.rotationEuler.clone();
        this._arriver = true;
    }
    /**竖直方向射线检测*/
    get DownRayCheck():boolean{
        var startPos:Vector3 = new Vector3(this._gameobject.transform.position.x,this._gameobject.transform.position.y+0.1,this._gameobject.transform.position.z);
        let foward = new Laya.Vector3(0,-1,0);
        var FowardRay:Laya.Ray = new Laya.Ray(startPos,foward);
        var isDownHit = false;
        if (AssetManager.Instance.mainscene.physicsSimulation.rayCast(FowardRay,this.Downhit,1))
        {
            if(this.Downhit!=null)
            {
               if (this.Downhit.collider.collisionGroup == 1 ) {
                   isDownHit = true;
               }
            }
        }
        return isDownHit;
     }
     ForwardTarget:Laya.PhysicsComponent;
     /** 水平方向射线检测*/
     get FowardRayCheck():boolean{
        var isFowardHit = false;
        var startPos:Vector3 = new Vector3(this._gameobject.transform.position.x,this._gameobject.transform.position.y+0.1,this._gameobject.transform.position.z);
        let foward = Transform3DHelper.getForward(this._gameobject.transform);
        var FowardRay:Laya.Ray = new Laya.Ray(startPos,foward);
         if(AssetManager.Instance.mainscene.physicsSimulation.rayCast(FowardRay,this.FowardHit,0.1)){
             if(this.FowardHit){
                //  console.log(this.FowardHit.collider.owner.parent.name);
                //  console.log("前方有物体");
             }
             if(this.FowardHit.collider.collisionGroup == 100){//Npc
                //  console.log("射线打到npc了");
                 this.ForwardTarget = this.FowardHit.collider;
                 isFowardHit = true;
                 let _mate = this.FowardHit.collider.owner.parent.getComponent(MateMove) as MateMove;
                if(_mate){
                    let _model;
                    if(this.RoleList.length>0){
                        _model = this.RoleList[this.RoleList.length-1];
                    }
                    else{
                        _model = this._gameobject;
                    }
                   _mate.Start(_model);
                   let target = this.FowardHit.collider.owner.parent as Laya.Sprite3D;
                   if(!this.JudgeEqual(target)){
                    this.RoleList.push(target);
                   }
                }
                AudioManager.PlayVoice(VoiceType.crashrole);
                WXSDK.Shake();
             }
             else if(this.FowardHit.collider.collisionGroup == 2){//墙
                this.isdead = true;
                let _wall = this.FowardHit.collider.owner.parent as Laya.Sprite3D; 
                let _behav = _wall.getComponent(WallBehav) as WallBehav;
                if(_behav){
                    _behav.Crash();
                }
                else{
                    _behav = _wall.addComponent(WallBehav) as WallBehav;
                    _behav.crash = true;
                }
                AudioManager.PlayVoice(VoiceType.crashwall);
                WXSDK.Shake();
             }
             else if(this.FowardHit.collider.collisionGroup == 3){//道具
                this.Crash();
             }
             else if(this.FowardHit.collider.collisionGroup == 4){//引导
                let stage = Number(AccountManager.Instance.curplayerData.GainAccount("stage"))+1;
                if(stage == 1){
                    if(this.FowardHit.collider.owner.parent.name == "roleguide"){
                        if(!this.showguide1){
                            this.showguide1 = true;
                            DispatcherManager.StageTrigger(StageState.ShowGuide1);
                        }
                    }
                    else if(this.FowardHit.collider.owner.parent.name == "rotateguide"){
                        if(!this.showguide2){
                            this.showguide2 = true;
                            DispatcherManager.StageTrigger(StageState.ShowGuide2);
                        }
                    }
                }
             }
             else if(this.FowardHit.collider.collisionGroup == 200){//终点
                // let _endpoint = this.FowardHit.collider.owner.parent as Laya.Sprite3D;
                // this.ChangeState(RoleState.Arriver);
                // AudioManager.PlayVoice(VoiceType.win);
                // WXSDK.Shake(false);
                // let _winpre = AssetManager.Instance.getPref(ResourcesPath.YanHua);
                // let _model = Laya.Sprite3D.instantiate(_winpre);
                // LevelManager.Instance.spritepool.addChild(_model);
                // _model.transform.position = _endpoint.transform.position.clone();
             }
             else if(this.FowardHit.collider.collisionGroup == 400){//终点攻击点
                let atkpos_z = (this.FowardHit.collider.owner.parent as Laya.Sprite3D).transform.position.clone();
                if(!this.isarrive){
                    this.isarrive = true;
                    this.NearAtkPos(this.RoleList.length+1,atkpos_z.z);
                }
             }
         }
         return isFowardHit;
     }

     //到达终点攻击点
    public isarrive:boolean = false;

     showguide1 = false;
     showguide2 = false;

     /**碰到道具 */
     Crash(){
         let target = this.FowardHit.collider.owner.parent;
         switch (target.name) {
             case "Ramp":
                AudioManager.PlayVoice(VoiceType.fly);
                 this.SpeedUp(0.8, 0.3);
                 this.Jump();
                 break;
             case "Coin":
                 let _behav = target.getComponent(CoinBehav) as CoinBehav;
                 if (_behav) {
                     _behav.OnTouch();
                 }
                 target.destroy();
                 break;
         }
     }
     
     /**重力效果*/
     Gravity(){
        if(this.curstate == RoleState.Sky ){
            if(this.ChargeSpeed>0.001){
                this.GravityTime += 0.008;
            }
            else {
               this.GravityTime += 0.010;
            }
        var hight = this.GravityTime * this.GravityTime;
        this._gameobject.transform.position = new Vector3(this._gameobject.transform.position.x,this._gameobject.transform.position.y-hight,this._gameobject.transform.position.z+hight/2);
       }
     }
     Jump(){
        this.CanJump = true;
        this.JumpTime = 0;
     }
     /**加速 */
    SpeedUp(_speed:number,_time:number){
        if(this.ChargeTime<_time){
            this.ChargeTime += 0.1;
        }
        if(this.ChargeSpeed<_speed){
            this.ChargeSpeed += 0.2;
        }
    }

    pause = false;
    Pause(){
        this._animator.play("idle");
        this.testspeed = 0;
        this.pause = true;

        for (let i = 0; i < this.RoleList.length; i++) {
            let _role = this.RoleList[i].getComponent(MateMove) as MateMove;
            if(_role){
                _role.Pause();
            }
        }
    }
    Resume(){
        this.pause = false;
        this._animator.play("run");
        this.testspeed = 0.5;
        
        for (let i = 0; i < this.RoleList.length; i++) {
            let _role = this.RoleList[i].getComponent(MateMove) as MateMove;
            if(_role){
                _role.Resume();
            }
        }
    }
     JudgeEqual(_model:Laya.Sprite3D){
         for (let i = 0; i < this.RoleList.length; i++) {
             let item = this.RoleList[i];
             if(item == _model){
                return true
             }
         }
         return false;
     }

     
     SetRoleAtk(){
         for (let i = 0; i < this.RoleList.length; i++) {
             let item = this.RoleList[i].getComponent(MateMove) as MateMove;
             item.isarrive = true;
             item.pause = true;
         }
     }
     MoveToAttackPoint(){
        Vector3.subtract(this.tp, this._gameobject.transform.position, this.forW);
        Vector3.normalize(this.forW, this.forW);
        this.forW = new Vector3(this.forW.x * 0.15, this.forW.y * 0.15, this.forW.z * 0.15);
        this._gameobject.transform.translate(this.forW, false);
        let _dis = Vector3.distance(this._gameobject.transform.position, this.tp);
        if (Vector3.distance(this._gameobject.transform.position, this.tp) < 1) {
            Laya.timer.clear(this, this.MoveToAttackPoint);
            this._animator.play("atk");
            return;
        }
        Quaternion.lookAt(this.tp, this._gameobject.transform.position, this._up, this.quaternion1);
        this.quaternion1.invert(this.quaternion1);
        this._gameobject.transform.rotation = this.quaternion1.clone();

        let _rotationEuler= this._transform.rotationEuler.clone();
        _rotationEuler.x=0;
        _rotationEuler.z=0;
        this._transform.rotationEuler=_rotationEuler;
     }
     SetBossRotate(){
        Laya.timer.frameLoop(1,this,this._SetBossRotate);
     }
     _SetBossRotate(){
        if(this.Tag_Head){
            let _boss = AssetManager.Instance.Boss.getComponent(BossBehav) as BossBehav;
            if(_boss){
                _boss.SetTarRotate(this._gameobject);
            }
        }
     }
     NearAtkPos(_count:number,_zpos:number){
        this.tp = AssetManager.Instance.Boss.transform.position.clone();
        Laya.timer.clear(this,this.SetNewMovePoint);
        Laya.timer.frameLoop(1,this,this.MoveToAttackPoint);
        this.SetRoleAtk();
        this.SetBossRotate();
        this.pause = true;
        if (_count == 1)return;
        this.v3.z = _zpos;

        var _dis = Math.abs(Number(AssetManager.Instance.leftpos.x) - Number(AssetManager.Instance.rightpos.x));
        var _interval = Math.floor(_dis/(_count-1));
        var mubiaodian = AssetManager.Instance.endground.transform.position.clone();

        var jiange = false;
        var index = 1;
        var num = 0;

        this.v3.x = mubiaodian.x;
        this.Defense(this.v3.clone());

        for (let i = 0; i < this.RoleList.length; i++) {
            num ++;
            const element = this.RoleList[i].getComponent(MateMove) as MateMove;
            const element2 = this.RoleList[i].getComponent(MateMove) as MateMove;
            if( num > 2 ){
                index ++;
                num = 1;
            } 
            jiange = !jiange;
            if(jiange){
                this.v3.x = mubiaodian.x - 1 * index;
            }
            else{
                this.v3.x = mubiaodian.x + 1 * index;
            }
            element.Defense(this.v3.clone());
            element.SetBossRotate();
        }
        DispatcherManager.StageTrigger(StageState.ArriverAttack);
     }
     
     ArriverAtkPos(){
        Laya.timer.clear(this, this._Attack);
        let BossTran = AssetManager.Instance.curstage.getChildByName("Boss") as Laya.Sprite3D;
        let BossPos = BossTran.transform.position.clone();
        Quaternion.lookAt(BossPos, this._gameobject.transform.position, this._up, this.quaternion2);
        this.quaternion2.invert(this.quaternion2);
        this._gameobject.transform.rotation = this.quaternion2.clone();

        let _rotationEuler= this._transform.rotationEuler.clone();
       _rotationEuler.x=0;
       _rotationEuler.z=0;
       this._transform.rotationEuler=_rotationEuler;
        Laya.timer.loop(1,this,this.PlayAtkAni);

        let tween = Laya.Tween.to(this._gameobject.transform, { RotationEulerY: 0 }, 100, Laya.Ease.quintIn, Laya.Handler.create(this, () => {
            tween.clear();
        }, null, false));
        return;
     }

    play = false;
    targetspr:Laya.Sprite3D;
    PlayAtkAni(){
        if(!this.play){
            this.play = true;
            AnimatorManager.Play(true,this._animator,"atk",false,Laya.Handler.create(this,()=>{
                let BossTran = AssetManager.Instance.curstage.getChildByName("Boss") as Laya.Sprite3D;
                let _behav = BossTran.getComponent(BossBehav) as BossBehav;
                if(_behav){
                    _behav.Injured(10);
                }
                this.play = false;
            }))
        }
    }

    private v3 = new Vector3(0,0,0);
    private ts = new Vector3(0, 0, 0.01);
    private tp: Vector3;
    private quaternion1:Laya.Quaternion = new Laya.Quaternion();
    private quaternion2:Laya.Quaternion = new Laya.Quaternion();
    private quaternion3:Laya.Quaternion = new Laya.Quaternion();
    _Defense() {
        Vector3.subtract(this.tp, this._gameobject.transform.position, this.forW);
        Vector3.normalize(this.forW, this.forW);
        this.forW = new Vector3(this.forW.x * 0.15, this.forW.y * 0.15, this.forW.z * 0.15);
        this._gameobject.transform.translate(this.forW, false);
        let _dis = Vector3.distance(this._gameobject.transform.position, this.tp);
        if (Vector3.distance(this._gameobject.transform.position, this.tp) < 0.5) {
            // this.ArriverAtkPos();
            Laya.timer.clear(this, this._Defense);
            this.Attack(AssetManager.Instance.Boss.transform.position.clone());
            return;
        }
        Quaternion.lookAt(this.tp, this._gameobject.transform.position, this._up, this.quaternion1);
        this.quaternion1.invert(this.quaternion1);
        this._gameobject.transform.rotation = this.quaternion1.clone();

        let _rotationEuler= this._transform.rotationEuler.clone();
       _rotationEuler.x=0;
       _rotationEuler.z=0;
       this._transform.rotationEuler=_rotationEuler;
    }
    Defense(pos) {
        this.tp = pos;
        Quaternion.lookAt(pos, this._gameobject.transform.position, this._up, this.quaternion1);
        this.quaternion1.invert(this.quaternion1);
        this._gameobject.transform.rotation = this.quaternion1.clone();
        Laya.timer.frameLoop(1, this, this._Defense);
    }
    Attack(pos){
        this.tp = pos;
        Quaternion.lookAt(pos, this._gameobject.transform.position, this._up2, this.quaternion3);
        this.quaternion3.invert(this.quaternion3);
        this._gameobject.transform.rotation = this.quaternion3.clone();
        Laya.timer.frameLoop(1, this, this._Attack);
    }
    _Attack(){
        Vector3.subtract(this.tp, this._gameobject.transform.position, this.forW);
        Vector3.normalize(this.forW, this.forW);
        this.forW = new Vector3(this.forW.x * 0.05, this.forW.y * 0.05, this.forW.z * 0.05);
        this._gameobject.transform.translate(this.forW, false);
        let _dis = Vector3.distance(this._gameobject.transform.position, this.tp);
        if (Vector3.distance(this._gameobject.transform.position, this.tp) < 0.5) {
            this.ArriverAtkPos();
            // Laya.timer.clear(this, this._Attack);
            return;
        }
        Quaternion.lookAt(this.tp, this._gameobject.transform.position, this._up2, this.quaternion3);
        this.quaternion3.invert(this.quaternion3);
        this._gameobject.transform.rotation = this.quaternion3.clone();

        let _rotationEuler= this._transform.rotationEuler.clone();
       _rotationEuler.x=0;
       _rotationEuler.z=0;
       this._transform.rotationEuler=_rotationEuler;
    }
    Killed(){
        if(this.isdead)return
        AnimatorManager.Play(false,this._animator, "die2", false, Laya.Handler.create(this,()=>{
            this.isdead = true;
        }));
    }
    showpro = false;
    Injured(_demage:number){
        if(this.health == 0)return;
        if(!this.showpro){
            this.showpro = true;
            this.AddProBar();
        }
        this.health -= _demage;
        if(this.health<=0){
            this.health = 0;
            this.Killed();
        }
    }
    AddProBar(){
        let probar = this._gameobject.addComponent(MateHpBarBehav) as MateHpBarBehav;
    }
    DestroyProBar(){
        let probar = this._gameobject.getComponent(MateHpBarBehav) as MateHpBarBehav;
        if(probar){
            probar.destroy();
        }
    }
}