MyTower.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { _decorator, director, find, 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 { UIMgr } from '../../../Frames/UIManager';
  7. import { GameOverData } from './Data/GameOverData';
  8. import { messageMgr } from '../../../Frames/MessageMgr';
  9. const { ccclass, property } = _decorator;
  10. @ccclass('MyTower')
  11. export class MyTower extends Role {
  12. private _lifeBar: LifeBar = null;
  13. private _strMyHp: Label = null;
  14. protected onLoad(): void {
  15. this._lifeBar = this.node.getComponent(LifeBar);
  16. this._strMyHp = this.node.getChildByPath("ProgressBar/Label").getComponent(Label);
  17. }
  18. start() {
  19. this.hp = GameInfo.Instance.getMyTowerHp();
  20. this._initLifeBar();
  21. //super._setupPhysics();
  22. }
  23. //初始化血条
  24. private _initLifeBar() {
  25. this._lifeBar._curHp = GameInfo.Instance.getMyTowerHp();
  26. this._lifeBar._totalHp = GameInfo.Instance.getMyTowerHp();
  27. }
  28. update(deltaTime: number) {
  29. if(this._lifeBar._curHp <= 0){
  30. this._gameOverDtSettlement();
  31. }
  32. this._strMyHp.string = `${this._lifeBar._curHp}/${this.hp}`;
  33. }
  34. getCurHp(){
  35. return this._lifeBar._curHp;
  36. }
  37. getTotalHp(){
  38. return this._lifeBar._totalHp;
  39. }
  40. //游戏结束数据结算
  41. private _gameOverDtSettlement(){
  42. GameOverData.Instance.LifePercent = 0;
  43. GameOverData.Instance.Win = false;
  44. UIMgr.openUI("GameOver");
  45. }
  46. //必须实现抽象方法
  47. protected _getCollisionGroup(): number {
  48. return PhysicsGroup.Role; //实际分组值根据项目设置
  49. }
  50. protected _isValidTarget(target: Role): boolean {
  51. //通过组件类型判断是否为敌人
  52. return target instanceof Enemy;
  53. }
  54. }