EnemyTower.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { _decorator, Label, PhysicsGroup } from 'cc';
  2. import { LifeBar } from './LifeBar';
  3. import { Role } from '../Role';
  4. import { Enemy } from './Enemy';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('EnemyTower')
  7. export class EnemyTower extends Role {
  8. private _lifeBar: LifeBar = null;
  9. private _strMyHp: Label = null;
  10. //敌人防御塔总血量通过数据表获取
  11. enemyTotalHp: number = null;
  12. protected onLoad(): void {
  13. this._lifeBar = this.node.getComponent(LifeBar);
  14. this._strMyHp = this.node.getChildByPath("ProgressBar/Label").getComponent(Label);
  15. }
  16. start() {
  17. this.hp = this.enemyTotalHp;
  18. this._initLifeBar();
  19. super._setupPhysics();
  20. }
  21. //初始化血条
  22. private _initLifeBar() {
  23. this._lifeBar._curHp = this.hp;
  24. this._lifeBar._totalHp = this.hp;
  25. }
  26. update(deltaTime: number) {
  27. if (this._lifeBar._curHp <= 0) {
  28. console.log("GameOver! RoleWin!");
  29. }
  30. this._strMyHp.string = `${this._lifeBar._curHp}/${this.hp}`;
  31. }
  32. //必须实现抽象方法
  33. protected _getCollisionGroup(): number {
  34. return PhysicsGroup.Enemy; //实际分组值根据项目设置
  35. }
  36. protected _isValidTarget(target: Role): boolean {
  37. //通过组件类型判断是否为敌人
  38. return target instanceof Enemy;
  39. }
  40. }