* @zh 更新效果(每帧调用) * @en Update effects (called every frame) * * @param deltaTime - @zh 帧时间(秒)@en Delta time in seconds
(deltaTime: number)
| 385 | * @param deltaTime - @zh 帧时间(秒)@en Delta time in seconds |
| 386 | */ |
| 387 | update(deltaTime: number): void { |
| 388 | const toRemove: string[] = []; |
| 389 | |
| 390 | for (const effect of this._effects.values()) { |
| 391 | if (!effect.isActive) continue; |
| 392 | |
| 393 | const definition = effect.definition; |
| 394 | const handler = this._handlers.get(definition.typeId); |
| 395 | |
| 396 | // Update remaining time |
| 397 | if (definition.duration.type === 'timed') { |
| 398 | effect.remainingTime -= deltaTime; |
| 399 | if (effect.remainingTime <= 0) { |
| 400 | this._emitEvent('expired', effect); |
| 401 | toRemove.push(effect.instanceId); |
| 402 | continue; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | // Check conditional duration |
| 407 | if (definition.duration.type === 'conditional') { |
| 408 | const condition = definition.duration.condition; |
| 409 | if (condition && !condition()) { |
| 410 | this._emitEvent('expired', effect); |
| 411 | toRemove.push(effect.instanceId); |
| 412 | continue; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | // Handle periodic tick |
| 417 | if (definition.tickInterval && definition.tickInterval > 0) { |
| 418 | effect.nextTickTime -= deltaTime; |
| 419 | if (effect.nextTickTime <= 0) { |
| 420 | handler?.onTick?.(effect, this._target, deltaTime); |
| 421 | this._emitEvent('ticked', effect); |
| 422 | effect.nextTickTime = definition.tickInterval; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | // Call update handler |
| 427 | handler?.onUpdate?.(effect, this._target, deltaTime); |
| 428 | } |
| 429 | |
| 430 | // Remove expired effects |
| 431 | for (const id of toRemove) { |
| 432 | this.remove(id); |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | /** |