

import Sprite3D = Laya.Sprite3D;
import Transform3D = Laya.Transform3D;
import Vector3 = Laya.Vector3;
// import { AnimatorClip } from "./ColliderAndMusicType";
import { Mathf } from "../Util/Mathf";
import CameraManager from "../Manager/CameraManager";
import { GlobalVariable } from "../game_module/GlobalVariable";
import { Scene3DManager } from "./Scene3DManager";
import Quaternion = Laya.Quaternion;
import MateManager from "../Manager/MateManager";
import { GamePool } from "./GamePool";
import { MainRoleShot } from "./MainRoleShot";
import MainRoleManager from "../Manager/MainRoleManager";
import { DispatcherManager, EventType } from "./DispatcherManager";
import { ParticleManager } from "./ParticleManager";
import RoleManager from "../Manager/RoleManager";
import { PlayerManager } from "./PlayerManager";
import GameManager from "./GameManager";
import { MusicType } from "./ColliderAndMusicType";
export default class Mate extends Laya.Script3D {
    public selfobj: Sprite3D;
    public tran: Transform3D;
    public animator: Laya.Animator;
    public forW: Vector3;
    private quaternion1: Quaternion;
    private rayPoint: Transform3D;
    private bulletPos: Transform3D;
    private speed: number;
    private _up = new Vector3(0, 1, 0);
    onAwake() {
        this.selfobj = this.owner as Sprite3D;
        this.tran = this.selfobj.transform;
        this.animator = this.selfobj.getChildAt(0).getComponent(Laya.Animator);
        this.rayPoint = (this.selfobj.getChildByName("rayPoint") as Sprite3D).transform;
        this.bulletPos = (this.selfobj.getChildByName("bulletPos") as Sprite3D).transform;
        this.forW = Mathf.VecZero;
        this.speed = GlobalVariable.friendspeed;
        this.quaternion1 = new Quaternion();
    }

    private dataPar: any;
    private tar: Sprite3D;
    private tarTra: Transform3D;
    SetTar() {
        this.isreturn = false;
        this.isClickCom = true;
        this.isStop = true;
        this.isover = false;
        // this.dataPar = tar;
        // this.dataPar["ishave"] = true;
        // this.tar = this.dataPar["sp"];
        // this.tarTra = this.tar.transform;
       // DispatcherManager.off(EventType.Event.StartShot, this, this.CreateBullet);
       this.PlayAni(MusicType.Idle,1);
        DispatcherManager.off(EventType.Event.StopShot, this, this.StopCreate);
    }


    TryAddFriend(dataPar) {
        this.PlayAni(MusicType.Idle, 1);
        this.dataPar = dataPar;
        this.dataPar["ishave"] = true;
        this.isClickCom = true;
        this.isStop = true;
        this.isreturn = true;
        this.isover=false;
        MateManager.Instance.AddGetDic(this.selfobj.id, this);
        this.BulletHandler();
        if (MainRoleManager.Instance.IsBulleting()) this.CreateBullet();
        Laya.timer.frameLoop(1, this, this.Move);
    }

    /**
     * 落地
     */
    TweenFall() {
        let tween = Laya.Tween.to(this.tran, { localPositionY: 0 }, 400, Laya.Ease.quintIn, Laya.Handler.create(this, () => {
            let dataPar = MainRoleManager.Instance.GetFrindPar();
            if (!dataPar) { console.warn("friendPar没有位置了"); return; }
            this.dataPar = dataPar;
            this.dataPar["ishave"] = true;
            this.tar = this.dataPar["sp"];
            this.tarTra = this.tar.transform;
            //this.tran.position
            tween.clear();
            this.StartMove();
        }, null, false));
    }

    private isreturn = false;
    private isover = false;
    StartMove() {
        this.PlayAni(MusicType.RunAndGun, 2);
        this.isreturn = false;
        this.isClickCom = true;
        this.isStop = true;
        this.isover = false;
        Laya.timer.frameLoop(1, this, this.Move);
    }
    _createRay() {
        this.CreateRay();
    }
    Move() {
        //if(GameManager.Instance.gameisover)return;
        if (this.isreturn) {
            this._createRay(); return;
        }
        let tarPos = this.tarTra.position.clone();
        Vector3.subtract(tarPos, this.tran.position, this.forW);
        Vector3.normalize(this.forW, this.forW);
        this.forW = new Vector3(this.forW.x * this.speed, this.forW.y * this.speed, this.forW.z * this.speed);
        this.tran.translate(this.forW, false);
        if (Vector3.distance(this.tran.position, tarPos) < 0.1) {
            this.ArriveTarPos();
            return;
        }
        Quaternion.lookAt(tarPos, this.tran.position, this._up, this.quaternion1);
        this.quaternion1.invert(this.quaternion1);
        Quaternion.slerp(this.tran.rotation, this.quaternion1, 0.1, this.quaternion1);
        this.tran.rotation = this.quaternion1.clone();
    }
    ArriveTarPos() {
        //if(GameManager.Instance.gameisover)return;
        this.isClickCom = true;
        this.isStop = true;
        this.isreturn = true;
        this.tar.addChild(this.selfobj);
        this.tran.localPosition = Mathf.VecZero;
        this.PlayAni(MusicType.RunAndGun, 2);
        MateManager.Instance.AddGetDic(this.selfobj.id, this);
        let tween = Laya.Tween.to(this.tran, { localRotationEulerY: 0 }, 100, Laya.Ease.quintIn, Laya.Handler.create(this, () => {
            tween.clear();
            this.tran.localRotationEuler = Mathf.VecZero;
            this.BulletHandler();
           // if (MainRoleManager.Instance.IsBulleting()) 
            this.CreateBullet();
        }, null, false));
    }

