TimeItem.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { _decorator, Color, Component, Label, Node, Tween, tween, v3 } from 'cc';
  2. import { TimeUtil } from '../util/TimeUtil';
  3. import { EventMgr } from '../manager/EventMgr';
  4. import { EventType } from '../enum/EventType';
  5. import { GameMgr } from '../manager/GameMgr';
  6. import { Debug } from '../util/Debug';
  7. import { Global } from '../Global';
  8. const { ccclass, property } = _decorator;
  9. const Tag: string = 'TimeItem'
  10. @ccclass('UIItem/TimeItem')
  11. export class TimeItem extends Component {
  12. @property(Node)
  13. private nz: Node = null
  14. private _label: Label = null
  15. private leftTime: number = 0
  16. public get LeftTime(): number {
  17. return this.leftTime
  18. }
  19. public set LeftTime(v: number) {
  20. this.leftTime = Math.max(v, 0)
  21. EventMgr.emit(EventType.TimeTick, this.leftTime)
  22. this._label.string = TimeUtil.formatTime_HHMMSS(Math.round(this.leftTime) * 1000)
  23. this._label.color = this.leftTime <= 5 ? Color.RED : Color.WHITE
  24. }
  25. private _freezeTime: number = 0
  26. private _freeze: boolean = false
  27. private _pause: boolean = false
  28. private _tw: Tween<Node> = null
  29. protected onLoad() {
  30. this._label = this.getComponentInChildren(Label)
  31. globalThis.TimeItem = this
  32. EventMgr.on(EventType.Revive, this.onRevive, this)
  33. EventMgr.on(EventType.UseSkillFreezeTime, this.onUseSkillFreeze, this)
  34. EventMgr.on(EventType.TimeReset, this.onTimeReset, this)
  35. }
  36. protected onDestroy(): void {
  37. EventMgr.off(EventType.Revive, this.onRevive, this)
  38. EventMgr.off(EventType.UseSkillFreezeTime, this.onUseSkillFreeze, this)
  39. EventMgr.off(EventType.TimeReset, this.onTimeReset, this)
  40. }
  41. protected update(dt: number): void {
  42. if (this._pause) return
  43. if (GameMgr.Pause) return
  44. if (Global.Level_Time === -1) return
  45. if (this._freeze) {
  46. this._freezeTime -= dt
  47. if (this._freezeTime <= 0) {
  48. this._freeze = false
  49. Debug.Log(Tag, '结束冰冻')
  50. }
  51. } else {
  52. const preTime: number = this.leftTime
  53. this.LeftTime -= dt
  54. if (preTime > Global.Time_Out_Alert_Time && this.leftTime <= Global.Time_Out_Alert_Time) {
  55. EventMgr.emit(EventType.TimeOutAlert)
  56. if (this._tw) this._tw.stop()
  57. this._tw = tween(this.nz).sequence(
  58. tween(this.nz).to(0.5, { scale: v3(1.2, 1.2, 1) }),
  59. tween(this.nz).to(0.5, { scale: v3(1, 1, 1) }),
  60. ).repeatForever().start()
  61. }
  62. if (this.leftTime <= 0) {
  63. this._pause = true
  64. EventMgr.emit(EventType.TimeOut)
  65. }
  66. }
  67. }
  68. protected onTimeReset(): void {
  69. this._pause = false
  70. this.LeftTime = Global.Level_Time
  71. this.nz.scale = v3(1, 1, 1)
  72. if (this._tw) {
  73. this._tw.stop()
  74. this._tw = null
  75. }
  76. }
  77. protected onRevive(): void {
  78. this._pause = false
  79. this.LeftTime = Global.Revive_Add_Time
  80. this.nz.scale = v3(1, 1, 1)
  81. if (this._tw) {
  82. this._tw.stop()
  83. this._tw = null
  84. }
  85. }
  86. protected onUseSkillFreeze(): void {
  87. this._freeze = true
  88. this._freezeTime = Global.Freeze_Time
  89. Debug.Log(Tag, '开始冰冻')
  90. }
  91. }