From 464ba2d5d49224fb009a184e6b3862175c0a1407 Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Tue, 24 Nov 2026 19:20:11 -0500 Subject: [PATCH] feat(npc-dialogue): natural typing rhythm, a beat between lines, a typing sound The reveal was one linear clock, so every character cost the same and a comma had nowhere to breathe. It now runs off a schedule: each visible unit costs BASE_UNIT_MS plus a pause after it, longer at a sentence end and longest at a line or paragraph break. Pauses never stack (a break after a full stop owns the pause) and a comma inside a number is not a clause. The schedule is built in relative units and scaled to the target duration, so a line paced to a recorded clip keeps the rhythm instead of flattening it. Between two spoken lines there is now a beat with the speaker's typing dots, and clicking during it starts the next line at once. The opening line of a cascade gets no beat. The typing sound is synthesized (one tick per 55ms at most) from precomputed, seeded tick times, so a re-render cannot re-roll the rhythm. It stays silent on a line with a recorded clip, when the header mute is on, and under reduced motion. It follows a new account preference, users.preferences.dialogue.typewriterSound, separate from the voice switch, with a migration. visibleLength is now defined as visibleText(text).length, so the schedule and the renderer cannot disagree about how long a line is. --- src/app/(frontend)/account/actions.ts | 2 + src/app/(frontend)/account/page.tsx | 3 + .../personnel/contacts/[id]/page.tsx | 6 + src/collections/users/Users.ts | 10 + .../command-palette/CommandPalette.tsx | 3 + .../frontend/account/PreferencesForm.tsx | 27 + src/components/frontend/blocks/AppSidebar.tsx | 1 + .../frontend/personnel/NpcConversation.tsx | 95 +- src/lib/npc-dialogue/AGENTS.md | 49 +- src/lib/npc-dialogue/DialogueText.tsx | 28 +- src/lib/npc-dialogue/pacing.ts | 202 + src/lib/npc-dialogue/typeSound.ts | 88 + ..._add_dialogue_typing_sound_preference.json | 47149 ++++++++++++++++ ...36_add_dialogue_typing_sound_preference.ts | 11 + src/migrations/index.ts | 8 +- src/payload-types.ts | 5 + tests/int/npc-dialogue-pacing.int.spec.ts | 168 + tests/int/npc-dialogue-reveal.int.spec.ts | 30 +- tests/int/preferences-form.int.spec.ts | 3 + 19 files changed, 47873 insertions(+), 15 deletions(-) create mode 100644 src/lib/npc-dialogue/pacing.ts create mode 100644 src/lib/npc-dialogue/typeSound.ts create mode 100644 src/migrations/20261125_000236_add_dialogue_typing_sound_preference.json create mode 100644 src/migrations/20261125_000236_add_dialogue_typing_sound_preference.ts create mode 100644 tests/int/npc-dialogue-pacing.int.spec.ts diff --git a/src/app/(frontend)/account/actions.ts b/src/app/(frontend)/account/actions.ts index abc0c32b..8819cbc2 100644 --- a/src/app/(frontend)/account/actions.ts +++ b/src/app/(frontend)/account/actions.ts @@ -211,6 +211,7 @@ export async function updatePreferences(input: { discordMutedTypes: string[]; xpDisplayMini: boolean; conversationTypewriter: "default" | "on" | "off"; + conversationTypewriterSound: boolean; conversationVoice: boolean; }): Promise { try { @@ -250,6 +251,7 @@ export async function updatePreferences(input: { }, dialogue: { typewriter: input.conversationTypewriter, + typewriterSound: input.conversationTypewriterSound, voice: input.conversationVoice, }, discord: { diff --git a/src/app/(frontend)/account/page.tsx b/src/app/(frontend)/account/page.tsx index 58aa48ee..0d64cf72 100644 --- a/src/app/(frontend)/account/page.tsx +++ b/src/app/(frontend)/account/page.tsx @@ -182,6 +182,9 @@ export default async function AccountPage() { showCallsign={fullUser.preferences?.display?.showCallsign ?? false} showVersionOverlay={fullUser.preferences?.display?.showVersionOverlay ?? true} conversationTypewriter={fullUser.preferences?.dialogue?.typewriter ?? "default"} + conversationTypewriterSound={ + fullUser.preferences?.dialogue?.typewriterSound !== false + } conversationVoice={fullUser.preferences?.dialogue?.voice !== false} showBackgroundTexture={fullUser.preferences?.display?.showBackgroundTexture ?? true} leadOrActual={fullUser.preferences?.display?.leadOrActual ?? "lead"} diff --git a/src/app/(frontend)/personnel/contacts/[id]/page.tsx b/src/app/(frontend)/personnel/contacts/[id]/page.tsx index 984c2192..9b77b7b3 100644 --- a/src/app/(frontend)/personnel/contacts/[id]/page.tsx +++ b/src/app/(frontend)/personnel/contacts/[id]/page.tsx @@ -188,6 +188,11 @@ export default async function NpcContactPage({ params }: NpcContactPageProps) { const voiceEnabled = dialogue ? (isPayloadUser(user) ? user.preferences?.dialogue?.voice : undefined) !== false : false; + // The typing sound has its own switch; it is silent anyway on lines that carry + // a recorded clip, so the two preferences do not have to agree. + const typewriterSoundEnabled = dialogue + ? (isPayloadUser(user) ? user.preferences?.dialogue?.typewriterSound : undefined) !== false + : false; return (
@@ -200,6 +205,7 @@ export default async function NpcContactPage({ params }: NpcContactPageProps) { dialogue={dialogue} initialSeenNodeIds={seenNodeIds} typewriter={typewriterEnabled} + typewriterSound={typewriterSoundEnabled} voice={voiceEnabled} /> } diff --git a/src/collections/users/Users.ts b/src/collections/users/Users.ts index cfa25b65..df2f4de9 100644 --- a/src/collections/users/Users.ts +++ b/src/collections/users/Users.ts @@ -342,6 +342,16 @@ export const Users: CollectionConfig = { "NPC dialogue lines type out letter by letter. Click a line to finish it immediately. Reduced-motion devices always show lines instantly.", }, }, + { + name: "typewriterSound", + label: "Typing sound in conversations", + type: "checkbox", + defaultValue: true, + admin: { + description: + "A soft key tick under lines that type out. Silent on lines with a recorded voice, and muted by the speaker button in a conversation header.", + }, + }, { name: "voice", label: "Play voice clips in conversations", diff --git a/src/components/command-palette/CommandPalette.tsx b/src/components/command-palette/CommandPalette.tsx index d32719e2..866bfa3c 100644 --- a/src/components/command-palette/CommandPalette.tsx +++ b/src/components/command-palette/CommandPalette.tsx @@ -118,6 +118,7 @@ export function CommandPalette({ discordMutedTypes: preferences?.discord?.mutedTypes ?? [], xpDisplayMini: preferences?.display?.xpDisplayMini ?? false, conversationTypewriter: preferences?.dialogue?.typewriter ?? "default", + conversationTypewriterSound: preferences?.dialogue?.typewriterSound !== false, conversationVoice: preferences?.dialogue?.voice !== false, }); if (result.success) { @@ -140,6 +141,7 @@ export function CommandPalette({ discordMutedTypes: preferences?.discord?.mutedTypes ?? [], xpDisplayMini: preferences?.display?.xpDisplayMini ?? false, conversationTypewriter: preferences?.dialogue?.typewriter ?? "default", + conversationTypewriterSound: preferences?.dialogue?.typewriterSound !== false, conversationVoice: preferences?.dialogue?.voice !== false, }); if (result.success) { @@ -162,6 +164,7 @@ export function CommandPalette({ discordMutedTypes: preferences?.discord?.mutedTypes ?? [], xpDisplayMini: !(preferences?.display?.xpDisplayMini ?? false), conversationTypewriter: preferences?.dialogue?.typewriter ?? "default", + conversationTypewriterSound: preferences?.dialogue?.typewriterSound !== false, conversationVoice: preferences?.dialogue?.voice !== false, }); if (result.success) { diff --git a/src/components/frontend/account/PreferencesForm.tsx b/src/components/frontend/account/PreferencesForm.tsx index d61aff80..d1ab80dd 100644 --- a/src/components/frontend/account/PreferencesForm.tsx +++ b/src/components/frontend/account/PreferencesForm.tsx @@ -24,6 +24,8 @@ interface PreferencesFormProps { readonly xpDisplayMini: boolean; /** Stored choice for the NPC conversation typing animation. */ readonly conversationTypewriter: "default" | "on" | "off"; + /** Whether the typing sound plays under lines that type out. */ + readonly conversationTypewriterSound: boolean; /** Whether recorded voice clips play in conversations. */ readonly conversationVoice: boolean; } @@ -39,6 +41,7 @@ export function PreferencesForm({ discordMutedTypes: initialDiscordMutedTypes, xpDisplayMini: initialXPDisplayMini, conversationTypewriter: initialConversationTypewriter, + conversationTypewriterSound: initialConversationTypewriterSound, conversationVoice: initialConversationVoice, }: PreferencesFormProps) { const router = useRouter(); @@ -57,6 +60,9 @@ export function PreferencesForm({ "default" | "on" | "off" >(initialConversationTypewriter); const [conversationVoice, setConversationVoice] = useState(initialConversationVoice); + const [conversationTypewriterSound, setConversationTypewriterSound] = useState( + initialConversationTypewriterSound, + ); const [error, setError] = useState(null); const [success, setSuccess] = useState(null); const [loading, setLoading] = useState(false); @@ -91,6 +97,7 @@ export function PreferencesForm({ discordMutedTypes, xpDisplayMini, conversationTypewriter, + conversationTypewriterSound, conversationVoice, }); @@ -248,6 +255,26 @@ export function PreferencesForm({
+ +