MyTower.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 { UIMgr } from '../../../Frames/UIManager';
  7. const { ccclass, property } = _decorator;
  8. @ccclass('MyTower')
  9. export class MyTower extends Role {
  10. private _lifeBar: LifeBar = null;
  11. private _strMyHp: Label = null;
  12. protected onLoad(): void {
  13. this._lifeBar = this.node.getComponent(LifeBar);
  14. this._strMyHp = this.node.getChildByPath("ProgressBar/Label").getComponent(Label);
  15. }
  16. start() {
  17. this.hp = GameInfo.Instance.getMyTowerHp();
  18. this._initLifeBar();
  19. super._setupPhysics();
  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. console.log("GameOver! EnemyWin!");
  29. UIMgr.openUI("GameOver")
  30. }
  31. this._strMyHp.string = `${this._lifeBar._curHp}/${this.hp}`;
  32. }
  33. //必须实现抽象方法
  34. protected _getCollisionGroup(): number {
  35. return PhysicsGroup.Role; //实际分组值根据项目设置
  36. }
  37. protected _isValidTarget(target: Role): boolean {
  38. //通过组件类型判断是否为敌人
  39. return target instanceof Enemy;
  40. }
  41. }