Skip to content

Commit

Permalink
feat: ✨ Basic Game Logic Working
Browse files Browse the repository at this point in the history
  • Loading branch information
skyrunner360 committed Apr 13, 2024
1 parent cae40cb commit d49dc48
Show file tree
Hide file tree
Showing 14 changed files with 206 additions and 146 deletions.
Binary file modified bun.lockb
Binary file not shown.
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,22 @@
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"react-icons": "^5.0.1",
"recoil": "^0.7.7"
},
"devDependencies": {
"@types/react": "^18.2.66",
"@types/react-dom": "^18.2.22",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"@typescript-eslint/parser": "^7.2.0",
"@vitejs/plugin-react-swc": "^3.5.0",
"autoprefixer": "^10.4.19",
"eslint": "^8.57.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.6",
"postcss": "^8.4.38",
"tailwindcss": "^3.4.3",
"typescript": "^5.2.2",
"vite": "^5.2.0"
}
Expand Down
6 changes: 6 additions & 0 deletions postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
42 changes: 0 additions & 42 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,42 +0,0 @@
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.react:hover {
filter: drop-shadow(0 0 2em #61dafbaa);
}

@keyframes logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}

@media (prefers-reduced-motion: no-preference) {
a:nth-of-type(2) .logo {
animation: logo-spin infinite 20s linear;
}
}

.card {
padding: 2em;
}

.read-the-docs {
color: #888;
}
43 changes: 16 additions & 27 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,24 @@
import { useState } from 'react'
import reactLogo from './assets/react.svg'
import viteLogo from '/vite.svg'
import './App.css'
import { useState } from "react";
import PlayerInfo from "./Components/PlayerInfo";
import Board from "./Components/Board";
import Dice from "./Components/Dice";

function App() {
const [count, setCount] = useState(0)

return (
<>
<div>
<a href="https://vitejs.dev" target="_blank">
<img src={viteLogo} className="logo" alt="Vite logo" />
</a>
<a href="https://react.dev" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h1>Vite + React</h1>
<div className="card">
<button onClick={() => setCount((count) => count + 1)}>
count is {count}
</button>
<p>
Edit <code>src/App.tsx</code> and save to test HMR
</p>
<div className="grid grid-cols-3 p-2">
<div className="border border-red-500 p-2">
<PlayerInfo />
</div>
<div className="border border-red-500 p-2">
<Board />
</div>
<div className="border border-red-500 p-2">
<Dice/>
</div>
</div>
<p className="read-the-docs">
Click on the Vite and React logos to learn more
</p>
</>
)
);
}

export default App
export default App;
7 changes: 7 additions & 0 deletions src/Components/Board.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from "react";

const Board = () => {
return <div>Board</div>;
};

export default Board;
67 changes: 67 additions & 0 deletions src/Components/Dice.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React from "react";
import { useRecoilState } from "recoil";
import { diceValue } from "../states/DiceStates";
import { diceRoll, DiceValToIconMap } from "../Utils";
import {
currentPlayer,
gameLogs,
gameState,
playerPositions,
} from "../states/BoardStates";

const Dice = () => {
const [diceVal, setDiceVal] = useRecoilState(diceValue);
const [gameLog, setGameLog] = useRecoilState(gameLogs);
const [currPlayer, setCurrPlayer] = useRecoilState(currentPlayer);
const [playerPos, setPlayerPos] = useRecoilState(playerPositions);
const [gState, setGState] = useRecoilState(gameState);

const rollDice = () => {
const newVal = diceRoll();
const newPlayer = currPlayer === 1 ? 2 : 1;
const isPlayerFree = newVal === 6;
setDiceVal(newVal);
setGameLog([
...gameLog,
`Player ${currPlayer} Rolled Dice to ${newVal}`,
...(isPlayerFree ? [`Player ${currPlayer} is Free to Move`] : []),
`Player ${newPlayer}'s Turn`,
]);
setCurrPlayer(newPlayer);
setPlayerPos({
player1:
currPlayer === 1 && isPlayerFree && !gState["player1"].move
? newVal
: gState["player1"].move && currPlayer === 1
? playerPos.player1 + newVal
: playerPos.player1,
player2:
currPlayer === 2 && isPlayerFree && !gState["player2"].move
? newVal
: gState["player2"].move && currPlayer === 2
? playerPos.player2 + newVal
: playerPos.player2,
});
if (isPlayerFree) {
setGState({ ...gState, [`player${currPlayer}`]: { move: true } });
} else {
setGState({ ...gState, global: "started" });
}
console.log("Game states", gState);
};

return (
<div>
<div className="flex justify-center content-center items-center">
<div className="cursor-pointer" onClick={rollDice}>
{DiceValToIconMap[diceVal]}
</div>
</div>
<div className="grid">
<i className="justify-self-center">Click to Roll Dice</i>
</div>
</div>
);
};

