Replace the path->part-list store with a per-part map keyed by part id, so each placed part carries its path, stack index, and face. Derive each path's ordered children for stacking, and flip face-down parts in PartPlacement.
3.2 KiB
bgm-tabletop
a r3f based interactive component library to work with bgm board games. will be used somewhere in the web app's bgm inspector routes.
1. stack
react - react, react router, tailwindv4
r3f - r3f, drei, postprocessing
zustand - for state management
2. states
source-of-truth game state:
{
surfaces: Record<string, boolean>, // enabled per surface id
parts: Record<string, PartState>, // part id -> placement state
}
interface PartState {
path: string, // the path key this part is on
index: number, // the part's position in its path's stack
face: boolean, // whether the part's face is up
}
assumption: each piece on the board has a unique id, even tokens of the same type. so a part id appears at most once, and a path's ordered children (for stacking) are derived from the map by sorting on index. this makes the render list keyed by piece id stable and unambiguous.
derived surface render state: game state + surface routes => map of piece id to { surface, route, candidate, index, stackSize, face } for rendering on a surface. keys of this map makes a stable render list.
route- the matched route.candidate- the matched candidate for a:paramroute, carrying its anchorx/y/rotation. absent for routes without candidates.index- the piece's position in its path's stack.stackSize- the number of pieces on the path.face- whether the piece's face is up.
the render map is per enabled surface: a piece may appear on more than one enabled surface (e.g. an expansion path and the main board), and each is rendered independently.
3. components
SetupLoaderside effect only component that seeds the game state with setup (enabled surfaces + part placement).WorldSurfaceViewmounts a surface to world space.HudSurfaceViewmounts a surface to hud space.PartPlacementa stable per-part component that positions a part on a surface location. uses the stacking hook (below) to apply the route's stacking strategy.PartViewused inPartPlacement, creates a mesh from part definition. a standalone component library, so it reuses geometry/shape code from@tts/meshrather than the web app's viewers.
4. stacking
the format's stacking strategy (curve / limit / align / steps / tilt / zStart / zEnd, see bgm-format.md §4) is implemented as a hook, e.g. useStacking(route.stacking, index, stackSize), returning the offset/rotation to apply to a piece: { x, y, rotation, z, tilt }. x/y/rotation come from the curve; z is the surface-normal height ramped from zStart to zEnd; tilt is the rotation about the card's local Y (long) axis, applied to every part. PartPlacement consumes it.
5. usage
- we will inspect individual parts with
PartViewin the web app's part inspection route. - as a library, the public surface is the components above: mount a surface with
WorldSurfaceView/HudSurfaceView, seed state withSetupLoader, and letPartPlacement/PartViewrender the pieces. the web app is one consumer; the library should not assume the web app's routes or store. - a surface is mounted only when enabled; a disabled surface is not rendered.