starting new structure
This commit is contained in:
@@ -1,22 +1,21 @@
|
||||
import { useEffect } from "react";
|
||||
import type { UseGameType } from "../../src/";
|
||||
import type { GameManager } from "../../src";
|
||||
|
||||
export const useKeyBindings = (gameManager: UseGameType) => {
|
||||
const { gameStatus, initGame, stopGame, clearLog, resetCount } = gameManager;
|
||||
export const useKeyBindings = (gameManager: GameManager) => {
|
||||
const { gameStatus, renderBoard, startGame, quitGame, clearKeyLog } = gameManager;
|
||||
|
||||
useEffect(() => {
|
||||
const handler = (e: KeyboardEvent) => {
|
||||
console.log(e.key);
|
||||
const handler = (ev: KeyboardEvent) => {
|
||||
// start game
|
||||
if (e.code === "Space") {
|
||||
if (ev.code === "Space") {
|
||||
if (["waiting", "game-over", "game-won"].includes(gameStatus)) {
|
||||
clearLog();
|
||||
resetCount();
|
||||
clearKeyLog();
|
||||
renderBoard();
|
||||
if (gameStatus === "game-over" || gameStatus === "game-won") {
|
||||
stopGame();
|
||||
setTimeout(() => initGame(), 0);
|
||||
quitGame();
|
||||
setTimeout(() => startGame(), 0);
|
||||
} else {
|
||||
initGame();
|
||||
startGame();
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -25,23 +24,15 @@ export const useKeyBindings = (gameManager: UseGameType) => {
|
||||
// ignore other keys while waiting
|
||||
if (gameStatus === "waiting") return;
|
||||
|
||||
// cancel count or stop game
|
||||
if (e.key === "Escape") {
|
||||
stopGame();
|
||||
resetCount();
|
||||
return;
|
||||
}
|
||||
|
||||
// quit
|
||||
if (e.key === "q" || e.key === "Q") {
|
||||
stopGame();
|
||||
resetCount();
|
||||
if (ev.key === "q" || ev.key === "Q") {
|
||||
quitGame();
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener("keydown", handler);
|
||||
return () => window.removeEventListener("keydown", handler);
|
||||
}, [gameStatus, initGame, stopGame, clearLog, resetCount]);
|
||||
}, [gameStatus]);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user