Skip to content

Commit

Permalink
game states: play, playing, played
Browse files Browse the repository at this point in the history
  • Loading branch information
Grzegorz Tańczyk committed Jul 10, 2024
1 parent 0bbe390 commit 39ab87f
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 7 deletions.
10 changes: 9 additions & 1 deletion games/nukes/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { BrowserRouter, Routes, Route, useNavigate } from 'react-router-dom';
import './App.css';
import { GameState } from './game-states/types';
import { GameStateIntro } from './game-states/state-intro';

import { GameStatePlay } from './game-states/state-play';
import { GameStatePlaying } from './game-states/state-playing';
import { GameStatePlayed } from './game-states/state-played';

import { GameStateTechWorld } from './game-states/state-tech-world';

function App() {
Expand All @@ -11,6 +16,9 @@ function App() {
<BrowserRouter basename="/">
<Routes>
<Route path={GameStateIntro.path} element={<GameStateRoute state={GameStateIntro} />} />
<Route path={GameStatePlay.path} element={<GameStateRoute state={GameStatePlay} />} />
<Route path={GameStatePlaying.path} element={<GameStateRoute state={GameStatePlaying} />} />
<Route path={GameStatePlayed.path} element={<GameStateRoute state={GameStatePlayed} />} />
<Route path={GameStateTechWorld.path} element={<GameStateRoute state={GameStateTechWorld} />} />
</Routes>
</BrowserRouter>
Expand All @@ -21,7 +29,7 @@ function App() {
function GameStateRoute({ state }: { state: GameState }) {
const navigate = useNavigate();

return <state.Component setGameState={(state) => navigate(state.path)} />;
return <state.Component setGameState={(state, params) => navigate(state.path, { state: params })} />;
}

export default App;
6 changes: 3 additions & 3 deletions games/nukes/src/game-states/state-intro.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { GameState, GameStateComponent } from './types';
import { GameStateTechWorld } from './state-tech-world';
import { GameStatePlay } from './state-play';

const Intro: GameStateComponent = ({ setGameState }) => {
return (
<>
<h3>intro</h3>
<h3>Nukes game</h3>
<br />
<button onClick={() => setGameState(GameStateTechWorld)}>Nuke world demo</button>
<button onClick={() => setGameState(GameStatePlay)}>Play</button>
</>
);
};
Expand Down
31 changes: 31 additions & 0 deletions games/nukes/src/game-states/state-play.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useState } from 'react';
import { GameState, GameStateComponent } from './types';
import { GameStatePlaying } from './state-playing';

const PlayComponent: GameStateComponent = ({ setGameState }) => {
const [stateName, setStateName] = useState('');

const handlePlay = () => {
setGameState(GameStatePlaying, { stateName, gameId: String(Date.now()) });
};

return (
<>
<input
type="text"
placeholder="Name your state"
value={stateName}
onChange={(event) => setStateName(event.currentTarget.value)}
/>

<button onClick={handlePlay} disabled={!stateName}>
Play
</button>
</>
);
};

export const GameStatePlay: GameState = {
Component: PlayComponent,
path: '/play',
};
10 changes: 10 additions & 0 deletions games/nukes/src/game-states/state-played.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { GameState, GameStateComponent } from './types';

const PlayedComponent: GameStateComponent = ({}) => {
return <>Game over, results</>;
};

export const GameStatePlayed: GameState = {
Component: PlayedComponent,
path: '/playing',
};
20 changes: 20 additions & 0 deletions games/nukes/src/game-states/state-playing.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useLocation } from 'react-router';
import { GameState, GameStateComponent } from './types';

const PlayingComponent: GameStateComponent = ({}) => {
const {
state: { stateName, gameId },
} = useLocation();

return (
<>
Playing: {stateName} {gameId}
Render world
</>
);
};

export const GameStatePlaying: GameState = {
Component: PlayingComponent,
path: '/playing',
};
2 changes: 1 addition & 1 deletion games/nukes/src/game-states/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type GameStateProps = {
setGameState: (gameState: GameState) => void;
setGameState: (gameState: GameState, params?: Record<string, string>) => void;
};

export type GameStateComponent = React.FunctionComponent<GameStateProps>;
Expand Down
4 changes: 2 additions & 2 deletions games/nukes/src/world-render/world-state-render.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import React from 'react';
import styled from 'styled-components';

import { WorldState } from '../world/world-state-types';
import { usePointerMove } from '../controls/pointer';
import { dispatchCustomEvent } from '../events';

import { SectorRender } from './sector-render';
import { StateRender } from './state-render';
import { CityRender } from './city-render';
import { LaunchSiteRender } from './launch-site-render';
import { MissileRender } from './missile-render';
import { ExplosionRender } from './explosion-render';
import { dispatchCustomEvent } from '../events';
import React from 'react';

export function WorldStateRender({ state }: { state: WorldState }) {
const pointerMove = usePointerMove();
Expand Down

0 comments on commit 39ab87f

Please sign in to comment.