EnemyTower.ts 2.3 KB

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