import Transform3D = Laya.Transform3D;
import Sprite3D = Laya.Sprite3D;
import Vector3 = Laya.Vector3;
import Quaternion = Laya.Quaternion;
import { Mathf } from "../Util/Mathf";
import { GlobalVariable } from "../game_module/GlobalVariable";
import { PlayerManager } from "./PlayerManager";
import RoleManager from "../Manager/RoleManager";
import PlaneManager from "../Manager/PlaneManager";
import { GamePool } from "./GamePool";
import { ParticleManager } from "./ParticleManager";
import GameManager from "./GameManager";
import AssetController from "./AssetController";

export class Opponent extends Laya.Script3D {

    public selfobj: Sprite3D;
    private tran: Transform3D;
    private rayPoint: Transform3D;

    public forW: Vector3;
    private quaternion1: Quaternion;
    private speed: number;
    private _up = new Vector3(0, 1, 0);
    private mat:Laya.PBRStandardMaterial;
    onAwake() {
        this.selfobj = this.owner as Sprite3D;
        this.tran = this.selfobj.transform;
        this.mat=((this.selfobj.getChildAt(0).getChildByName("enemy")as Laya.SkinnedMeshSprite3D).skinnedMeshRenderer.material) as Laya.PBRStandardMaterial;
        this.forW = Mathf.VecZero;
        this.speed = GlobalVariable.enemySpeed;
        this.quaternion1 = new Quaternion();
    }

    private tarPos: Vector3;
    private hp: number;
    private tarSp: Sprite3D;
    private tar1: Vector3;
    private isArrive1 = false;

    SetTar(tar, hp,cloth) {
        this.tarSp = tar;
        this.isArrive1 = false;
        if (this.tarSp == null) return;
        this.tarPos = this.tarSp.transform.position.clone();
        if (this.tarPos.x > 0) {
            this.tarPos = PlaneManager.Instance.toured1.clone();
        } else {
            this.tarPos = PlaneManager.Instance.toured2.clone();
        }
        this.hp =  hp;//TODO
        this.mat.albedoTexture=AssetController.Instance.cloth[cloth-1];
        this.mat.emissionTexture=AssetController.Instance.cloth[cloth-1];
        Laya.timer.frameLoop(1, this, this.Move);
    }
    Move() {
        if (this.isArrive1) {
            if (this.tarSp == null) return;
            if (!this.tarSp.active) {
                this.tarSp = RoleManager.Instance.GetTar();
                if (this.tarSp == null) return;
                this.tarPos = this.tarSp.transform.position.clone();
            }
            Vector3.subtract(this.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, this.tarPos) < 0.1) {
                return;
            }
            Quaternion.lookAt(this.tarPos, this.tran.position, this._up, this.quaternion1);
            this.quaternion1.invert(this.quaternion1);
            this.tran.rotation = this.quaternion1.clone();
        } else {
            Vector3.subtract(this.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, this.tarPos) < 0.1) {


                this.tarSp = RoleManager.Instance.GetTar();
                this.isArrive1 = true;
                if (this.tarSp == null) return;
                this.tarPos = this.tarSp.transform.position.clone();
                return;
            }
            Quaternion.lookAt(this.tarPos, this.tran.position, this._up, this.quaternion1);
            this.quaternion1.invert(this.quaternion1);
            this.tran.rotation = this.quaternion1.clone();
        }


    }
    Atked(hitpos) {
        // if(this.isdead){
        //     console.log("死过了");
        //     return;
        // }
        //console.log(this.selfobj.id,"time")
        this.hp -= PlayerManager.bulletAttack;
        if (this.hp <= 0) {
            //this.isdead=true;
            this.Died();
            return;
        }
        ParticleManager.Instance.CreateHitEffect(hitpos);
    }


  
    Died() {
       
        ParticleManager.Instance.CreateXueEffect(this.tran.position);
        this.Des();
        RoleManager.Instance.CheackGame();
        GameManager.Instance.roleCount++;
    }



    Des() {
        Laya.timer.clearAll(this);
        RoleManager.Instance.RemoveEnemy(this.selfobj);
        GamePool.Instance.SetDisableObject(this.selfobj);
        
    }

    onDestroy() {

    }
}