Skip to content

Commit

Permalink
Merge pull request #26 from ThePhoenix08/responsive-2
Browse files Browse the repository at this point in the history
Made App Responsive and Dynamic
  • Loading branch information
prrockzed authored Dec 22, 2024
2 parents 39d2a8a + b2be7d1 commit 114a0fc
Show file tree
Hide file tree
Showing 14 changed files with 524 additions and 373 deletions.
2 changes: 1 addition & 1 deletion src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
display: grid;
place-content: center;
height: 100vh;
background-color: black;
background-color: var(--app-background-color);
}

1 change: 1 addition & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import './App.css'
import './mediaQueries.css'
import Arbiter from './components/Arbiter/Arbiter'

// The app component
Expand Down
5 changes: 1 addition & 4 deletions src/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import { PieceType, TeamType } from './Types'
export const VERTICAL_AXIS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
export const HORIZONTAL_AXIS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

// Grid Size
export const GRID_SIZE = 50

// InitialBoardState
export const initialBoard: Board = new Board(
[
Expand Down Expand Up @@ -87,7 +84,7 @@ export const initialBoard: Board = new Board(
new Piece(new Position(12, 9), PieceType.PAWN, TeamType.GREEN, false),
new Piece(new Position(12, 10), PieceType.PAWN, TeamType.GREEN, false),
],
0
1
)

initialBoard.calculateAllMoves()
52 changes: 30 additions & 22 deletions src/components/Arbiter/Arbiter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { initialBoard } from '../../Constants'
import { PieceType, TeamType } from '../../Types'
import { Piece, Position } from '../../models'
import { useRef, useState } from 'react'
import { ChessProvider } from '../context/ChessContext'
import ChessWrapper from '../ChessWrapper/ChessWrapper'

export default function Arbiter() {
// Declaring the constants
Expand Down Expand Up @@ -91,16 +93,19 @@ export default function Arbiter() {
}

// Deciding the type of color of the pieces when opening the modal
function promotionTeamType() {
if (promotionPawn?.team === TeamType.RED) {
return 'r'
} else if (promotionPawn?.team === TeamType.BLUE) {
return 'b'
} else if (promotionPawn?.team === TeamType.YELLOW) {
return 'y'
} else if (promotionPawn?.team === TeamType.GREEN) {
return 'g'
function promotionTeamType(): 'r' | 'b' | 'y' | 'g' {
if (promotionPawn && promotionPawn.team) {
if (promotionPawn.team === TeamType.RED) {
return 'r'
} else if (promotionPawn.team === TeamType.BLUE) {
return 'b'
} else if (promotionPawn.team === TeamType.YELLOW) {
return 'y'
} else if (promotionPawn.team === TeamType.GREEN) {
return 'g'
}
}
return 'r'; // defaulting to red team at initial state
}

// Writing the full name of the winning team
Expand All @@ -126,29 +131,29 @@ export default function Arbiter() {

setBoard(initialBoard.clone())
}

return (
<>
<div className='modal hidden' ref={modalRef}>
<div className='modal-body'>
<img
onClick={() => promotePawn(PieceType.ROOK)}
src={`${basePath}/assets/images/${promotionTeamType()}R.png`}
src={`${basePath}assets/images/${promotionTeamType()}R.png`}
alt='Rook'
/>
<img
onClick={() => promotePawn(PieceType.KNIGHT)}
src={`${basePath}/assets/images/${promotionTeamType()}N.png`}
src={`${basePath}assets/images/${promotionTeamType()}N.png`}
alt='Knight'
/>
<img
onClick={() => promotePawn(PieceType.BISHOP)}
src={`${basePath}/assets/images/${promotionTeamType()}B.png`}
src={`${basePath}assets/images/${promotionTeamType()}B.png`}
alt='Bishop'
/>
<img
onClick={() => promotePawn(PieceType.QUEEN)}
src={`${basePath}/assets/images/${promotionTeamType()}Q.png`}
src={`${basePath}assets/images/${promotionTeamType()}Q.png`}
alt='Queen'
/>
</div>
Expand All @@ -166,7 +171,7 @@ export default function Arbiter() {
<td>{teamNames[team]}</td>
<td>
<img
src={`${basePath}/assets/images/${team}${lbPieces[i]}.png`}
src={`${basePath}assets/images/${team}${lbPieces[i]}.png`}
alt={`${teamNames[team]} ${pieceNames[lbPieces[i]]}`}
/>
</td>
Expand All @@ -179,13 +184,16 @@ export default function Arbiter() {
</div>
</div>

<Chessboard
playMove={playMove}
pieces={board.pieces}
whoseTurn={board.currentTeam}
loseOrder={board.loseOrder}
isChecked={board.isChecked}
/>
<ChessProvider whoseTurn={board.currentTeam}>
<ChessWrapper loseOrder={board.loseOrder}>
<Chessboard
playMove={playMove}
pieces={board.pieces}
loseOrder={board.loseOrder}
isChecked={board.isChecked}
/>
</ChessWrapper>
</ChessProvider>
</>
)
}
81 changes: 81 additions & 0 deletions src/components/ChessWrapper/ChessWrapper.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
.game-layout {
display: flex;
flex-direction: column;
padding: 20px;
gap: 10px;
}

.game-layout > .middle-panel {
display: flex;
gap: 10px;
}

.game-layout > .panel-area,
.game-layout > .middle-panel > .panel-area {
display: flex;
align-items: center;
justify-content: center;
}

.middle-panels > .player-panel {
writing-mode: vertical-rl;
height: calc(var(--tile-size) * 8);
width: 0.5vw;
padding: 0 12px;
}

.game-layout > .panel-area > .player-panel {
height: 0.5vw;
width: calc(var(--tile-size) * 8);
padding: 12px 0;
}

div#left-panel {
transform: rotate(-180deg);
}

.player-panel {
display: flex;
justify-content: center;
align-items: center;
border-radius: calc(var(--tile-size) * 0.1);
}

.game-display {
display: none;
}

@media all and (max-width: 700px) {
/* phone styles here */
.game-layout {
padding: 4vh 4vw;
}

.player-panel > .player-name {
display: none;
}

.middle-panels > .player-panel {
padding: 0;
width: 5px;
border-radius: 1px;
}

.game-layout > .panel-area > .player-panel {
padding: 0;
height: 5px;
border-radius: 1px;
}

.game-display {
display: flex;
justify-content: center;
align-items: center;
}

.game-display > .currentPlayerDisplay {
color: var(--secondary-text-color);
padding: 2px 10px;
border-radius: calc(1rem + 2px);
}
}
102 changes: 102 additions & 0 deletions src/components/ChessWrapper/ChessWrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import './ChessWrapper.css'
import { useChessContext } from '../context/ChessContext'
import { TeamType } from '../../Types'

type PlayerState = 'active' | 'inactive' | 'lost';

/** Wrapper component for the chess board, holds player panels surrounding the board */
export default function ChessWrapper({
children,
loseOrder: lostTeams,
}: {
children: React.ReactNode;
loseOrder: TeamType[];
}) {
const { currentPlayer } = useChessContext();
const players = [
{ name: "Red Player", color: "red", id: "r" },
{ name: "Blue Player", color: "blue", id: "b" },
{ name: "Yellow Player", color: "yellow", id: "y" },
{ name: "Green Player", color: "green", id: "g" },
];

const getPlayerState = (playerId: TeamType): PlayerState => {
if (lostTeams.includes(playerId)) return 'lost'
return currentPlayer === playerId ? 'active' : 'inactive'
}

const displayBgColor = players.find((p) => p.id === currentPlayer)?.color || 'grey';

return (
<div className='chess-wrapper'>
<div className='game-display'>
<div
className='currentPlayerDisplay'
style={{ backgroundColor: displayBgColor }}
>
{players.find((p) => p.id === currentPlayer)?.name}
</div>
</div>
<div className='game-layout'>
<div className='panel-area'>
<PlayerPanel
name='Yellow Player'
color='yellow'
playerState={getPlayerState(TeamType.YELLOW)}
/>
</div>
<div className='middle-panel'>
<div className='panel-area middle-panels' id='left-panel'>
<PlayerPanel
name='Blue Player'
color='blue'
playerState={getPlayerState(TeamType.BLUE)}
/>
</div>
<div className='panel-area chessboard-container'>{children}</div>
<div className='panel-area middle-panels'>
<PlayerPanel
name='Green Player'
color='green'
playerState={getPlayerState(TeamType.GREEN)}
/>
</div>
</div>
<div className='panel-area'>
<PlayerPanel
name='Red Player'
color='red'
playerState={getPlayerState(TeamType.RED)}
/>
</div>
</div>
</div>
);
}

interface PlayerPanelProps {
name: string
color: string
playerState: PlayerState
}

/** Player panel component, holds player name and indicates active player */
function PlayerPanel({ name, color, playerState }: PlayerPanelProps) {
const stateToColorMap = {
active: color,
inactive: 'grey',
lost: '#444',
}

return (
<div
className={`player-panel ${playerState === 'active' ? 'active' : ''}`}
style={{
backgroundColor: stateToColorMap[playerState],
color: playerState === 'lost' ? "#eee" : "black"
}}
>
<div className='player-name'>{`${name} ${playerState === 'lost' ? 'lost' : ''}`}</div>
</div>
)
}
Loading

0 comments on commit 114a0fc

Please sign in to comment.