feat: enable seamless game mode switching during active gameplay

- Remove quitGame() call from changeGameMode to allow continuous play
- Game now transitions between modes without stopping
- Add test case in example App to auto-switch from letters to words at score 10
This commit is contained in:
2025-11-04 00:26:17 +00:00
parent ab3a1e13cd
commit 9037af2e31
2 changed files with 23 additions and 20 deletions
+22 -16
View File
@@ -1,4 +1,4 @@
import { useState } from "react";
import { useState, useEffect } from "react";
import { useGame, type GameMode } from "@vimazing/typing-chud";
import "@vimazing/typing-chud/game.css";
import { useKeyBindings } from "./useKeyBindings";
@@ -8,6 +8,13 @@ function App() {
const gameManager = useGame({ initialGameMode: gameMode }, useKeyBindings);
const { containerRef, gameStatus, scoreManager } = gameManager;
useEffect(() => {
if (scoreManager.score === 10 && gameMode === "letters") {
setGameMode("words");
gameManager.changeGameMode("words");
}
}, [scoreManager.score, gameMode, gameManager]);
const formatTime = (ms: number) => {
const seconds = Math.floor(ms / 1000);
const minutes = Math.floor(seconds / 60);
@@ -21,21 +28,20 @@ function App() {
<h1 className="text-2xl font-bold text-center">Typing Chud</h1>
<div className="flex gap-2 justify-center text-sm">
{(['letters', 'words', 'sentences', 'paragraphs'] as const).map((mode) => (
<button
key={mode}
onClick={(e) => {
setGameMode(mode);
gameManager.changeGameMode(mode);
e.currentTarget.blur(); // Remove focus from button
}}
className={`px-3 py-1 rounded capitalize font-medium transition ${
gameMode === mode
? 'bg-blue-600 text-white'
: 'bg-muted text-foreground hover:bg-blue-500 hover:text-white'
}`}
>
{mode}
</button>
<button
key={mode}
onClick={(e) => {
setGameMode(mode);
gameManager.changeGameMode(mode);
e.currentTarget.blur(); // Remove focus from button
}}
className={`px-3 py-1 rounded capitalize font-medium transition ${gameMode === mode
? 'bg-blue-600 text-white'
: 'bg-muted text-foreground hover:bg-blue-500 hover:text-white'
}`}
>
{mode}
</button>
))}
</div>
</div>
+1 -4
View File
@@ -42,10 +42,7 @@ export function useGame(
const changeGameMode = (mode: GameMode) => {
setGameModeState(mode);
// Only quit if the game is actively running
if (gameStatusManager.gameStatus === 'started') {
gameStatusManager.quitGame();
}
// Don't quit - just switch modes and let game continue
};
// Set the game loop callback