EnemyTower.ts 2.2 KB

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