monsterModel.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { ConfigConst, EConfigConst } from "../cfg/ConfigDef";
  2. import { ConfigMgr } from "../cfg/configMgr";
  3. import { MonsterItem } from "../cfg/monsterItem";
  4. import { Utils } from "../common/utils";
  5. import { ZooId } from "../zoo/zooId";
  6. import { BulletPlay } from "./bulletPlay";
  7. const { ccclass, property, executeInEditMode, playOnFocus } = cc._decorator;
  8. @ccclass
  9. export class MonsterModel extends cc.Component {
  10. hp: number = 1000;
  11. atk: number = 100;
  12. zooId: number;
  13. protected onLoad(): void {
  14. this.zooId = this.addComponent(ZooId).id;
  15. cc.log('[MonsterModel]', this.zooId);
  16. }
  17. beAttacked(harmNum: number) {
  18. this.hp -= harmNum;
  19. }
  20. isOver() {
  21. return this.hp <= 0;
  22. }
  23. init(id: string, level: number) {
  24. let monsterData = ConfigMgr.inst.getCfgClassById<MonsterItem>(EConfigConst.monster, id, MonsterItem);
  25. let atk = Utils.strToJson(monsterData.data.atk);
  26. let hp = Utils.strToJson(monsterData.data.hp);
  27. this.hp = hp[0] + (level - 1) * hp[1];
  28. this.atk = atk[0] + (level - 1) * atk[1];
  29. cc.log('[MonsterModel] [init]', `[id:${id}]-[level:${level}]`, `[hp:${this.hp} atk:${this.atk}]`);
  30. }
  31. setBullet(data: []) {
  32. if (!data) return;
  33. cc.log('[MonsterModel] [setBullet]', data);
  34. let len = Math.floor(data.length / 2);
  35. for (let index = 0; index < len; index++) {
  36. let cpt = this.addComponent(BulletPlay);
  37. cpt.monsterZooid = this.zooId;
  38. cpt.init(data[1 + index * 2], data[2 + index * 2]);
  39. cpt.start();
  40. }
  41. // let cpt = this.addComponent(BulletPlay);
  42. // cpt.monsterZooid = this.zooId;
  43. // cpt.init('1001', 0);
  44. }
  45. }