Skip to content

Commit

Permalink
add grid markings
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanquirino committed Sep 18, 2023
1 parent 2d19d56 commit e81563e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
33 changes: 19 additions & 14 deletions src/blockpuzzle/components/Game.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
import { forwardRef, useEffect } from "react";
import { useGameStore } from "../store";
import UnitBlock from "./UnitBlock";
import {
acceptedKeys,
idleInput,
} from "../constants";
import { acceptedKeys, idleInput } from "../constants";
import Status from "./Status";
import GridBlock from "./GridBlock";

interface GameProps {
width: number;
height: number;
blockSize: number
blockSize: number;
}

const Game = forwardRef<HTMLDivElement, GameProps>(function Game(props, ref) {
const { width, height, blockSize } = props;
const { width, height, blockSize } = props;

const grid = useGameStore((state) => state.grid);

const input = useGameStore((state) => state.input);
const current = useGameStore((state) => state.current);
const currentPieceId = useGameStore((state) => state.currentPieceId);
const isReady = useGameStore(state => state.status !== "loading")

const isReady = useGameStore((state) => state.status !== "loading");

useEffect(() => {
const keyboardHandler = (event: KeyboardEvent) => {
Expand Down Expand Up @@ -90,24 +88,31 @@ const Game = forwardRef<HTMLDivElement, GameProps>(function Game(props, ref) {
{grid.map(
(row, y) =>
y > 1 &&
row.map(
(col, x) =>
col && (
row.map((col, x) => (
<>
<GridBlock
key={`g${x}${y}`}
x={x}
y={y - 2}
size={blockSize}
/>
{col && (
<UnitBlock
key={`${y}${x}`}
x={x}
y={y - 2}
size={blockSize}
color={col}
/>
)
)
)}
</>
))
)}
</div>
)}
</div>
</div>
);
})
});

export default Game;
26 changes: 26 additions & 0 deletions src/blockpuzzle/components/GridBlock.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import classNames from "classnames";
import { Block } from "../types";

type GridBlockProps = Omit<Block, "color">;

function GridBlock(props: GridBlockProps) {
const { x, y, size } = props;

const classes = classNames(
"bg-transparent aspect-square border-[1px] border-white absolute z-0 opacity-10",
);

return (
<div
style={{
left: x * size,
top: y * size,
width: size,
height: size,
}}
className={classes}
/>
);
}

export default GridBlock;
2 changes: 1 addition & 1 deletion src/blockpuzzle/components/UnitBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function UnitBlock(props: Block) {
const { x, y, size, color } = props;

const classes = classNames(
"bg-white rounded aspect-square border-[4px] absolute unit-block",
"bg-white rounded aspect-square border-[4px] absolute unit-block z-50",
styles.unitBlock,
colors[color]
);
Expand Down

0 comments on commit e81563e

Please sign in to comment.