export default Dice;
23 changes: 23 additions & 0 deletions src/Components/PlayerInfo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from "react";
import { useRecoilState, useRecoilValue } from "recoil";
import { gameLogs, playerPositions } from "../states/BoardStates";

const PlayerInfo = () => {
const [playerPos, setPlayerPos] = useRecoilState(playerPositions);
const gLogs = useRecoilValue(gameLogs);
return (
<div>
<h3>Game Logs</h3>
<div className="border border-gray-500 overflow-auto h-20">
{gLogs?.map((log) => (
<p className="p-1">{log}</p>
))}
</div>
<h3>Player Positions</h3>
<p>Player 1 :- {playerPos.player1}</p>
<p>Player 2 :- {playerPos.player2}</p>
</div>
);
};

export default PlayerInfo;
21 changes: 21 additions & 0 deletions src/Utils/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { IoDice } from "react-icons/io5";
import { FaDiceOne } from "react-icons/fa6";
import { FaDiceTwo } from "react-icons/fa6";
import { FaDiceThree } from "react-icons/fa6";
import { FaDiceFour } from "react-icons/fa";
import { FaDiceFive } from "react-icons/fa";
import { FaDiceSix } from "react-icons/fa6";

export const DiceValToIconMap = {
0: <IoDice size={150} />,
1: <FaDiceOne size={150} />,
2: <FaDiceTwo size={150} />,
3: <FaDiceThree size={150} />,
4: <FaDiceFour size={150} />,
5: <FaDiceFive size={150} />,
6: <FaDiceSix size={150} />,
};

export const diceRoll = () => {
return Math.trunc(Math.random() * 6 + 1);
};
71 changes: 3 additions & 68 deletions src/index.css
Original file line number Diff line number Diff line change
@@ -1,68 +1,3 @@
:root {
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
line-height: 1.5;
font-weight: 400;

color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;

font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}

body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}

h1 {
font-size: 3.2em;
line-height: 1.1;
}

button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}

@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
}
@tailwind base;
@tailwind components;
@tailwind utilities;
19 changes: 11 additions & 8 deletions src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.tsx'
import './index.css'
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App.tsx";
import "./index.css";
import { RecoilRoot } from "recoil";

ReactDOM.createRoot(document.getElementById('root')!).render(
ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<App />
</React.StrictMode>,
)
<RecoilRoot>
<App />
</RecoilRoot>
</React.StrictMode>
);
32 changes: 32 additions & 0 deletions src/states/BoardStates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { atom } from "recoil";

export const playerPositions = atom({
key: "playerPositions",
default: {
player1: 0,
player2: 0,
},
});

export const gameLogs = atom({
key: "gameLogs",
default: ["Player 1 Click Dice to Start Game"],
});

export const currentPlayer = atom({
key: "currentPlayer",
default: 1,
});

export const gameState = atom({
key: "gameState",
default: {
global: "notStarted", // notStarted || started || over
player1: {
move: false,
},
player2: {
move: false,
},
},
});
6 changes: 6 additions & 0 deletions src/states/DiceStates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { atom } from "recoil";

export const diceValue = atom({
key: "diceValue",
default: 0,
});
8 changes: 8 additions & 0 deletions tailwind.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @type {import('tailwindcss').Config} */
export default {
content: ["./index.html", "./src/**/*.{js,ts,jsx,tsx}"],
theme: {
extend: {},
},
plugins: [],
};

0 comments on commit d49dc48

Please sign in to comment.