Introduce clean and commit action buttons
parent
8bbfe472c6
commit
7391d78c03
Binary file not shown.
|
After Width: | Height: | Size: 8.9 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 16 KiB |
|
|
@ -2,7 +2,7 @@ import React, { useState } from "react";
|
||||||
import GameBoard from "./components/GameBoard";
|
import GameBoard from "./components/GameBoard";
|
||||||
import DiceSet from "./components/DiceSet";
|
import DiceSet from "./components/DiceSet";
|
||||||
import "./GamePage.scss";
|
import "./GamePage.scss";
|
||||||
import { PieceId } from "interface";
|
import { buildBoard, PieceId } from "interface";
|
||||||
import { DieViewProps } from "./types/DieViewProps";
|
import { DieViewProps } from "./types/DieViewProps";
|
||||||
|
|
||||||
const GamePage = () => {
|
const GamePage = () => {
|
||||||
|
|
@ -11,18 +11,18 @@ const GamePage = () => {
|
||||||
return Object.values(PieceId)[randomId];
|
return Object.values(PieceId)[randomId];
|
||||||
};
|
};
|
||||||
|
|
||||||
const [dice, setDice] = useState(
|
const getRandomDiceSet: () => DieViewProps[] = () =>
|
||||||
[1, 2, 3, 4].map(() => {
|
[1, 2, 3, 4].map(() => {
|
||||||
const dieViewProps: DieViewProps = {
|
return {
|
||||||
pieceId: getRandomPieceId(),
|
pieceId: getRandomPieceId(),
|
||||||
isSelected: false,
|
isSelected: false,
|
||||||
isDisabled: false,
|
isDisabled: false,
|
||||||
isSpecial: false,
|
isSpecial: false,
|
||||||
rotation: 0,
|
rotation: 0,
|
||||||
};
|
};
|
||||||
return dieViewProps;
|
});
|
||||||
}),
|
|
||||||
);
|
const [dice, setDice] = useState(getRandomDiceSet());
|
||||||
const [specialDice, setSpecialDice] = useState(
|
const [specialDice, setSpecialDice] = useState(
|
||||||
[
|
[
|
||||||
PieceId.P03,
|
PieceId.P03,
|
||||||
|
|
@ -71,14 +71,40 @@ const GamePage = () => {
|
||||||
return dice.concat(specialDice).find((die) => matcher(die));
|
return dice.concat(specialDice).find((die) => matcher(die));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const [storedBoard, setStoredBoard] = useState(buildBoard());
|
||||||
|
const [board, setBoard] = useState(buildBoard());
|
||||||
|
const [id, setId] = useState(1);
|
||||||
|
const refreshBoardRender = () => {
|
||||||
|
setBoard(board);
|
||||||
|
setId(id + 1);
|
||||||
|
};
|
||||||
|
const resetBoard = () => {
|
||||||
|
setBoard(storedBoard);
|
||||||
|
modifyDieState(
|
||||||
|
() => true,
|
||||||
|
() => {
|
||||||
|
return { isSelected: false, isDisabled: false };
|
||||||
|
},
|
||||||
|
);
|
||||||
|
setId(id + 1);
|
||||||
|
};
|
||||||
|
const commitBoard = () => {
|
||||||
|
if (dice.some((die) => !die.isDisabled)) return;
|
||||||
|
setStoredBoard(board);
|
||||||
|
setDice(getRandomDiceSet());
|
||||||
|
setSpecialDieUsedInRound(false);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<React.Fragment>
|
<React.Fragment>
|
||||||
<h1>Game Page Title</h1>
|
<h1>Game Page Title</h1>
|
||||||
<div className="game-panel">
|
<div className="game-panel" id={id.toString()}>
|
||||||
<GameBoard
|
<GameBoard
|
||||||
modifyDieState={modifyDieState}
|
modifyDieState={modifyDieState}
|
||||||
fetchDie={fetchDie}
|
fetchDie={fetchDie}
|
||||||
setSpecialDieUsedInRound={setSpecialDieUsedInRound}
|
setSpecialDieUsedInRound={setSpecialDieUsedInRound}
|
||||||
|
refreshBoardRender={refreshBoardRender}
|
||||||
|
board={board}
|
||||||
/>
|
/>
|
||||||
<div className="right-panel">
|
<div className="right-panel">
|
||||||
<DiceSet
|
<DiceSet
|
||||||
|
|
@ -86,6 +112,8 @@ const GamePage = () => {
|
||||||
specialDice={specialDice}
|
specialDice={specialDice}
|
||||||
modifyDieState={modifyDieState}
|
modifyDieState={modifyDieState}
|
||||||
specialDieUsedInRound={specialDieUsedInRound}
|
specialDieUsedInRound={specialDieUsedInRound}
|
||||||
|
resetBoard={resetBoard}
|
||||||
|
commitBoard={commitBoard}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -9,13 +9,17 @@
|
||||||
box-shadow: 0 0 10px green;
|
box-shadow: 0 0 10px green;
|
||||||
border-radius: 20%;
|
border-radius: 20%;
|
||||||
padding: 5px;
|
padding: 5px;
|
||||||
width: 80px;
|
width: 70px;
|
||||||
height: 80px;
|
height: 70px;
|
||||||
margin-right: 10px;
|
margin-right: 10px;
|
||||||
|
|
||||||
&.icon-inverted {
|
&.icon-inverted {
|
||||||
transform: scaleX(-1);
|
transform: scaleX(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
&:last-child {
|
||||||
|
margin-right: 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,9 +11,18 @@ export interface DiceSetProps {
|
||||||
newStateComputer: (die: DieViewProps) => Partial<DieViewProps>,
|
newStateComputer: (die: DieViewProps) => Partial<DieViewProps>,
|
||||||
) => void;
|
) => void;
|
||||||
specialDieUsedInRound: boolean;
|
specialDieUsedInRound: boolean;
|
||||||
|
resetBoard: () => void;
|
||||||
|
commitBoard: () => void;
|
||||||
}
|
}
|
||||||
const DiceSet = (props: DiceSetProps) => {
|
const DiceSet = (props: DiceSetProps) => {
|
||||||
const { dice, specialDice, modifyDieState, specialDieUsedInRound } = props;
|
const {
|
||||||
|
dice,
|
||||||
|
specialDice,
|
||||||
|
modifyDieState,
|
||||||
|
specialDieUsedInRound,
|
||||||
|
resetBoard,
|
||||||
|
commitBoard,
|
||||||
|
} = props;
|
||||||
const handleDieClick = (clickedDie: DieViewProps) => {
|
const handleDieClick = (clickedDie: DieViewProps) => {
|
||||||
if (clickedDie.isDisabled) return;
|
if (clickedDie.isDisabled) return;
|
||||||
const isSpecialDie = clickedDie.isSpecial;
|
const isSpecialDie = clickedDie.isSpecial;
|
||||||
|
|
@ -51,6 +60,8 @@ const DiceSet = (props: DiceSetProps) => {
|
||||||
className="icon-inverted"
|
className="icon-inverted"
|
||||||
onClick={() => handleRotateButton(90)}
|
onClick={() => handleRotateButton(90)}
|
||||||
></img>
|
></img>
|
||||||
|
<img src="./clean-icon.png" onClick={resetBoard}></img>
|
||||||
|
<img src="./commit-icon.png" onClick={commitBoard}></img>
|
||||||
</div>
|
</div>
|
||||||
<div className="dice-set">
|
<div className="dice-set">
|
||||||
{dice.map((die) => (
|
{dice.map((die) => (
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
import { buildBoard } from "interface";
|
import { Cell } from "interface";
|
||||||
import "./GameBoard.scss";
|
import "./GameBoard.scss";
|
||||||
import { useState } from "react";
|
|
||||||
import BoardCell from "./BoardCell";
|
import BoardCell from "./BoardCell";
|
||||||
import { DieViewProps } from "../types/DieViewProps";
|
import { DieViewProps } from "../types/DieViewProps";
|
||||||
|
|
||||||
|
|
@ -13,26 +12,17 @@ export interface GameBoardProps {
|
||||||
matcher: (die: DieViewProps) => boolean,
|
matcher: (die: DieViewProps) => boolean,
|
||||||
) => DieViewProps | undefined;
|
) => DieViewProps | undefined;
|
||||||
setSpecialDieUsedInRound: React.Dispatch<React.SetStateAction<boolean>>;
|
setSpecialDieUsedInRound: React.Dispatch<React.SetStateAction<boolean>>;
|
||||||
|
refreshBoardRender: () => void;
|
||||||
|
board: Cell[][];
|
||||||
}
|
}
|
||||||
|
|
||||||
const GameBoard = (props: GameBoardProps) => {
|
const GameBoard = (props: GameBoardProps) => {
|
||||||
const [board, setBoard] = useState(buildBoard());
|
const { board } = props;
|
||||||
const [id, setId] = useState(1);
|
|
||||||
const refreshBoardRender = () => {
|
|
||||||
setBoard(board);
|
|
||||||
setId(id + 1);
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="game-board" id={id.toString()}>
|
<div className="game-board">
|
||||||
{board.flatMap((row) =>
|
{board.flatMap((row) =>
|
||||||
row.map((cell) => (
|
row.map((cell) => <BoardCell {...props} cell={cell} />),
|
||||||
<BoardCell
|
|
||||||
{...props}
|
|
||||||
cell={cell}
|
|
||||||
refreshBoardRender={refreshBoardRender}
|
|
||||||
/>
|
|
||||||
)),
|
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,6 @@ export interface DieViewProps {
|
||||||
pieceId: PieceId;
|
pieceId: PieceId;
|
||||||
isSelected: boolean;
|
isSelected: boolean;
|
||||||
isDisabled: boolean;
|
isDisabled: boolean;
|
||||||
isSpecial: boolean;
|
readonly isSpecial: boolean;
|
||||||
rotation: number;
|
rotation: number;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue