MyTower.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. this._lifeBar.progressBar.progress = 1;
  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. getCurHp(){
  37. return this._lifeBar._curHp;
  38. }
  39. getTotalHp(){
  40. return this._lifeBar._totalHp;
  41. }
  42. //游戏结束数据结算
  43. private _gameOverDtSettlement(){
  44. GameInfo.Instance.setIsGameOver(true);
  45. GameInfo.Instance.setOverWin(false);
  46. GameInfo.Instance.setLifePecent(0);
  47. GameMgr.Instance.getModuler(GameOver).show();
  48. director.pause();
  49. }
  50. //必须实现抽象方法
  51. protected _getCollisionGroup(): number {
  52. return PhysicsGroup.Role; //实际分组值根据项目设置
  53. }
  54. protected _isValidTarget(target: Role): boolean {
  55. //通过组件类型判断是否为敌人
  56. return target instanceof Enemy;
  57. }
  58. }