starting new structure
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
export { useGameStatus } from './useGameStatus';
|
||||
export type { GameStatus, GameStatusManager } from './types';
|
||||
@@ -0,0 +1,9 @@
|
||||
export type GameStatus = 'waiting' | 'started' | 'paused' | 'game-over' | 'game-won';
|
||||
|
||||
export type GameStatusManager = {
|
||||
gameStatus: GameStatus;
|
||||
setGameStatus: (status: GameStatus) => void;
|
||||
startGame: () => void;
|
||||
togglePause: (pause?: boolean) => void;
|
||||
quitGame: () => void;
|
||||
};
|
||||
@@ -0,0 +1,32 @@
|
||||
import { useState, useCallback } from 'react';
|
||||
import type { GameStatusManager, GameStatus } from './types';
|
||||
|
||||
export function useGameStatus(): GameStatusManager {
|
||||
const [gameStatus, setGameStatus] = useState<GameStatus>('waiting');
|
||||
|
||||
const startGame = useCallback(() => {
|
||||
setGameStatus('started');
|
||||
}, []);
|
||||
|
||||
const togglePause = useCallback((pause?: boolean) => {
|
||||
setGameStatus(current => {
|
||||
if (current === 'started' || current === 'paused') {
|
||||
return pause !== undefined ? (pause ? 'paused' : 'started') :
|
||||
(current === 'started' ? 'paused' : 'started');
|
||||
}
|
||||
return current;
|
||||
});
|
||||
}, []);
|
||||
|
||||
const quitGame = useCallback(() => {
|
||||
setGameStatus('waiting');
|
||||
}, []);
|
||||
|
||||
return {
|
||||
gameStatus,
|
||||
setGameStatus,
|
||||
startGame,
|
||||
togglePause,
|
||||
quitGame
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user