EnemyTower.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 { MyTower } from './MyTower';
  6. import { GameInfo } from '../../../GameInfo';
  7. import { GameMgr } from '../../GameFrameWork/GameMgr';
  8. import { GameOver } from './GameOver';
  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.initData();
  22. }
  23. initData(){
  24. this.hp = this.enemyTotalHp;
  25. this._initLifeBar();
  26. }
  27. //初始化血条
  28. private _initLifeBar() {
  29. this._lifeBar._curHp = this.hp;
  30. this._lifeBar._totalHp = this.hp;
  31. }
  32. update(deltaTime: number) {
  33. if (this._lifeBar._curHp <= 0) {
  34. this._gameOverDtSettlement();
  35. }
  36. this._strMyHp.string = `${this._lifeBar._curHp}/${this.hp}`;
  37. }
  38. //游戏结束数据结算
  39. private _gameOverDtSettlement() {
  40. GameInfo.Instance.setIsGameOver(true);
  41. GameInfo.Instance.setOverWin(true);
  42. const myTower = find("Canvas/GameRoot/MyTower").getComponent(MyTower);
  43. this._lifePercent(myTower.getCurHp(), myTower.getTotalHp())
  44. GameMgr.Instance.getModuler(GameOver).show();
  45. director.pause();
  46. }
  47. private _lifePercent(curHp: number, totalHp: number) {
  48. if (totalHp <= 0) {
  49. return;
  50. }
  51. let lifePercent = Math.floor((curHp / totalHp) * 100);
  52. lifePercent = Math.max(0, Math.min(lifePercent, 100));
  53. GameInfo.Instance.setLifePecent(lifePercent);
  54. }
  55. //必须实现抽象方法
  56. protected _getCollisionGroup(): number {
  57. return PhysicsGroup.Enemy; //实际分组值根据项目设置
  58. }
  59. protected _isValidTarget(target: Role): boolean {
  60. //通过组件类型判断是否为敌人
  61. return target instanceof Enemy;
  62. }
  63. }