Timer.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { _decorator, Component, Node } from 'cc';
  2. import { SaveBase } from '../saveCompent/SaveBase';
  3. const { ccclass, property } = _decorator;
  4. interface evt {
  5. update: (dt: number) => void
  6. // stop:(bool:boolean)=>void
  7. }
  8. // 时间类
  9. export class Timer {
  10. private _evt = chsdk.get_new_event<evt>();
  11. get evt() {
  12. return this._evt;
  13. }
  14. private isStop = false
  15. update(dt: number) {
  16. if (!this.isStop) {
  17. this.evt.emit('update', dt)
  18. }
  19. }
  20. stop(bool: boolean) {
  21. this.isStop = bool
  22. }
  23. clear() {
  24. this.evt.clearAll()
  25. }
  26. }
  27. interface evt1 {
  28. valueChange: (nowValue: number) => void
  29. }
  30. /**
  31. * 时间累加类,计算时间差,过needTime时间 给value 加 1
  32. */
  33. export class IncrementData extends SaveBase {
  34. // 最大回体力
  35. private maxValue = 0
  36. // 回一点所需体力时间,单位毫秒
  37. private needTime = 5 * 60 * 1000
  38. // 上次恢复体力的时间,0代表当前时间
  39. private lastAddTime: number = 0
  40. // 当前默认值
  41. private _value: number
  42. private defValue: number = 0
  43. private _evt = chsdk.get_new_event<evt1>();
  44. public get evt() {
  45. return this._evt
  46. }
  47. get value() {
  48. return this._value
  49. }
  50. set value(value: number) {
  51. this._value = value
  52. this.evt.emit("valueChange", value)
  53. }
  54. get needAddTime() {
  55. return this.lastAddTime + this.needTime
  56. }
  57. get max() {
  58. return this.maxValue
  59. }
  60. // 体力是否回满
  61. isMax(): boolean {
  62. if (!this.maxValue) return false
  63. return this.maxValue <= this.value
  64. }
  65. init(needTime: number, defValue: number = 0, maxValue?: number) {
  66. this.needTime = needTime
  67. this.maxValue = maxValue
  68. this.defValue = defValue
  69. // 这里可能有些问题
  70. if(this.lastAddTime == 0){
  71. this._value = defValue
  72. }
  73. }
  74. /**
  75. * 用于自定义,不会累加自带的value也没有最大值
  76. * @param num 当前时间的时间戳,单位到毫秒
  77. * @returns 计算增加值
  78. */
  79. useLastTime(num: number): number {
  80. if (!this.lastAddTime) {
  81. this.lastAddTime = num
  82. return 0
  83. }
  84. if (this.lastAddTime > num) {
  85. this.lastAddTime = num
  86. return 0
  87. }
  88. let difference = num - this.lastAddTime
  89. // 将时间差转化为秒
  90. let addValue = ~~(difference / this.needTime)
  91. if (addValue > 0) {
  92. // 更新lastTime
  93. this.lastAddTime = num - (difference % this.needTime)
  94. return addValue
  95. }
  96. return 0
  97. }
  98. /**
  99. *
  100. * @param time 当前时间的时间戳,单位到毫秒
  101. * @param value 当前结果
  102. * @returns 返回value累加时候的结果
  103. */
  104. addLastTime(time: number) {
  105. if (!this.maxValue) {
  106. return
  107. }
  108. let value = this.value
  109. if (value >= this.maxValue) {
  110. this.lastAddTime = time
  111. return value
  112. }
  113. if (!this.lastAddTime) {
  114. this.lastAddTime = time
  115. return value
  116. }
  117. if (this.lastAddTime > time) {
  118. this.lastAddTime = time
  119. return value
  120. }
  121. let difference = time - this.lastAddTime
  122. // 将时间差转化为秒
  123. let addValue = ~~(difference / this.needTime)
  124. if (addValue > 0) {
  125. // 更新lastTime
  126. let realAdd = Math.min(this.maxValue - value, addValue)
  127. if (realAdd < addValue) {
  128. this.lastAddTime = time - (difference % this.needTime)
  129. } else {
  130. this.lastAddTime = time
  131. }
  132. this.value = value + realAdd
  133. return value + realAdd
  134. }
  135. return value
  136. }
  137. serialize() {
  138. let data = {
  139. lastAddTime: this.lastAddTime,
  140. _value: this._value
  141. }
  142. return data
  143. }
  144. unserialize(data: any) {
  145. if (data) {
  146. this.lastAddTime = data.lastAddTime || 0
  147. this._value = data._value ?? this.defValue
  148. } else {
  149. this.lastAddTime = 0
  150. this._value = this.defValue
  151. }
  152. return this
  153. }
  154. }