MyTower.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { _decorator, director, Label, PhysicsGroup} from 'cc';
  2. import { Role } from '../Role';
  3. import { LifeBar } from './LifeBar';
  4. import { Enemy } from './Enemy';
  5. import { GameInfo } from '../../../GameInfo';
  6. import { GameMgr } from '../../GameFrameWork/GameMgr';
  7. import { GameOver } from './GameOver';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('MyTower')
  10. export class MyTower extends Role {
  11. private _lifeBar: LifeBar = null;
  12. private _strMyHp: Label = null;
  13. protected onLoad(): void {
  14. this._lifeBar = this.node.getComponent(LifeBar);
  15. this._strMyHp = this.node.getChildByPath("ProgressBar/Label").getComponent(Label);
  16. }
  17. start() {
  18. this.initData();
  19. }
  20. initData(){
  21. this.hp = GameInfo.Instance.getMyTowerHp();
  22. this._initLifeBar();
  23. }
  24. //初始化血条
  25. private _initLifeBar() {
  26. this._lifeBar._curHp = GameInfo.Instance.getMyTowerHp();
  27. this._lifeBar._totalHp = GameInfo.Instance.getMyTowerHp();
  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. getCurHp(){
  36. return this._lifeBar._curHp;
  37. }
  38. getTotalHp(){
  39. return this._lifeBar._totalHp;
  40. }
  41. //游戏结束数据结算
  42. private _gameOverDtSettlement(){
  43. GameInfo.Instance.setIsGameOver(true);
  44. GameInfo.Instance.setOverWin(false);
  45. GameInfo.Instance.setLifePecent(0);
  46. GameMgr.Instance.getModuler(GameOver).show();
  47. director.pause();
  48. }
  49. //必须实现抽象方法
  50. protected _getCollisionGroup(): number {
  51. return PhysicsGroup.Role; //实际分组值根据项目设置
  52. }
  53. protected _isValidTarget(target: Role): boolean {
  54. //通过组件类型判断是否为敌人
  55. return target instanceof Enemy;
  56. }
  57. }