Skip to content

Commit

Permalink
game: Code tidyup
Browse files Browse the repository at this point in the history
  • Loading branch information
TomNuttall committed Dec 8, 2024
1 parent 7dac13d commit 436ab4c
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 113 deletions.
8 changes: 7 additions & 1 deletion frontend/game/src/App.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
.app {
&__pixi {
display: flex;
justify-content: center;
flex-direction: column;
align-items: center;

h2 {
padding-top: 0.25rem;
margin: 0;
}
}

&__main {
Expand Down
47 changes: 18 additions & 29 deletions frontend/game/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import React, { useState } from 'react'
import { useState } from 'react'
import useGameState from './hooks/useGameState'
import PixiApp from './containers/PixiApp'
import CharacterSelect from './containers/CharacterSelect'
import GameMessage from './containers/GameMessage'
import Character from './components/Character'
import Header from './components/Header'
import Toast from './components/Toast'
import WaitPanel from './components/WaitPanel'
import { formatOrdinals } from './utils/helper'
import { CharacterSelectData } from './types'
import { GameState } from './hooks/useGameState'

Expand All @@ -19,31 +18,6 @@ const App: React.FC = () => {
})
const { gameData, sendMessage } = useGameState()

const renderGameState = (state: GameState) => {
switch (state) {
case GameState.CharacterSelect:
default:
return (
<CharacterSelect sendMessage={sendMessage} updatePlayer={setPlayer} />
)

case GameState.WaitPlayers:
return <WaitPanel msg={'Waiting for Players'} />

case GameState.WaitGame:
return <WaitPanel msg={'Game in Progress'} />

case GameState.Results:
return (
<WaitPanel
msg={`You finished ${formatOrdinals(
(gameData?.position ?? 0) + 1,
)}`}
/>
)
}
}

return (
<div className="app">
<Header
Expand All @@ -57,8 +31,23 @@ const App: React.FC = () => {
<PixiApp width={200} height={200}>
<Character width={200} height={200} player={player} />
</PixiApp>
<h2>
{player.name && player?.name?.length > 0 ? player.name : '-'}
</h2>
</div>
<div className="app__main">
{gameData.gameState === GameState.CharacterSelect ? (
<CharacterSelect
sendMessage={sendMessage}
updatePlayer={setPlayer}
/>
) : (
<GameMessage
gameState={gameData.gameState}
position={gameData?.position ?? 0}
/>
)}
</div>
<div className="app__main">{renderGameState(gameData.gameState)}</div>
</>
)}
</div>
Expand Down
15 changes: 13 additions & 2 deletions frontend/game/src/_form.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
&-row {
display: flex;
flex-direction: row;
gap: 0.5rem;
gap: 0.25rem;

label {
margin-bottom: 0 !important;
padding-right: 0.5rem;
}
}
}
Expand All @@ -45,6 +44,18 @@
}
}

fieldset {
padding: 0;
margin: 0;
border: none;
}

legend {
font-size: 0.9rem;
font-weight: 600;
margin-bottom: 0.25rem;
}

label {
font-size: 0.9rem;
font-weight: 600;
Expand Down
12 changes: 2 additions & 10 deletions frontend/game/src/components/Character/Character.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useContext } from 'react'
import { Container, Sprite } from '@pixi/react'
import { AssetContext } from '../../context/AssetContext'
import { TEXTURE_LOOKUP } from '../../defs'
import type { CharacterSelectData } from '../../types'