    private _scene;
    private rayF: Laya.Ray;
    private outHitF: Laya.HitResult;
    private rayL: Laya.Ray;
    private outHitL: Laya.HitResult;
    private rayR: Laya.Ray;
    private outHitR: Laya.HitResult;
    private CreateRay() {
        //if(GameManager.Instance.gameisover)return;
        let origP = this.rayPoint.position;
        if (!this._scene) {
            this._scene = Scene3DManager.Instance._mainscene;
        }

        //前面的射线
        if (!this.rayF) {
            this.rayF = new Laya.Ray(Mathf.VecZero, new Vector3(0, 0, 1));
            this.outHitF = new Laya.HitResult();
        }
        this.rayF.origin = origP;
        let isHItF = this._scene.physicsSimulation.rayCast(this.rayF, this.outHitF, GlobalVariable.playerRayLengthF);
        if (isHItF) {
            let obj = this.outHitF.collider.owner as Sprite3D;
            //console.log("前面的射线碰撞");
            this.Died(this.outHitF.point);
            return;
        }
        //左边的射线
        if (!this.rayL) {
            this.rayL = new Laya.Ray(Mathf.VecZero, new Vector3(1, 0, 0));
            this.outHitL = new Laya.HitResult();
        }
        this.rayL.origin = origP;
        let isHItL = this._scene.physicsSimulation.rayCast(this.rayL, this.outHitL, GlobalVariable.playerRayLengthLR);
        if (isHItL) {
            let obj = this.outHitL.collider.owner as Sprite3D;
            //console.log("左边的射线碰撞")
            this.Died(this.outHitL.point);
            return;
        }
        //右边的射线
        if (!this.rayR) {
            this.rayR = new Laya.Ray(Mathf.VecZero, new Vector3(-1, 0, 0));
            this.outHitR = new Laya.HitResult();
        }
        this.rayR.origin = origP;
        let isHItR = this._scene.physicsSimulation.rayCast(this.rayR, this.outHitR, GlobalVariable.playerRayLengthLR);
        if (isHItR) {
            let obj = this.outHitR.collider.owner as Sprite3D;
            //console.log("右边的射线碰撞")
            this.Died(this.outHitR.point);
            return;
        }
    }

    public PlayAni(clip: string, speed, callback?) {
        this.animator.play(clip);
        if (!speed) speed = 1;
        this.animator.speed = speed;
       
    }
    BulletHandler() {
      //  DispatcherManager.on(EventType.Event.StartShot, this, this.CreateBullet);
        DispatcherManager.on(EventType.Event.StopShot, this, this.StopCreate);
    }
    /**
     * 创建子弹
     */
    public CreateBullet() {
        this.isStop = false;
        if (!this.isClickCom) return;
        this.isClickCom = false;
        this._CreateBullet();
        Laya.timer.loop(PlayerManager.bulletTime * 2 * 1000, this, this.LoopCreate);

    }
    LoopCreate() {
        //if(GameManager.Instance.gameisover)return;
        if (this.isStop) {
            Laya.timer.clear(this, this.LoopCreate);
            this.isClickCom = true;
            return;
        }
        this._CreateBullet();
    }
    private _CreateBullet() {
        //  let bulletPos = this.playerBehavior.CreateBullet();
        let bu = GamePool.Instance.SetActiveObject(GamePool.pooltype.PlayerBullet);
        if (bu) {
            Scene3DManager.Instance.bulletCon.addChild(bu);
            bu.transform.position = this.bulletPos.position.clone();
            bu.transform.localRotationEuler = this.bulletPos.rotationEuler.clone();
            let pb: MainRoleShot = bu.getComponent(MainRoleShot);
            if (!pb) pb = bu.addComponent(MainRoleShot) as MainRoleShot;
            pb.StartMove(this.bulletPos, this.isover);
        }
    }
    /**
     * 停止创建子弹
     */
    private isClickCom = true;
    private isStop = true;
    public StopCreate() {
        this.isStop = true;
    }

    Died(pos) {
        //if(GameManager.Instance.gameisover)return;
        if (this.dataPar)
            this.dataPar["ishave"] = false;
        ParticleManager.Instance.CreateXueEffect(pos);
        MateManager.Instance.RemoveGet(this.selfobj);
        this.Des();
        MainRoleManager.Instance.CheackGame();
    }

