e to ev
This commit is contained in:
@@ -14,9 +14,16 @@ function App() {
|
|||||||
const { containerRef, gameStatus } = gameManager;
|
const { containerRef, gameStatus } = gameManager;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<div>
|
||||||
<div className="relative mx-auto my-4 w-fit" id="maze_container">
|
<div className="relative mx-auto my-4 w-fit" id="maze_container">
|
||||||
<div ref={containerRef} className="relative" />
|
<div ref={containerRef} className="relative" />
|
||||||
</div>
|
</div>
|
||||||
|
<div className="text-center text-2xl text-muted-foreground">
|
||||||
|
{gameStatus === "waiting" && <p>Press <kbd className="px-2 py-1 bg-muted rounded">space</kbd> to start</p>}
|
||||||
|
{gameStatus === "started" && <p>Use <kbd className="px-2 py-1 bg-muted rounded">hjkl</kbd> to change direction • Press <kbd className="px-2 py-1 bg-muted rounded">q</kbd> to quit</p>}
|
||||||
|
{gameStatus === "game-over" && <p>Game Over! Press <kbd className="px-2 py-1 bg-muted rounded">space</kbd> to restart</p>}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -5,12 +5,10 @@ export const useKeyBindings = (gameManager: GameManager) => {
|
|||||||
const { gameStatus, startGame, quitGame, keyManager } = gameManager;
|
const { gameStatus, startGame, quitGame, keyManager } = gameManager;
|
||||||
const { clearLog } = keyManager;
|
const { clearLog } = keyManager;
|
||||||
|
|
||||||
console.log('gameStatus', gameStatus);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handler = (e: KeyboardEvent) => {
|
const handler = (ev: KeyboardEvent) => {
|
||||||
// start game
|
// start game
|
||||||
if (e.key === "i" || e.key === "I") {
|
if (ev.code === "Space") {
|
||||||
if (["waiting", "game-over", "game-won"].includes(gameStatus)) {
|
if (["waiting", "game-over", "game-won"].includes(gameStatus)) {
|
||||||
clearLog();
|
clearLog();
|
||||||
if (gameStatus === "game-over" || gameStatus === "game-won") {
|
if (gameStatus === "game-over" || gameStatus === "game-won") {
|
||||||
@@ -27,13 +25,13 @@ export const useKeyBindings = (gameManager: GameManager) => {
|
|||||||
if (gameStatus === "waiting") return;
|
if (gameStatus === "waiting") return;
|
||||||
|
|
||||||
// cancel count or stop game
|
// cancel count or stop game
|
||||||
if (e.key === "Escape") {
|
if (ev.key === "Escape") {
|
||||||
quitGame();
|
quitGame();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// quit
|
// quit
|
||||||
if (e.key === "q" || e.key === "Q") {
|
if (ev.key === "q" || ev.key === "Q") {
|
||||||
quitGame();
|
quitGame();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,35 +45,35 @@ export const useKeyBindings = ({ cursor, gameStatus }: UseKeyBindingsParams) =>
|
|||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handler = (e: KeyboardEvent) => {
|
const handler = (ev: KeyboardEvent) => {
|
||||||
if (["waiting", "game-over", "game-won"].includes(gameStatus)) return;
|
if (["waiting", "game-over", "game-won"].includes(gameStatus)) return;
|
||||||
|
|
||||||
// "0" behaves like Vim: beginning of line
|
// "0" behaves like Vim: beginning of line
|
||||||
if (e.key === "0" && cursor.getCount() === "") {
|
if (ev.key === "0" && cursor.getCount() === "") {
|
||||||
recordKey("0");
|
recordKey("0");
|
||||||
cursor.resetCount();
|
cursor.resetCount();
|
||||||
cursor.moveToStart();
|
cursor.moveToStart();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const isDigit = e.key >= "0" && e.key <= "9";
|
const isDigit = ev.key >= "0" && e.key <= "9";
|
||||||
if (isDigit) {
|
if (isDigit) {
|
||||||
if (e.key === "0" && cursor.getCount() === "") return;
|
if (ev.key === "0" && cursor.getCount() === "") return;
|
||||||
recordKey(e.key);
|
recordKey(ev.key);
|
||||||
cursor.setCount(e.key);
|
cursor.setCount(ev.key);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (e.key === ".") {
|
if (ev.key === ".") {
|
||||||
recordKey(".");
|
recordKey(".");
|
||||||
cursor.repeatLastMotion();
|
cursor.repeatLastMotion();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (e.key === "^" || e.key === "$") {
|
if (ev.key === "^" || ev.key === "$") {
|
||||||
recordKey(e.key);
|
recordKey(e.key);
|
||||||
cursor.resetCount();
|
cursor.resetCount();
|
||||||
if (e.key === "^") {
|
if (ev.key === "^") {
|
||||||
cursor.moveToStart();
|
cursor.moveToStart();
|
||||||
} else {
|
} else {
|
||||||
cursor.moveToEnd();
|
cursor.moveToEnd();
|
||||||
@@ -81,21 +81,21 @@ export const useKeyBindings = ({ cursor, gameStatus }: UseKeyBindingsParams) =>
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (e.key === "g" || e.key === "G") {
|
if (ev.key === "g" || ev.key === "G") {
|
||||||
if (e.key === "g") {
|
if (ev.key === "g") {
|
||||||
if (cursor.getLastKey() === "g") {
|
if (cursor.getLastKey() === "g") {
|
||||||
recordKey(e.key);
|
recordKey(ev.key);
|
||||||
cursor.resetCount();
|
cursor.resetCount();
|
||||||
cursor.setLastKey("");
|
cursor.setLastKey("");
|
||||||
cursor.moveToTop();
|
cursor.moveToTop();
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
recordKey(e.key);
|
recordKey(ev.key);
|
||||||
cursor.setLastKey("g");
|
cursor.setLastKey("g");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
recordKey(e.key);
|
recordKey(ev.key);
|
||||||
cursor.resetCount();
|
cursor.resetCount();
|
||||||
cursor.setLastKey("");
|
cursor.setLastKey("");
|
||||||
cursor.moveToBottom();
|
cursor.moveToBottom();
|
||||||
@@ -103,14 +103,14 @@ export const useKeyBindings = ({ cursor, gameStatus }: UseKeyBindingsParams) =>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!RELEVANT_KEYS.has(e.key)) return;
|
if (!RELEVANT_KEYS.has(ev.key)) return;
|
||||||
|
|
||||||
recordKey(e.key);
|
recordKey(ev.key);
|
||||||
|
|
||||||
const count = Math.max(1, parseInt(cursor.getCount() || "1", 10));
|
const count = Math.max(1, parseInt(cursor.getCount() || "1", 10));
|
||||||
cursor.resetCount();
|
cursor.resetCount();
|
||||||
|
|
||||||
switch (e.key) {
|
switch (ev.key) {
|
||||||
case "h":
|
case "h":
|
||||||
case "H": cursor.moveLeft(count); break;
|
case "H": cursor.moveLeft(count); break;
|
||||||
case "j":
|
case "j":
|
||||||
|
|||||||
Reference in New Issue
Block a user