EnemyTower.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { _decorator, director, Label, PhysicsGroup } from 'cc';
  2. import { LifeBar } from './LifeBar';
  3. import { Role } from '../Role';
  4. import { Enemy } from './Enemy';
  5. import { GameOverData } from './Data/GameOverData';
  6. import { UIMgr } from '../../../Frames/UIManager';
  7. import { messageMgr } from '../../../Frames/MessageMgr';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('EnemyTower')
  10. export class EnemyTower extends Role {
  11. private _lifeBar: LifeBar = null;
  12. private _strMyHp: Label = null;
  13. //敌人防御塔总血量通过数据表获取
  14. enemyTotalHp: number = null;
  15. protected onLoad(): void {
  16. this._lifeBar = this.node.getComponent(LifeBar);
  17. this._strMyHp = this.node.getChildByPath("ProgressBar/Label").getComponent(Label);
  18. }
  19. start() {
  20. this.hp = this.enemyTotalHp;
  21. this._initLifeBar();
  22. super._setupPhysics();
  23. }
  24. //初始化血条
  25. private _initLifeBar() {
  26. this._lifeBar._curHp = this.hp;
  27. this._lifeBar._totalHp = this.hp;
  28. }
  29. update(deltaTime: number) {
  30. if (this._lifeBar._curHp <= 0) {
  31. this._gameOverDtSettlement();
  32. }
  33. this._strMyHp.string = `${this._lifeBar._curHp}/${this.hp}`;
  34. }
  35. //游戏结束数据结算
  36. private _gameOverDtSettlement(){
  37. messageMgr.addEvent("Victory",this._lifePercent, this)
  38. GameOverData.Instance.Win = true;
  39. UIMgr.openUI("GameOver");
  40. director.pause();
  41. }
  42. private _lifePercent(curHp: number, totalHp: number){
  43. console.log(curHp + " " + totalHp)
  44. if(totalHp <= 0){
  45. GameOverData.Instance.LifePercent = 0;
  46. return;
  47. }
  48. let lifePercent = Math.floor((curHp / totalHp) * 100);
  49. lifePercent = Math.max(0, Math.min(lifePercent,100));
  50. GameOverData.Instance.LifePercent = lifePercent;
  51. }
  52. //必须实现抽象方法
  53. protected _getCollisionGroup(): number {
  54. return PhysicsGroup.Enemy; //实际分组值根据项目设置
  55. }
  56. protected _isValidTarget(target: Role): boolean {
  57. //通过组件类型判断是否为敌人
  58. return target instanceof Enemy;
  59. }
  60. }