* Update timer (called by ECS system each frame) * 更新定时器(每帧由 ECS 系统调用) * * @param deltaMs Delta time in milliseconds | 间隔时间(毫秒)
(deltaMs: number)
| 92 | * @param deltaMs Delta time in milliseconds | 间隔时间(毫秒) |
| 93 | */ |
| 94 | public update(deltaMs: number): void { |
| 95 | this.delta = deltaMs; |
| 96 | this.currentTime += deltaMs; |
| 97 | this.frameCount++; |
| 98 | |
| 99 | this._updating = true; |
| 100 | |
| 101 | // Process timers |
| 102 | for (const callback of this._callbacks.values()) { |
| 103 | if (callback.removed) continue; |
| 104 | |
| 105 | callback.elapsed += deltaMs; |
| 106 | if (callback.elapsed >= callback.interval) { |
| 107 | callback.callback.call(callback.caller); |
| 108 | if (callback.repeat) { |
| 109 | callback.elapsed = 0; |
| 110 | } else { |
| 111 | callback.removed = true; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // Clean up removed callbacks |
| 117 | for (const [id, callback] of this._callbacks) { |
| 118 | if (callback.removed) { |
| 119 | this._callbacks.delete(id); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // Process callLater |
| 124 | const pending = this._callLaterList; |
| 125 | this._callLaterList = this._callLaterPending; |
| 126 | this._callLaterPending = []; |
| 127 | |
| 128 | for (const item of pending) { |
| 129 | item.callback.call(item.caller); |
| 130 | } |
| 131 | pending.length = 0; |
| 132 | this._callLaterList = pending; |
| 133 | |
| 134 | this._updating = false; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Execute callback after specified delay (one time) |