import { DispatcherManager, StageState } from "../GameUtils/EventManager";
import HpBarBehav from "./HpBarBehav";
import { AnimatorManager } from "../Manager/AnimatorManager";
import { MateMove } from "./MateMove";
import { GameManager } from "../Manager/GameManager";

export default class BossBehav extends Laya.Script3D{
    constructor(){
        super();
    }
    private _gameobject:Laya.Sprite3D;
    private _animator:Laya.Animator;
    public health:number;
    public maxhealth:number;
    public dead:boolean = false;
    public canmove:boolean = true;

    public atk:number;

    private start:boolean = false;
    public onAwake(){
        
        this._gameobject = this.owner as Laya.Sprite3D;
        this._animator = this._gameobject.getComponent(Laya.Animator) as Laya.Animator;
        this.health = 100;
        this.maxhealth = 100;
        this.atk = 10;

        DispatcherManager.StageOn(StageState.ArriverAttack,this,this.Start);
        DispatcherManager.StageOn(StageState.Win,this,this.Destroy);
        DispatcherManager.StageOn(StageState.Lose,this,this.Destroy);
    }
    onUpdate(){
        if(!this.start)return;
        if(this.dead){
            this.BossDie();
            return;
        }
        if(this.canmove){
            this.Attack();
        }
    }
    play = false;
    Attack(){
        if(!this.play){
            this.play = true;
                AnimatorManager.Play(true,this._animator, "atk", false, Laya.Handler.create(this,()=>{
                    this.play = false;
                    this._Attack();
                }));
        }
    }
    _Attack(){
        if(this.target){
            let _matemove = this.target.getComponent(MateMove) as MateMove;
            if(_matemove){
                _matemove.Injured(this.atk);
            }
        }
    }
    //受到攻击
    Injured(_damega:number){
        if(this.health == 0)return
        this.health -= _damega;
        if(this.health<=0){
            this.health = 0;
            this.dead = true;
        }
    }
    BossDie(){
        this._animator.play("die2");
        this.canmove = false;
        Laya.timer.clearAll(this);
        GameManager.Instance.GameWin();
        this.destroy();
    }
    Start(){
        this.start = true;
        let _hp = this._gameobject.addComponent(HpBarBehav) as HpBarBehav;
    }
    HideHpBar(){
        let _hp = this._gameobject.getComponent(HpBarBehav) as HpBarBehav;
        if(_hp){
            _hp.destroy();
        }
    }
    targetpos:Laya.Vector3;
    target:Laya.Sprite3D;

    _up:Laya.Vector3 = new Laya.Vector3(0,1,0);
    private quaternion1:Laya.Quaternion = new Laya.Quaternion();

    SetTarRotate(_trans:Laya.Sprite3D){
        this.target = _trans;
        this.targetpos = _trans.transform.position;
        Laya.Quaternion.lookAt(this.targetpos, this._gameobject.transform.position, this._up, this.quaternion1);
        this.quaternion1.invert(this.quaternion1);
        this._gameobject.transform.rotation = this.quaternion1.clone();
    }
    
    Destroy(){
        let _hp = this._gameobject.getComponent(HpBarBehav) as HpBarBehav;
        if(_hp){
            _hp.destroy();
        }
    }
}