EnemyTower.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { _decorator, 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.hp = this.enemyTotalHp;
  22. this._initLifeBar();
  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. GameInfo.Instance.setIsGameOver(true);
  38. GameInfo.Instance.setOverWin(true);
  39. const myTower = find("Canvas/GameRoot/MyTower").getComponent(MyTower);
  40. this._lifePercent(myTower.getCurHp(), myTower.getTotalHp())
  41. GameMgr.Instance.getModuler(GameOver).show();
  42. }
  43. private _lifePercent(curHp: number, totalHp: number){
  44. if(totalHp <= 0){
  45. return;
  46. }
  47. let lifePercent = Math.floor((curHp / totalHp) * 100);
  48. lifePercent = Math.max(0, Math.min(lifePercent,100));
  49. GameInfo.Instance.setLifePecent(lifePercent);
  50. }
  51. //必须实现抽象方法
  52. protected _getCollisionGroup(): number {
  53. return PhysicsGroup.Enemy; //实际分组值根据项目设置
  54. }
  55. protected _isValidTarget(target: Role): boolean {
  56. //通过组件类型判断是否为敌人
  57. return target instanceof Enemy;
  58. }
  59. }