

import Sprite3D = Laya.Sprite3D;
import Transform3D = Laya.Transform3D;
import Vector3 = Laya.Vector3;
import {  MusicType } from "./ColliderAndMusicType";
import { Mathf } from "../Util/Mathf";
import CameraManager from "../Manager/CameraManager";
import { GlobalVariable } from "../game_module/GlobalVariable";
import { Scene3DManager } from "./Scene3DManager";
import ConstructManager from "../Manager/ConstructManager";
import PlaneManager from "../Manager/PlaneManager";
import StreetManager from "../Manager/StreetManager";
import Quaternion = Laya.Quaternion;
import { ParticleManager } from "./ParticleManager";
import MainRoleManager from "../Manager/MainRoleManager";
import RoleManager from "../Manager/RoleManager";
export default class MainRole extends Laya.Script3D {
    public selfobj: Sprite3D;
    public tran: Transform3D;
    public animator: Laya.Animator;
    public _speed: Vector3;
    private rayPoint: Transform3D;
    private bulletPos: Transform3D;
    public body: Sprite3D;
    public forW: Vector3;
    private quaternion1: Quaternion;
    onAwake() {
        this.selfobj = this.owner as Sprite3D;
        this.tran = this.selfobj.transform;
        this.body = this.selfobj.getChildAt(0) as Sprite3D;
        this.animator = this.body.getComponent(Laya.Animator);
        this.rayPoint = (this.selfobj.getChildByName("rayPoint") as Sprite3D).transform;
        this.bulletPos = (this.selfobj.getChildByName("bulletPos") as Sprite3D).transform;
        this._speed = Mathf.VecZero;
        this._speed.z = GlobalVariable.playerspeed;
        this.forW = Mathf.VecZero;
        this.quaternion1 = new Quaternion();
    }

    private isarrive = false;
    public isdead = false;
    StartMove() {
        this.isdead = false;
        this.isarrive = false;
        Laya.timer.frameLoop(1, this, this.Move);
    }
    Move() {
        if (this.isarrive) {
            if (this.isdead) return;
            this.CreateRay(); return;
        }
        this.tran.translate(this._speed);
        let pz = this.tran.localPositionZ;
        ConstructManager.Instance.CheckPlayerRoadPos(pz);
        PlaneManager.Instance.CheckPlayerRoadPos(pz);
        StreetManager.Instance.CheckPlayerRoadPos(pz);
        CameraManager.Instance.CameraMoveIngame(pz);
        if (this.isdead) return;
        this.CreateRay();
    }

    Arrive() {
        this.isarrive = true;
        //Laya.timer.clear(this, this.Move);
    }

    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 (this.isdead) 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);
        //this.animator.play(clip);
        if (!speed) speed = 1;
        this.animator.speed = speed;
    }
    Slider(x) {
        //if (this.isdead) return;
        this.tran.localPositionX += x;
        let px = this.tran.localPositionX;
        let abs = Math.abs(px);
        if (abs > GlobalVariable.playerMoveMaxX) {
            if (px > 0) this.tran.localPositionX = GlobalVariable.playerMoveMaxX;
            else this.tran.localPositionX = -GlobalVariable.playerMoveMaxX
        }
    }

    CreateBullet() {
        if (this.isdead) return null;
        return this.bulletPos;
    }

    Died(pos) {
        this.isdead = true;
        this.body.active = false;
        ParticleManager.Instance.CreateXueEffect(pos);
        MainRoleManager.Instance.CheackGame(true);
    }

    Reset() {
        this.body.active = true;
        this.PlayAni(MusicType.Idle, 1);
        Laya.timer.clearAll(this);
      
    }

    _up = new Vector3(0, 1, 0);
    ToStand(pos) {
        if (this.isdead) return;
        //  Laya.timer.clearAll(this);
        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);
        if (Vector3.distance(this.tran.position, this.tp) < 0.1) {
            Laya.timer.clear(this, this._TOSTAND);
            return;
        }
        Quaternion.lookAt(this.tp, 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.isFristShoot = true;

        this.enemyTar = null;
        Laya.timer.frameLoop(1, this, this._atkEnemy)
    }
    private isFristShoot = true;
    _atkEnemy() {
        if (this.isdead) 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.tp = this.enemyTar.transform.position.clone();
                Quaternion.lookAt(this.tp, 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.tp = this.enemyTar.transform.position.clone();
                    Quaternion.lookAt(this.tp, this.tran.position, this._up, this.quaternion1);
                    this.quaternion1.invert(this.quaternion1);
                    this.tran.rotation = this.quaternion1.clone();
                }
                
            }
            if (this.enemyTar == null) {
                MainRoleManager.Instance.StopCreate(true);
                this.isFristShoot = true;
                return;
            }
            if (this.isFristShoot) {
                this.isFristShoot = false;

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


                MainRoleManager.Instance.CreateBullet(true);
            }

            this.tp = this.enemyTar.transform.position.clone();
            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 dancepos:Vector3;
    Dance(sp: Transform3D) {
        this.PlayAni(MusicType.RunAndGun, 2);
        this.dancepos = sp.position.clone();
        this.r = sp.rotationEuler.clone();
        Laya.timer.frameLoop(1, this, this._Dance);
    }
    _Dance() {
        Vector3.subtract(this.dancepos, 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.dancepos) < 0.15) {
            Laya.timer.clear(this, this._Dance);
            this.tran.position = this.dancepos.clone();
            this.tran.rotationEuler = this.r.clone();
            this.PlayAni(MusicType.Dance, 1);
            return;
        }
        Quaternion.lookAt(this.dancepos, 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();
    }

    onDisable() {

    }
    onDestroy() {

    }
}