feat(mystery): mobile chip computation and rendering
Pure computeChips function (TDD, 4 tests) generates context-aware direction/item/encounter/meta chips from game state; chip-render.ts wires chips to DOM; terminal.ts calls refreshChips on init, each Enter dispatch, restart, and undo. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
import type { World } from '../world/types'
|
||||
import type { GameState, Direction } from '../engine/types'
|
||||
|
||||
export type ChipKind = 'direction' | 'item' | 'encounter' | 'meta'
|
||||
|
||||
export interface Chip {
|
||||
kind: ChipKind
|
||||
label: string
|
||||
command: string // the literal string to inject as input
|
||||
disabled: boolean
|
||||
}
|
||||
|
||||
const DIRECTION_LABELS: Record<Direction, string> = {
|
||||
n: '↑ N', s: '↓ S', e: '→ E', w: '← W', u: '↑ U', d: '↓ D',
|
||||
}
|
||||
|
||||
export function computeChips(state: GameState, world: World): Chip[] {
|
||||
const out: Chip[] = []
|
||||
const room = world.rooms[state.location]
|
||||
if (!room) return out
|
||||
|
||||
// Direction chips: enabled if exit exists, dimmed otherwise.
|
||||
const dirs: Direction[] = ['n', 's', 'e', 'w', 'u', 'd']
|
||||
for (const d of dirs) {
|
||||
const present = !!room.exits[d]
|
||||
if (present || ['n', 's', 'e', 'w'].includes(d)) {
|
||||
out.push({
|
||||
kind: 'direction',
|
||||
label: DIRECTION_LABELS[d],
|
||||
command: d,
|
||||
disabled: !present,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Item chips: TAKE for visible items.
|
||||
for (const itemId of room.items) {
|
||||
const item = world.items[itemId]
|
||||
if (!item || !item.takeable) continue
|
||||
out.push({
|
||||
kind: 'item',
|
||||
label: `TAKE ${item.names[0]?.toUpperCase() ?? itemId.toUpperCase()}`,
|
||||
command: `take ${item.names[0] ?? itemId}`,
|
||||
disabled: false,
|
||||
})
|
||||
}
|
||||
|
||||
// Encounter chips: surface the verbs from the current phase as suggestions.
|
||||
if (room.encounter && state.encounterState[room.encounter]) {
|
||||
const def = world.encounters[room.encounter]
|
||||
const phase = def?.phases[state.encounterState[room.encounter]!]
|
||||
if (def && phase) {
|
||||
for (const t of phase.transitions) {
|
||||
const targetLabel = t.target && t.target !== '*' ? ` ${t.target.toUpperCase()}` : ''
|
||||
const command = t.target && t.target !== '*' ? `${t.verb} ${t.target}` : t.verb
|
||||
out.push({
|
||||
kind: 'encounter',
|
||||
label: `${t.verb.toUpperCase()}${targetLabel}`,
|
||||
command,
|
||||
disabled: false,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Persistent meta chips.
|
||||
out.push({ kind: 'meta', label: 'LOOK', command: 'look', disabled: false })
|
||||
out.push({ kind: 'meta', label: 'INV', command: 'inventory', disabled: false })
|
||||
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user