LifeView.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { _decorator, Component, Label, Node, Sprite } from 'cc';
  2. import { rootMgr } from '../../scene/RootMgr';
  3. import { Player } from '../../data/player/Player';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('LifeView')
  6. export class LifeView extends Component {
  7. @property(Label)
  8. timeLabel: Label
  9. @property(Label)
  10. lifeLabel: Label
  11. @property(Sprite)
  12. overIcon: Sprite
  13. protected onEnable(): void {
  14. let life = rootMgr.dataControl.getCompent(Player).getLifeControl()
  15. if (life) {
  16. life.evt.on('valueChange', this.reFlashView, this)
  17. }
  18. this.reFlashView(life.value)
  19. }
  20. protected onDisable(): void {
  21. let life = rootMgr.dataControl.getCompent(Player).getLifeControl()
  22. if (life) {
  23. life.evt.off('valueChange', this.reFlashView, this)
  24. }
  25. }
  26. reFlashView(v: number) {
  27. this.lifeLabel.string = v + ""
  28. }
  29. update() {
  30. let life = rootMgr.dataControl.getCompent(Player).getLifeControl()
  31. if (!life) {
  32. this.timeLabel.node.active = false
  33. this.overIcon.node.active = true
  34. return
  35. }
  36. this.timeLabel.node.active = !life.isMax()
  37. this.overIcon.node.active = life.isMax()
  38. let now = chsdk.date.now()
  39. life.addLastTime(now)
  40. let last = life.needAddTime
  41. this.timeLabel.string = chsdk.date.ms_format(last - now, "$M:$S")
  42. }
  43. }