/** 延迟调度任务 */ interface ScheduledTask { /** 触发时刻(逻辑秒) */ at: number; /** 回调函数 */ fn: () => void; /** 是否已取消 */ cancelled: boolean; } /** 战斗时钟:逻辑时间、倍速、延迟调度 */ export class BattleClock { /** 逻辑时间(秒) */ time = 0; /** 时间倍速 */ timeScale = 1; private _tasks: ScheduledTask[] = []; private _taskId = 0; /** * @param delaySec 延迟秒数 * @param fn 回调函数 * @returns 任务 ID */ schedule(delaySec: number, fn: () => void): number { const id = ++this._taskId; this._tasks.push({ at: this.time + Math.max(0, delaySec), fn, cancelled: false }); return id; } /** @param dt 帧间隔(秒);须为已乘战斗 TimeScale 的逻辑步长(见 BattleRuntime.tick) */ advance(dt: number): void { const scaled = dt * this.timeScale; this.time += scaled; if (this._tasks.length === 0) return; const due: ScheduledTask[] = []; const pending: ScheduledTask[] = []; for (const t of this._tasks) { if (t.cancelled) continue; if (t.at <= this.time) due.push(t); else pending.push(t); } this._tasks = pending; for (const t of due) { try { t.fn(); } catch (e) { console.error('[BattleClock] scheduled task error', String(e)); } } } /** 重置逻辑时间与待执行任务 */ reset(): void { this.time = 0; this._tasks.length = 0; } }