diff --git a/apps/client/src/scenes/game/application/BombManager.ts b/apps/client/src/scenes/game/application/BombManager.ts index 0b6ef4d..9ed623a 100644 --- a/apps/client/src/scenes/game/application/BombManager.ts +++ b/apps/client/src/scenes/game/application/BombManager.ts @@ -9,11 +9,14 @@ import { BombController } from "@client/scenes/game/entities/bomb/BombController"; import type { GamePlayers } from "./game.types"; +/** 経過時間ミリ秒を返す関数型 */ +export type ElapsedMsProvider = () => number; + type BombManagerOptions = { worldContainer: Container; players: GamePlayers; myId: string; - getElapsedMs: () => number; + getElapsedMs: ElapsedMsProvider; }; /** 爆弾エンティティのライフサイクルを管理する */ @@ -21,7 +24,7 @@ private worldContainer: Container; private players: GamePlayers; private myId: string; - private getElapsedMs: () => number; + private getElapsedMs: ElapsedMsProvider; private bombs: BombController[] = []; private lastBombPlacedElapsedMs = Number.NEGATIVE_INFINITY; diff --git a/apps/client/src/scenes/game/application/GameTimer.ts b/apps/client/src/scenes/game/application/GameTimer.ts index f880cac..1670150 100644 --- a/apps/client/src/scenes/game/application/GameTimer.ts +++ b/apps/client/src/scenes/game/application/GameTimer.ts @@ -5,9 +5,17 @@ */ import { config } from "@repo/shared"; +/** 現在時刻ミリ秒を返す関数型 */ +export type NowMsProvider = () => number; + /** ゲーム制限時間の残り秒数を管理するタイマーモデル */ export class GameTimer { private gameStartTime: number | null = null; + private nowMsProvider: NowMsProvider; + + constructor(nowMsProvider: NowMsProvider = () => Date.now()) { + this.nowMsProvider = nowMsProvider; + } public setGameStart(startTime: number) { this.gameStartTime = startTime; @@ -16,7 +24,7 @@ public getRemainingTime(): number { if (!this.gameStartTime) return config.GAME_CONFIG.GAME_DURATION_SEC; - const elapsedMs = Date.now() - this.gameStartTime; + const elapsedMs = this.nowMsProvider() - this.gameStartTime; const remainingSec = config.GAME_CONFIG.GAME_DURATION_SEC - elapsedMs / 1000; return Math.max(0, remainingSec); @@ -24,6 +32,6 @@ public getElapsedMs(): number { if (!this.gameStartTime) return 0; - return Math.max(0, Date.now() - this.gameStartTime); + return Math.max(0, this.nowMsProvider() - this.gameStartTime); } }