import { ConfigConst, EConfigConst } from "../cfg/ConfigDef";
import { ConfigMgr } from "../cfg/configMgr";
import { MonsterItem } from "../cfg/monsterItem";
import { Utils } from "../common/utils";
import { ZooId } from "../zoo/zooId";
import { BulletPlay } from "./bulletPlay";

const {ccclass, property, executeInEditMode, playOnFocus} = cc._decorator;

@ccclass
export class MonsterModel extends cc.Component{
    hp:number = 1000;
    atk:number = 100;

    zooId: number;

    protected onLoad(): void {
        this.zooId = this.addComponent(ZooId).id;
        cc.log('[MonsterModel]', this.zooId);
    }

    beAttacked(harmNum: number){
        this.hp -= harmNum;
    }

    isOver(){
        return this.hp <= 0;
    }

    init(id: string, level: number){
        cc.log('[MonsterModel] [init]', `[id:${id}]-[level:${level}]`);
        let monsterData= ConfigMgr.inst.getCfgClassById<MonsterItem>(EConfigConst.monster, id, MonsterItem);
        let atk = Utils.pos2vec2(monsterData.data.atk);
        let hp = Utils.pos2vec2(monsterData.data.hp);
        
        this.hp = hp.x + level * hp.y;
        this.atk = atk.x + level * hp.y;
    }

    setBullet(data: []){
        if(!data) return;
        cc.log('[MonsterModel] [setBullet]', data);
        
        let len = Math.floor(data.length/2);
        for (let index = 0; index < len; index++) {
            let cpt = this.addComponent(BulletPlay);
            cpt.monsterZooid = this.zooId;
            cpt.init(data[1+index*2], data[2+index*2]);
            cpt.start();            
        }
        
        // let cpt = this.addComponent(BulletPlay);
        // cpt.monsterZooid = this.zooId;
        // cpt.init('1001', 0);
    }
}