before Unified API
This commit is contained in:
@@ -2,7 +2,7 @@ import { useEffect } from "react";
|
|||||||
import type { UseGameType } from "@vimazing/vim-snake";
|
import type { UseGameType } from "@vimazing/vim-snake";
|
||||||
|
|
||||||
export const useKeyBindings = (gameManager: UseGameType) => {
|
export const useKeyBindings = (gameManager: UseGameType) => {
|
||||||
const { gameStatus, startGame, stopGame, clearLog, resetCount } = gameManager;
|
const { gameStatus, startGame, stopGame, clearLog, resetCount, togglePause } = gameManager;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handler = (ev: KeyboardEvent) => {
|
const handler = (ev: KeyboardEvent) => {
|
||||||
@@ -22,6 +22,12 @@ export const useKeyBindings = (gameManager: UseGameType) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('ev.key', ev.key)
|
||||||
|
|
||||||
|
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") {
|
if (gameStatus === "started") {
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ export function useGame(cols: number, rows: number, platformHook?: unknown) {
|
|||||||
const foodManager = useFood(cols, rows, rendererManager);
|
const foodManager = useFood(cols, rows, rendererManager);
|
||||||
|
|
||||||
const gameManager = useGameStatus(rendererManager, snakeManager, foodManager);
|
const gameManager = useGameStatus(rendererManager, snakeManager, foodManager);
|
||||||
const { gameStatus, startGame, stopGame, score, level } = gameManager;
|
const { gameStatus, startGame, stopGame, togglePause, score, level } = gameManager;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
renderBoard(cols, rows);
|
renderBoard(cols, rows);
|
||||||
@@ -36,6 +36,7 @@ export function useGame(cols: number, rows: number, platformHook?: unknown) {
|
|||||||
gameStatus,
|
gameStatus,
|
||||||
startGame,
|
startGame,
|
||||||
stopGame,
|
stopGame,
|
||||||
|
togglePause,
|
||||||
score,
|
score,
|
||||||
level,
|
level,
|
||||||
...snakeManager,
|
...snakeManager,
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ export function useGameStatus(
|
|||||||
const [gameStatus, setGameStatus] = useState<GameStatus>("waiting");
|
const [gameStatus, setGameStatus] = useState<GameStatus>("waiting");
|
||||||
const [score, setScore] = useState(0);
|
const [score, setScore] = useState(0);
|
||||||
const [level, setLevel] = useState(1);
|
const [level, setLevel] = useState(1);
|
||||||
|
const [paused, setPaused] = useState(false);
|
||||||
const levelRef = useRef(1);
|
const levelRef = useRef(1);
|
||||||
const foodsEatenRef = useRef(0);
|
const foodsEatenRef = useRef(0);
|
||||||
const gameLoopRef = useRef<number | null>(null);
|
const gameLoopRef = useRef<number | null>(null);
|
||||||
@@ -60,7 +61,7 @@ export function useGameStatus(
|
|||||||
console.log("useGameStatus effect - gameStatus:", gameStatus);
|
console.log("useGameStatus effect - gameStatus:", gameStatus);
|
||||||
if (gameStatus === "started") {
|
if (gameStatus === "started") {
|
||||||
console.log("Starting game loop");
|
console.log("Starting game loop");
|
||||||
|
if (paused) return;
|
||||||
let shouldGrow = false;
|
let shouldGrow = false;
|
||||||
lastUpdateRef.current = performance.now();
|
lastUpdateRef.current = performance.now();
|
||||||
|
|
||||||
@@ -141,7 +142,7 @@ export function useGameStatus(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}, [gameStatus]);
|
}, [gameStatus, paused]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (gameStatus === "game-over") {
|
if (gameStatus === "game-over") {
|
||||||
@@ -157,6 +158,8 @@ export function useGameStatus(
|
|||||||
setGameStatus,
|
setGameStatus,
|
||||||
startGame,
|
startGame,
|
||||||
stopGame,
|
stopGame,
|
||||||
|
paused,
|
||||||
|
togglePause: () => setPaused(curr => !curr),
|
||||||
score,
|
score,
|
||||||
level,
|
level,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user