LifeBar.ts 904 B

1234567891011121314151617181920212223242526272829
  1. import { _decorator, Component, ProgressBar } from 'cc';
  2. import { Role, RoleState } from '../Role';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('LifeBar')
  5. export class LifeBar extends Component {
  6. _curHp: number = null;
  7. _totalHp: number = null;
  8. private _progressBar: ProgressBar = null;
  9. private _role: Role = null;
  10. protected onLoad(): void {
  11. this._role = this.node.getComponent(Role);
  12. this._progressBar = this.node.getChildByName("ProgressBar").getComponent(ProgressBar);
  13. }
  14. start() {
  15. this._progressBar.progress = this._curHp / this._totalHp;
  16. }
  17. updateProgressBar(hp: number) {
  18. this._curHp = hp;
  19. this._progressBar.progress = this._curHp / this._totalHp;
  20. if (hp <= 0) {
  21. this._role.playAnimation(RoleState.Die);
  22. this._curHp = 0;
  23. this._progressBar.progress = 0;
  24. }
  25. }
  26. }