Skip to content

Commit

Permalink
Allow starting game with space
Browse files Browse the repository at this point in the history
  • Loading branch information
imkunet committed Mar 25, 2024
1 parent daba0c5 commit 111fa77
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/components/Board.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Accessor, For, Show } from 'solid-js';
import { Accessor, For, Show, onCleanup, onMount } from 'solid-js';
import { Grid } from '@/utils/grids';
import Mino from './Mino';
import { Motion } from 'solid-motionone';
import { Settings } from '@/utils/settings';

interface BoardProps {
twoMode: Accessor<boolean>;
Expand All @@ -11,6 +12,7 @@ interface BoardProps {
startTime: Accessor<number>;
inGame: Accessor<boolean>;
grid: Accessor<Grid>;
settings: Accessor<Settings>;

hoveredXY: () => [number, number] | null;
start: () => void;
Expand All @@ -32,6 +34,23 @@ export default function Board(props: BoardProps) {
};
};

onMount(() => {
const keyHandler = (event: KeyboardEvent) => {
if (props.startTime() != -1) return;
event.preventDefault();

if (event.altKey || event.ctrlKey) return;
if (event.key != ' ' && event.key != '1' && event.key != '2') return;
props.start();
};

document.addEventListener('keydown', keyHandler);

onCleanup(() => {
document.removeEventListener('keydown', keyHandler);
});
});

return (
<>
<Show when={props.audioCtx() == null || props.startTime() == -1}>
Expand All @@ -41,7 +60,7 @@ export default function Board(props: BoardProps) {
class="audio-request"
onClick={() => props.start()}
>
Click to start
{props.settings().alternateControlStyle ? 'Space to start' : 'Click to start'}
</Motion.button>
</Show>
<Show when={props.inGame()}>
Expand Down
1 change: 1 addition & 0 deletions src/pages/Game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ export default function Game() {
startTime={startTime}
inGame={inGame}
grid={grid}
settings={settings}
hoveredXY={hoveredXY}
start={start}
boardElement={boardElement!}
Expand Down

0 comments on commit 111fa77

Please sign in to comment.