MyTower.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { _decorator, 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.hp = GameInfo.Instance.getMyTowerHp();
  19. this._initLifeBar();
  20. }
  21. //初始化血条
  22. private _initLifeBar() {
  23. this._lifeBar._curHp = GameInfo.Instance.getMyTowerHp();
  24. this._lifeBar._totalHp = GameInfo.Instance.getMyTowerHp();
  25. }
  26. update(deltaTime: number) {
  27. if(this._lifeBar._curHp <= 0){
  28. this._gameOverDtSettlement();
  29. }
  30. this._strMyHp.string = `${this._lifeBar._curHp}/${this.hp}`;
  31. }
  32. getCurHp(){
  33. return this._lifeBar._curHp;
  34. }
  35. getTotalHp(){
  36. return this._lifeBar._totalHp;
  37. }
  38. //游戏结束数据结算
  39. private _gameOverDtSettlement(){
  40. GameInfo.Instance.setIsGameOver(true);
  41. GameInfo.Instance.setOverWin(false);
  42. GameInfo.Instance.setLifePecent(0);
  43. GameMgr.Instance.getModuler(GameOver).show();
  44. }
  45. //必须实现抽象方法
  46. protected _getCollisionGroup(): number {
  47. return PhysicsGroup.Role; //实际分组值根据项目设置
  48. }
  49. protected _isValidTarget(target: Role): boolean {
  50. //通过组件类型判断是否为敌人
  51. return target instanceof Enemy;
  52. }
  53. }