testsAndMisc/reverse_survivors/src/App.tsx
Krzysztof kuhy Rudnicki 5d0caa9725
Some checks failed
Pre-commit checks / pre-commit (push) Waiting to run
Shell tests / shell-tests (push) Waiting to run
Shell tests / arch-tests (push) Waiting to run
reverse-survivors CI / check (push) Has been cancelled
reverse_survivors: difficulty tiers
Haunting, Normal and Crusade scale enemy hp and speed, the survivor's
health, your income, and the length of the night. Normal is exactly 1.0 on
every multiplier — it is the identity tier, which is what lets every
existing raw-value assertion keep passing untouched.

Enemy speed now derives from a single helper, so difficulty and frenzy
compose in one place rather than fighting at three call sites.

Also fixes a bug this surfaced: handleRestart ignored the selected tier, so
restarting after choosing Crusade silently reverted to Normal. The
react-hooks purity rule flagged Date.now() in the new handler, and
extracting restartSeed() exposed the omission.

Endless mode is deliberately left out — fmtTime(Infinity) renders
Infinity:NaN, which would cost a guard plus tests for a mode whose only
content is the same game but longer.

README rewritten for all four features, including the reversal of its old
placement promise.

160 tests, 100% coverage.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VFyXexNjMQG9V7K8de17fc
2026-08-09 23:17:07 +02:00

103 lines
3.3 KiB
TypeScript

import type { ReactElement } from 'react'
import { useRef, useState } from 'react'
import { fmtTime } from './ui/format'
import { createInitialState, step } from './core/sim'
import type { HudSnapshot } from './core/snapshot'
import { snapshotOf } from './core/snapshot'
import type { DifficultyId, DirectorAction, GameState } from './core/types'
import { DIFFICULTIES, DIFFICULTY_ORDER } from './core/types'
import type { Ctx2D } from './render/draw'
import { draw } from './render/draw'
import { CanvasStage } from './ui/CanvasStage'
import { Hud } from './ui/Hud'
import { SpawnBar } from './ui/SpawnBar'
import { useGameLoop } from './ui/useGameLoop'
interface Props {
seed?: number
/** Test seam: mutate the freshly created state before the loop starts. */
boot?: (state: GameState) => void
}
/**
* Fresh seed for a restart. Extracted so the impurity lives outside render —
* the react-hooks purity rule rejects `Date.now()` inline in a component body.
*/
const restartSeed = (): number => Date.now() >>> 0
const createGame = (
seed: number,
boot: Props['boot'],
difficulty: DifficultyId = 'normal',
): GameState => {
const state = createInitialState(seed, undefined, difficulty)
if (boot !== undefined) {
boot(state)
}
return state
}
export const App = ({ seed = 20260802, boot }: Props): ReactElement => {
const [difficulty, setDifficulty] = useState<DifficultyId>('normal')
const gameRef = useRef<GameState>(createGame(seed, boot))
const [snap, setSnap] = useState<HudSnapshot>(() => snapshotOf(createGame(seed, boot)))
const ctxRef = useRef<Ctx2D | null>(null)
const queueRef = useRef<DirectorAction[]>([])
useGameLoop((dt) => {
const state = gameRef.current
step(state, queueRef.current, dt)
queueRef.current = []
const ctx = ctxRef.current
if (ctx !== null) {
draw(ctx, state)
}
setSnap(snapshotOf(state))
})
const handleAction = (action: DirectorAction): void => {
queueRef.current.push(action)
}
const handleRestart = (): void => {
gameRef.current = createGame(Date.now() >>> 0, undefined, difficulty)
queueRef.current = []
setSnap(snapshotOf(gameRef.current))
}
const handleDifficulty = (id: DifficultyId): void => {
setDifficulty(id)
gameRef.current = createGame(restartSeed(), undefined, id)
queueRef.current = []
setSnap(snapshotOf(gameRef.current))
}
const handleCtx = (ctx: Ctx2D | null): void => {
ctxRef.current = ctx
}
return (
<div className="frame">
<header className="masthead">
<h1>Reverse Survivors</h1>
<p className="tagline">You are the horde. The hero is the problem.</p>
<p className="clock">{fmtTime(snap.t)} / {fmtTime(snap.duration)}</p>
<div className="difficulty" role="group" aria-label="Difficulty">
{DIFFICULTY_ORDER.map((id) => (
<button
key={id}
type="button"
className={id === difficulty ? 'diff diff-on' : 'diff'}
onClick={() => { handleDifficulty(id) }}
>
{DIFFICULTIES[id].label}
</button>
))}
</div>
</header>
<main className="board">
<CanvasStage onCtx={handleCtx} />
<Hud snap={snap} onRestart={handleRestart} />
</main>
<SpawnBar snap={snap} onAction={handleAction} />
</div>
)
}