import { _decorator, Component, Node, ProgressBarComponent, Vec3, log } from 'cc';
import { GameController } from '../GameController';
import { MonsterBase } from './MonsterBase';
import { MonsterHPUIPool } from './MonsterHPUIPool';
const { ccclass, property } = _decorator;

@ccclass('MonsterHP')
export class MonsterHP extends Component {
    
    public monster:MonsterBase;

    private progressBar:ProgressBarComponent;

    private HELP_V3_0:Vec3=new Vec3();
    private HELP_V3_1:Vec3=new Vec3();
    start () {
        this.progressBar=this.getComponent(ProgressBarComponent);
        this.node.setScale(0,0,0);
        this.progressBar.progress=0;
    }

    onEnable():void{
        this.progressBar=this.getComponent(ProgressBarComponent);
        this.node.setScale(0,0,0);
    }


    update (deltaTime: number) {
        if(this.monster==null){
            return;
        }
        //更新位置
        this.monster.hpSocket.getWorldPosition(this.HELP_V3_0);
        GameController.single.camera.convertToUINode(this.HELP_V3_0,GameController.single.monsterHPUILayer,this.HELP_V3_1);
        this.node.setPosition(this.HELP_V3_1);
        
        
        //根据距离缩放血条
        let scale:number=1-Math.min(1,Math.abs(this.monster.node.worldPosition.z/GameController.single.middleStartNode.worldPosition.z*0.4));
        this.HELP_V3_0.set(scale,scale,scale);
        this.node.setScale(this.HELP_V3_0);
        
        //更新HP
        this.progressBar.progress=this.monster.hp/this.monster.maxHP;
    }
}
