I’ve built the foundational Red Dead Redemption 1‑style Liar’s Dice game as a React + Vite + Tailwind CSS + Three.js/WebGL application, using @react-three/rapier for deterministic, CCD-enabled physics.
The project compiles successfully (npm run build passes) and the generated dist/index.html is ready to be served.
1. Engine & Stack Choice
| Layer | Technology | Reason |
|---|
| Renderer / 3D | Three.js via @react-three/fiber | Web-native, easy React integration |
| Physics | Rapier via @react-three/rapier | Deterministic, built-in CCD, sleep, and thick-collider support |
| UI | React + Tailwind CSS v4 | Fast diegetic/HUD layout, western theme |
| Build | Vite | Fast dev + single-file production output |
2. Overall Architecture
App.tsx
└── Scene.tsx // 3D saloon: table, cup, dice physics
└── GameUI.tsx // HUD, bidding menu, banners, opponent cards
└── useGameController // state machine + game rules + timers
├── types.ts // Player, Bid, Die, GameState, Phases
├── rules.ts // legal bids, Liar/Spot On resolution, penalties
└── botAI.ts // probability-based AI with bluffing
Game State Machine
RoundStart → Turn (Human or Bot) → Reveal → RoundEnd → RoundStart | GameOver
roundStart – all players are dealt 5 dice (or their current count); only the human dice are physically rolled in the 3D cup. Once the dice settle, the read values are written back into state and play begins.
turn – current player must Raise, Call Liar, or Call Spot On. Human sees a validated bidding UI; bots use probability math.
reveal – all dice are counted, a cinematic banner shows the result, and the loser is determined.
roundEnd – penalties are applied, eliminations checked, and the next round is dealt. The player who lost a die (or the successful Spot On caller) opens the next round.
3. Core Game Loop (Hook)
useGameController.ts owns everything:
startGame(opponents) creates 1 human + N bots, each with 5 dice.
dealRound() regenerates Die[] arrays per player every round.
onRollComplete(humanDice) is invoked by the physics layer after the human dice settle; it overwrites the human hand with the real rolled faces.
humanBid / humanCallLiar / humanCallSpotOn are the player actions.
- A
useEffect schedules bot turns with a deliberate think delay (~1.2s) so AI moves are readable.
- Another
useEffect drives the timed reveal → roundEnd → next round / gameOver transitions.
Strict RDR1 rule validation lives in rules.ts:
- No wilds;
1s count as 1.
- Legal raises: higher quantity of any face, OR same quantity with a higher face.
- Liar resolution: actual count
< bid → bidder loses; else caller loses.
- Spot On resolution: exact → everyone except caller loses; otherwise caller loses.
4. Bot AI (botAI.ts)
Each bot computes:
- Own known count of the bid face.
- Unknown dice =
totalDice - ownDice.
- Expected count =
ownCount + unknownDice / 6.
Decision heuristics:
- If bid is well above expected → call Liar.
- If bid is very close to expected → occasionally call Spot On.
- Otherwise → Raise with a scored legal bid (prefers faces it actually holds, small raises, with an 18% chance to bluff higher).
5. Physics Setup & Optimization
Strict Dice-in-Cup Physics
- CCD enabled on every die:
<RigidBody ccd …>.
- Fixed time step:
<Physics timeStep={1/60} gravity={[0, -9.81, 0]}>.
- Thick cup collider: the cup is built from a ring of
CuboidCollider walls plus a solid CylinderCollider bottom, not a thin mesh collider, so dice cannot tunnel through.
- Damping: high linear/angular damping (
0.45) so dice settle quickly.
- Sleep:
canSleep is enabled; once all dice report isSleeping(), the round proceeds.
- Fallback timer: if dice somehow never fully sleep, the roller auto-finalizes after
4.5s.
Performance
- Dice use
useMemo-created canvas textures (6 per die) and are destroyed/re-created only on new rounds.
- Only the cup and dice cast dynamic shadows; the saloon table is a static shadow-receiver.
- Multi-light setup uses a warm key light + cool fill + rim + lantern point light so the scene is never muddy.
- UI updates are batched; game state is kept flat and immutable.
Lighting for Clarity
- Warm ambient (
#ffd8a6)
- Strong directional key (
#ffecd2) from front-left with shadows
- Cool fill from back-right
- Overhead lantern point light + spot rim light
6. Core Scripts / File Map
| File | Responsibility |
|---|
src/App.tsx | Mounts Scene + GameUI |
src/game/types.ts | Type definitions for all game entities |
src/game/rules.ts | Legal-bid generation, Liar/Spot On resolution, penalties |
src/game/botAI.ts | Probability-based opponent AI |
src/game/useGameController.ts | State machine, timers, round flow |
src/components/Scene.tsx | R3F canvas, lights, physics world, table |
src/components/Cup.tsx | Visual leather cup + thick physics colliders |
src/components/Die3D.tsx | Individual rigid die with face textures |
src/components/DiceRoller.tsx | Spawns dice, waits for sleep, reads top faces |
src/components/GameUI.tsx | HUD, opponent cards, bidding menu, banners, menus |
src/components/DieIcon.tsx | 2D SVG die icons for UI |
src/utils/dice3d.ts | Face normals, texture generation, rotation helpers, top-face reader |
src/index.css | Tailwind + western theme + animation keyframes |
index.html | Title + Rye font import |
7. How to Run
npm install
npm run dev # local development
npm run build # production build (already verified)
The build produces a self-contained dist/index.html.
8. What’s Implemented vs. Next Polish
Already working:
- Full RDR1 ruleset (no wilds, legal raises, Liar, Spot On, elimination)
- 2–4 players (1 human vs 1–3 bots)
- 3D saloon table, warm clear lighting, leather cup
- Physics-driven human dice roll with CCD
- Probability-based AI with deliberate pacing
- Validated bidding UI
- Cinematic action/reveal banners
- Game-over restart flow
Suggested next steps:
- Replace the abstract opponent cards with 3D character busts/cups around the table.
- Add a leather-shaking animation for the human cup before dice settle.
- Add audio (cup thud, dice rattle, “Liar!” callouts).
- Bake a lightmap for the static saloon geometry and disable real-time shadows on the room.
- Implement object-pooled dice instantiation if you scale to many players/cups.
Files built: index.html, src/index.css, src/game/types.ts, src/game/rules.ts, src/game/botAI.ts, src/game/useGameController.ts, src/components/DieIcon.tsx, src/utils/dice3d.ts, src/components/Die3D.tsx, src/components/Cup.tsx, src/components/DiceRoller.tsx, src/components/Scene.tsx, src/components/GameUI.tsx, src/App.tsx