    SetPar() {
        let t = this.tran.position.clone();
        MainRoleManager.Instance.playerContainer.addChild(this.selfobj);
        this.tran.position = t.clone();
        //console.log(this.selfobj.id);
    }
    ToStand(pos) {
        //  Laya.timer.clearAll(this);
        // let t = this.tran.position.clone();
        // MainRoleManager.Instance.playerContainer.addChild(this.selfobj);
        // this.tran.position = t;
        this.SetPar();
        this.tp = pos;
        Quaternion.lookAt(pos, this.tran.position, this._up, this.quaternion1);
        this.quaternion1.invert(this.quaternion1);
        this.tran.rotation = this.quaternion1.clone();
        this.PlayAni(MusicType.CrouchFiring, 1);
        Laya.timer.frameLoop(1, this, this._TOSTAND);
    }
    private ts = new Vector3(0, 0, 0.01);
    private tp: Vector3;
    _TOSTAND() {
        Vector3.subtract(this.tp, this.tran.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.tran.translate(this.forW, false);
        if (Vector3.distance(this.tran.position, this.tp) < 0.1) {
            Laya.timer.clear(this, this._TOSTAND);
            let tween = Laya.Tween.to(this.tran, { localRotationEulerY: 0 }, 100, Laya.Ease.quintIn, Laya.Handler.create(this, () => {
                tween.clear();
            }, null, false));
            return;
        }
        Quaternion.lookAt(this.tp, this.tran.position, this._up, this.quaternion1);
        this.quaternion1.invert(this.quaternion1);
        this.tran.rotation = this.quaternion1.clone();
    }


    private r;
    private danceP: Vector3;
    Dance(sp: Transform3D) {

        this.PlayAni(MusicType.RunAndGun, 2);
        this.danceP = sp.position.clone();
        this.r = sp.rotationEuler.clone();
        Laya.timer.frameLoop(1, this, this._Dance);


    }
    _Dance() {
        Vector3.subtract(this.danceP, this.tran.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.tran.translate(this.forW);
        if (Vector3.distance(this.tran.position, this.danceP) < 0.15) {
            //console.log("this.tp", this.danceP);
            Laya.timer.clear(this, this._Dance);
            this.tran.position = this.danceP.clone();
            this.tran.rotationEuler = this.r.clone();
            this.PlayAni(MusicType.Dance, 1);
            return;
        }
        Quaternion.lookAt(this.danceP, this.tran.position, this._up, this.quaternion1);
        this.quaternion1.invert(this.quaternion1);
        //Quaternion.slerp(this.tran.rotation, this.quaternion1, 0.1, this.quaternion1);
        this.tran.rotation = this.quaternion1.clone();
    }


    private enemyTar: Sprite3D;
    StartAtkEnemy() {
        Laya.timer.clear(this, this._TOSTAND);
        this.isover = true;
        this.isFristShoot = true;
        this.enemyTar = null;
        Laya.timer.frameLoop(1, this, this._atkEnemy)
    }
    private isFristShoot = true;
    private ATKp: Vector3;
    _atkEnemy() {
        //if(GameManager.Instance.gameisover)return;
        if (this.enemyTar == null) {
            this.enemyTar = RoleManager.Instance.PlayerGetEnemy();
            // if (this.enemyTar == null) return;
            // if (!this.enemyTar.active) this.enemyTar = RoleManager.Instance.PlayerGetEnemy();
            if (this.enemyTar != null) {
                this.ATKp = this.enemyTar.transform.position.clone();
                Quaternion.lookAt(this.ATKp, this.tran.position, this._up, this.quaternion1);
                this.quaternion1.invert(this.quaternion1);
                this.tran.rotation = this.quaternion1.clone();
            }
        } else {
            if (!this.enemyTar.active) {
                this.enemyTar = RoleManager.Instance.PlayerGetEnemy();
                if (this.enemyTar != null) {
                    this.ATKp = this.enemyTar.transform.position.clone();
                    Quaternion.lookAt(this.ATKp, this.tran.position, this._up, this.quaternion1);
                    this.quaternion1.invert(this.quaternion1);
                    this.tran.rotation = this.quaternion1.clone();
                }
            }
            if (this.enemyTar == null) {
                this.StopCreate();
                this.isFristShoot = true;
                return;
            }
            if (this.isFristShoot) {
                this.isFristShoot = false;

                this.CreateBullet();
            }

            this.ATKp = this.enemyTar.transform.position.clone();
            Quaternion.lookAt(this.ATKp, this.tran.position, this._up, this.quaternion1);
            this.quaternion1.invert(this.quaternion1);
            this.tran.rotation = this.quaternion1.clone();
        }
    }



    Des() {
        //this.dataPar["ishave"] = false;
        if(this.dataPar){
            this.dataPar["ishave"] = false;
            this.dataPar = null;
        }
       
        MateManager.Instance.RemoveFriend(this.selfobj);
        MateManager.Instance.RemoveGet(this.selfobj);
      
        Laya.timer.clearAll(this);
        Laya.Tween.clearAll(this);
       // DispatcherManager.off(EventType.Event.StartShot, this, this.CreateBullet);
        DispatcherManager.off(EventType.Event.StopShot, this, this.StopCreate);
        GamePool.Instance.SetDisableObject(this.selfobj);
    }

    onDestroy() {

    }
}