interface PlayerProps {
Expand All @@ -9,18 +10,9 @@ interface PlayerProps {
player: CharacterSelectData
}

const TEXTURE_LOOKUP: Record<string, string> = {
'character-1': 'penguin',
'character-2': 'snowman',
'character-3': 'dalek',
'character-4': 'hippo',
'character-5': 'reindeer',
'character-6': 'grinch',
}

const Character: React.FC<PlayerProps> = ({ width, height, player }) => {
const { textures, assetsLoaded } = useContext(AssetContext)
const textureName = TEXTURE_LOOKUP[`character-${player?.character ?? '0'}`]
const textureName = TEXTURE_LOOKUP[`character-${player?.character ?? '1'}`]

return (
<Container width={width} height={width}>
Expand Down
1 change: 0 additions & 1 deletion frontend/game/src/components/WaitPanel/WaitPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from 'react'
import './WaitPanel.scss'

interface WaitPanelProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
&__options {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
padding-bottom: 0.25rem;
padding: 0.5rem;
border-radius: 0.25rem;
border: 1px solid var(--brand-border);
}
}
88 changes: 19 additions & 69 deletions frontend/game/src/containers/CharacterSelect/CharacterSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import React, { useEffect } from 'react'
import { useEffect } from 'react'
import { useForm } from 'react-hook-form'
import { CHARACTER_OPTIONS } from '../../defs'
import type { CharacterSelectData } from '../../types'

import './CharacterSelect.scss'

const CHARACTER_OPTIONS: Record<string, string> = {
penguin: 'Penguin',
snowman: 'Snowman',
dalek: 'Dalek',
hippo: 'Hippo',
reindeer: 'Reindeer',
grinch: 'Grinch',
}

interface CharacterSelectProps {
sendMessage: (playerData: CharacterSelectData) => void
updatePlayer: (PlayerData: CharacterSelectData) => void
Expand Down Expand Up @@ -46,67 +38,25 @@ const CharacterSelect: React.FC<CharacterSelectProps> = ({
</label>
<input id="name" {...register('name', { required: true })} />
</div>
<div>
<fieldset>
<legend>Character</legend>
<div className="character-select__options">
<div className="form__input-row">
<input
type="radio"
id="penguin"
value={1}
defaultChecked={true}
{...register('character', { required: true })}
/>
<label htmlFor="penguin">Penguin</label>
</div>
<div className="form__input-row">
<input
type="radio"
id="snowman"
value={2}
{...register('character', { required: true })}
/>
<label htmlFor="snowman">Snowman</label>
</div>
<div className="form__input-row">
<input
type="radio"
id="dalek"
value={3}
{...register('character', { required: true })}
/>
<label htmlFor="dalek">Dalek</label>
</div>
{Object.keys(CHARACTER_OPTIONS).map((characterKey, index) => (
<div className="form__input-row" key={characterKey}>
<input
type="radio"
id={characterKey}
value={index + 1}
defaultChecked={index === 0 ? true : false}
{...register('character', { required: true })}
/>
<label htmlFor={characterKey}>
{CHARACTER_OPTIONS[characterKey]}
</label>
</div>
))}
</div>
<div className="character-select__options">
<div className="form__input-row">
<input
type="radio"
id="hippo"
value={4}
{...register('character', { required: true })}
/>
<label htmlFor="hippo">Hippo</label>
</div>
<div className="form__input-row">
<input
type="radio"
id="reindeer"
value={5}
{...register('character', { required: true })}
/>
<label htmlFor="reindeer">Reindeer</label>
</div>
<div className="form__input-row">
<input
type="radio"
id="grinch"
value={6}
{...register('character', { required: true })}
/>
<label htmlFor="grinch">Grinch</label>
</div>
</div>
</div>
</fieldset>
<div className="form__input character-select__tint">
<label className="label" htmlFor="tint">
Tint
Expand Down
23 changes: 23 additions & 0 deletions frontend/game/src/containers/GameMessage/GameMessage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import WaitPanel from '../../components/WaitPanel'
import { formatOrdinals } from '../../utils/helper'
import { GameState } from '../../hooks/useGameState'

interface GameMessageProps {
gameState: GameState
position: number
}

const GameMessage: React.FC<GameMessageProps> = ({ gameState, position }) => {
switch (gameState) {
case GameState.WaitPlayers:
return <WaitPanel msg={'Waiting for Players'} />

case GameState.WaitGame:
return <WaitPanel msg={'Game in Progress'} />

case GameState.Results:
return <WaitPanel msg={`You finished ${formatOrdinals(position + 1)}`} />
}
}

export default GameMessage
2 changes: 2 additions & 0 deletions frontend/game/src/containers/GameMessage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import GameMessage from './GameMessage'
export default GameMessage
17 changes: 17 additions & 0 deletions frontend/game/src/defs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const CHARACTER_OPTIONS: Record<string, string> = {
penguin: 'Penguin',
snowman: 'Snowman',
dalek: 'Dalek',
hippo: 'Hippo',
reindeer: 'Reindeer',
grinch: 'Grinch',
}

export const TEXTURE_LOOKUP: Record<string, string> = {
'character-1': 'penguin',
'character-2': 'snowman',
'character-3': 'dalek',
'character-4': 'hippo',
'character-5': 'reindeer',
'character-6': 'grinch',
}

0 comments on commit 436ab4c

Please sign in to comment.