fix(example): update useKeyBindings to work with new GameManager interface
- Import GameManager type directly from main package - Use new Unified API methods: renderBoard, quitGame, clearKeyLog - Remove pause functionality for now (simplified like vim-maze) - Match vim-maze's key binding pattern - Game now starts and plays correctly
This commit is contained in:
@@ -1,19 +1,18 @@
|
|||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
import type { UseGameType } from "@vimazing/vim-snake";
|
import type { GameManager } from "../../src";
|
||||||
|
|
||||||
export const useKeyBindings = (gameManager: UseGameType) => {
|
export const useKeyBindings = (gameManager: GameManager) => {
|
||||||
const { gameStatus, startGame, stopGame, clearLog, resetCount, togglePause } = gameManager;
|
const { gameStatus, renderBoard, startGame, quitGame, clearKeyLog } = gameManager;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handler = (ev: KeyboardEvent) => {
|
const handler = (ev: KeyboardEvent) => {
|
||||||
|
|
||||||
// start game
|
// start game
|
||||||
if (ev.code === "Space") {
|
if (ev.code === "Space") {
|
||||||
if (["waiting", "game-over", "game-won"].includes(gameStatus)) {
|
if (["waiting", "game-over", "game-won"].includes(gameStatus)) {
|
||||||
clearLog();
|
clearKeyLog();
|
||||||
resetCount();
|
renderBoard();
|
||||||
if (gameStatus === "game-over" || gameStatus === "game-won") {
|
if (gameStatus === "game-over" || gameStatus === "game-won") {
|
||||||
stopGame();
|
quitGame();
|
||||||
setTimeout(() => startGame(), 0);
|
setTimeout(() => startGame(), 0);
|
||||||
} else {
|
} else {
|
||||||
startGame();
|
startGame();
|
||||||
@@ -22,24 +21,18 @@ export const useKeyBindings = (gameManager: UseGameType) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('ev.key', ev.key)
|
// ignore other keys while waiting
|
||||||
|
if (gameStatus === "waiting") return;
|
||||||
if (ev.key === 'p' || ev.key === 'P') {
|
|
||||||
togglePause();
|
|
||||||
}
|
|
||||||
|
|
||||||
// quit
|
// quit
|
||||||
if (ev.key === "q" || ev.key === "Q") {
|
if (ev.key === "q" || ev.key === "Q") {
|
||||||
if (gameStatus === "started") {
|
quitGame();
|
||||||
stopGame();
|
return;
|
||||||
resetCount();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
window.addEventListener("keydown", handler, { capture: true });
|
window.addEventListener("keydown", handler);
|
||||||
return () => window.removeEventListener("keydown", handler, { capture: true });
|
return () => window.removeEventListener("keydown", handler);
|
||||||
}, [gameStatus, startGame, stopGame, clearLog, resetCount]);
|
}, [gameStatus]);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user