beep
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
import { createContext } from "react";
|
||||
import defaultGameContextValue from "./const";
|
||||
import { GameContextType } from "./types";
|
||||
|
||||
const GameContext = createContext<GameContextType>(defaultGameContextValue);
|
||||
|
||||
export default GameContext;
|
||||
@@ -0,0 +1,11 @@
|
||||
import useGameContext from "./useGameContext";
|
||||
import GameContext from "./GameContext";
|
||||
|
||||
const GameContextProvider = ({ children }) => {
|
||||
const contextValue = useGameContext();
|
||||
return (
|
||||
<GameContext.Provider value={contextValue}>{children}</GameContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export default GameContextProvider;
|
||||
@@ -0,0 +1,21 @@
|
||||
import { GameContextType } from "./types";
|
||||
import {
|
||||
defaultGameOptions,
|
||||
defaultGlobalConfigs,
|
||||
defaultRulesetName,
|
||||
} from "../internal";
|
||||
|
||||
const defaultGameContextValue: GameContextType = {
|
||||
currentGame: defaultRulesetName,
|
||||
gameSpecificConfigs: defaultGameOptions,
|
||||
globalConfigs: defaultGlobalConfigs,
|
||||
grid: null,
|
||||
paused: false,
|
||||
setCurrentGame: () => null,
|
||||
setGameSpecificConfigs: () => null,
|
||||
setGlobalConfigs: () => null,
|
||||
setGrid: () => null,
|
||||
togglePause: () => null,
|
||||
};
|
||||
|
||||
export default defaultGameContextValue;
|
||||
@@ -0,0 +1,6 @@
|
||||
import GameContext from "./GameContext";
|
||||
import GameContextProvider from "./GameContextProvider";
|
||||
import useGameContext from "./useGameContext";
|
||||
|
||||
export * from "./types";
|
||||
export { GameContext, GameContextProvider, useGameContext };
|
||||
@@ -0,0 +1,24 @@
|
||||
import { Dispatch, SetStateAction } from "react";
|
||||
import { GameSpecificConfig, Grid, GridConfig, RulesetName } from "../internal";
|
||||
|
||||
export type GameSpecificConfigs = { [key in RulesetName]: GameSpecificConfig };
|
||||
|
||||
export interface GlobalConfig {
|
||||
gridConfig: GridConfig;
|
||||
throttleAmount: number;
|
||||
}
|
||||
|
||||
export type GlobalConfigs = { [key in RulesetName]: GlobalConfig };
|
||||
|
||||
export interface GameContextType {
|
||||
currentGame: RulesetName;
|
||||
gameSpecificConfigs: GameSpecificConfigs;
|
||||
globalConfigs: GlobalConfigs;
|
||||
grid: Grid;
|
||||
paused: boolean;
|
||||
setCurrentGame: Dispatch<SetStateAction<RulesetName>>;
|
||||
setGameSpecificConfigs: Dispatch<SetStateAction<GameSpecificConfigs>>;
|
||||
setGlobalConfigs: Dispatch<SetStateAction<GlobalConfigs>>;
|
||||
setGrid: Dispatch<SetStateAction<Grid>>;
|
||||
togglePause: () => void;
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import { useCallback, useMemo, useState } from "react";
|
||||
import { GameContextType, GameSpecificConfigs, GlobalConfigs } from "./types";
|
||||
import defaultGameContextValue from "./const";
|
||||
import {
|
||||
createGrid,
|
||||
defaultRulesetName,
|
||||
Grid,
|
||||
defaultGlobalConfigs,
|
||||
defaultGameOptions,
|
||||
RulesetName,
|
||||
} from "../internal";
|
||||
|
||||
const useGameContext = (): GameContextType => {
|
||||
const [currentGame, setCurrentGame] =
|
||||
useState<RulesetName>(defaultRulesetName);
|
||||
const [globalConfigs, setGlobalConfigs] =
|
||||
useState<GlobalConfigs>(defaultGlobalConfigs);
|
||||
const [gameSpecificConfigs, setGameSpecificConfigs] =
|
||||
useState<GameSpecificConfigs>(defaultGameOptions);
|
||||
const [grid, setGrid] = useState<Grid>(
|
||||
createGrid(
|
||||
defaultRulesetName,
|
||||
defaultGlobalConfigs[defaultRulesetName],
|
||||
gameSpecificConfigs[defaultRulesetName]
|
||||
)
|
||||
);
|
||||
const [paused, setPaused] = useState(defaultGameContextValue.paused);
|
||||
|
||||
const togglePause = useCallback(() => {
|
||||
setPaused(!paused);
|
||||
}, [paused]);
|
||||
|
||||
return useMemo(
|
||||
() => ({
|
||||
currentGame,
|
||||
gameSpecificConfigs,
|
||||
globalConfigs,
|
||||
grid,
|
||||
paused,
|
||||
setCurrentGame,
|
||||
setGameSpecificConfigs,
|
||||
setGlobalConfigs,
|
||||
setGrid,
|
||||
togglePause,
|
||||
}),
|
||||
[
|
||||
currentGame,
|
||||
gameSpecificConfigs,
|
||||
globalConfigs,
|
||||
grid,
|
||||
paused,
|
||||
setCurrentGame,
|
||||
setGameSpecificConfigs,
|
||||
setGlobalConfigs,
|
||||
setGrid,
|
||||
togglePause,
|
||||
]
|
||||
);
|
||||
};
|
||||
|
||||
export default useGameContext;
|
||||
Reference in New Issue
Block a user