| 1 | /** |
| 2 | * Play dialogue panel: page lines one-by-one, reward notices, then choices / end. |
| 3 | * Speaker icons are live WebGL portraits from WorldView (same stack as the world). |
| 4 | */ |
| 5 | |
| 6 | import { applyDialogueTextReplacements } from "/shared/speech/dialogueText.js"; |
| 7 | |
| 8 | const SPEAKER_ICON_SIZE = 64; |
| 9 | |
| 10 | export function initDialogue ({ socket, MessageType, getEntity, captureSpeakerIcon }) |
| 11 | { |
| 12 | const panel = document.getElementById("dialogue-panel"); |
| 13 | const titleEl = document.getElementById("dialogue-title"); |
| 14 | const linesEl = document.getElementById("dialogue-lines"); |
| 15 | const triggersEl = document.getElementById("dialogue-triggers"); |
| 16 | let open = false; |
| 17 | /** @type {null | { |
| 18 | * label: string, |
| 19 | * npcEntityId: number|null, |
| 20 | * lines: object[], |
| 21 | * lineIndex: number, |
| 22 | * rewardMessages: string[]|null, |
| 23 | * rewardIndex: number, |
| 24 | * triggers: object[]|null |
| 25 | * }} */ |
| 26 | let current = null; |
| 27 | |
| 28 | function show (visible) |
| 29 | { |
| 30 | open = visible; |
| 31 | if (panel) |
| 32 | { panel.hidden = !visible; } |
| 33 | if (!visible) |
| 34 | { current = null; } |
| 35 | } |
| 36 | |
| 37 | function localIdentity () |
| 38 | { |
| 39 | const entity = getEntity?.("local") ?? null; |
| 40 | return { |
| 41 | firstName: String(entity?.firstName ?? "").trim(), |
| 42 | lastName: String(entity?.lastName ?? "").trim(), |
| 43 | sex: entity?.sex === "female" ? "female" : "male" |
| 44 | }; |
| 45 | } |
| 46 | |
| 47 | function formatDialogueText (text) |
| 48 | { |
| 49 | return applyDialogueTextReplacements(text, localIdentity()); |
| 50 | } |
| 51 | |
| 52 | function speakerEntity (speaker) |
| 53 | { |
| 54 | if (speaker === "player") |
| 55 | { return getEntity?.("local") ?? null; } |
| 56 | const id = current?.npcEntityId; |
| 57 | if (id == null) |
| 58 | { return null; } |
| 59 | return getEntity?.(id) ?? getEntity?.(Number(id)) ?? null; |
| 60 | } |
| 61 | |
| 62 | function speakerIconCanvas (speaker) |
| 63 | { |
| 64 | const entity = speakerEntity(speaker); |
| 65 | const captured = entity?.entityId != null |
| 66 | ? captureSpeakerIcon?.(entity.entityId, SPEAKER_ICON_SIZE) |
| 67 | : null; |
| 68 | if (captured) |
| 69 | { |
| 70 | captured.className = "dialogue-speaker-icon"; |
| 71 | return captured; |
| 72 | } |
| 73 | const canvas = document.createElement("canvas"); |
| 74 | canvas.className = "dialogue-speaker-icon"; |
| 75 | canvas.width = SPEAKER_ICON_SIZE; |
| 76 | canvas.height = SPEAKER_ICON_SIZE; |
| 77 | const ctx = canvas.getContext("2d"); |
| 78 | ctx.fillStyle = speaker === "player" ? "#6a8caf" : "#8a6a4a"; |
| 79 | ctx.fillRect(0, 0, SPEAKER_ICON_SIZE, SPEAKER_ICON_SIZE); |
| 80 | return canvas; |
| 81 | } |
| 82 | |
| 83 | function entityDisplayName (speaker) |
| 84 | { |
| 85 | const entity = speakerEntity(speaker); |
| 86 | const first = (entity?.firstName ?? "").trim(); |
| 87 | const last = (entity?.lastName ?? "").trim(); |
| 88 | if (first && last) |
| 89 | { return `${first} ${last}`; } |
| 90 | if (first) |
| 91 | { return first; } |
| 92 | return speaker === "player" ? "You" : "…"; |
| 93 | } |
| 94 | |
| 95 | function renderRewardLine () |
| 96 | { |
| 97 | if (!current?.rewardMessages?.length) |
| 98 | { return false; } |
| 99 | const message = current.rewardMessages[current.rewardIndex]; |
| 100 | if (!message) |
| 101 | { return false; } |
| 102 | if (titleEl) |
| 103 | { titleEl.textContent = "Reward"; } |
| 104 | linesEl?.replaceChildren(); |
| 105 | triggersEl?.replaceChildren(); |
| 106 | const row = document.createElement("div"); |
| 107 | row.className = "dialogue-line dialogue-line-reward"; |
| 108 | const text = document.createElement("span"); |
| 109 | text.className = "dialogue-line-text dialogue-reward-text"; |
| 110 | text.textContent = message; |
| 111 | row.append(text); |
| 112 | linesEl?.append(row); |
| 113 | |
| 114 | const atLast = current.rewardIndex >= current.rewardMessages.length - 1; |
| 115 | const nextBtn = document.createElement("button"); |
| 116 | nextBtn.type = "button"; |
| 117 | nextBtn.className = "dialogue-next"; |
| 118 | nextBtn.textContent = atLast ? "Continue" : "Next"; |
| 119 | nextBtn.addEventListener("click", () => |
| 120 | { |
| 121 | if (!atLast) |
| 122 | { |
| 123 | current.rewardIndex += 1; |
| 124 | renderRewardLine(); |
| 125 | return; |
| 126 | } |
| 127 | current.rewardMessages = null; |
| 128 | socket.send({ type: MessageType.dialogueAdvance }); |
| 129 | }); |
| 130 | triggersEl?.append(nextBtn); |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | function renderLine () |
| 135 | { |
| 136 | if (!current) |
| 137 | { return; } |
| 138 | if (current.rewardMessages?.length) |
| 139 | { |
| 140 | renderRewardLine(); |
| 141 | return; |
| 142 | } |
| 143 | const line = current.lines[current.lineIndex]; |
| 144 | const speaker = line?.speaker === "player" ? "player" : "npc"; |
| 145 | if (titleEl) |
| 146 | { titleEl.textContent = entityDisplayName(speaker); } |
| 147 | linesEl?.replaceChildren(); |
| 148 | triggersEl?.replaceChildren(); |
| 149 | |
| 150 | if (line) |
| 151 | { |
| 152 | const row = document.createElement("div"); |
| 153 | row.className = `dialogue-line dialogue-line-${speaker}`; |
| 154 | row.append(speakerIconCanvas(speaker)); |
| 155 | const text = document.createElement("span"); |
| 156 | text.className = "dialogue-line-text"; |
| 157 | text.textContent = formatDialogueText(line.text ?? ""); |
| 158 | row.append(text); |
| 159 | linesEl?.append(row); |
| 160 | } |
| 161 | |
| 162 | const atLast = current.lineIndex >= (current.lines?.length ?? 1) - 1; |
| 163 | if (!atLast) |
| 164 | { |
| 165 | const nextBtn = document.createElement("button"); |
| 166 | nextBtn.type = "button"; |
| 167 | nextBtn.className = "dialogue-next"; |
| 168 | nextBtn.textContent = "Next"; |
| 169 | nextBtn.addEventListener("click", () => |
| 170 | { |
| 171 | current.lineIndex += 1; |
| 172 | renderLine(); |
| 173 | }); |
| 174 | triggersEl?.append(nextBtn); |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | if (Array.isArray(current.triggers)) |
| 179 | { |
| 180 | renderTriggers(current.triggers); |
| 181 | return; |
| 182 | } |
| 183 | const cont = document.createElement("button"); |
| 184 | cont.type = "button"; |
| 185 | cont.className = "dialogue-next"; |
| 186 | cont.textContent = "Continue"; |
| 187 | cont.addEventListener("click", () => |
| 188 | { |
| 189 | socket.send({ type: MessageType.dialogueAdvance }); |
| 190 | }); |
| 191 | triggersEl?.append(cont); |
| 192 | } |
| 193 | |
| 194 | function renderTriggers (triggers) |
| 195 | { |
| 196 | triggersEl?.replaceChildren(); |
| 197 | if (!triggers.length) |
| 198 | { |
| 199 | const closeBtn = document.createElement("button"); |
| 200 | closeBtn.type = "button"; |
| 201 | closeBtn.textContent = "Close"; |
| 202 | closeBtn.addEventListener("click", () => |
| 203 | { |
| 204 | socket.send({ type: MessageType.closeDialogue }); |
| 205 | show(false); |
| 206 | }); |
| 207 | triggersEl?.append(closeBtn); |
| 208 | return; |
| 209 | } |
| 210 | for (const trig of triggers) |
| 211 | { |
| 212 | const btn = document.createElement("button"); |
| 213 | btn.type = "button"; |
| 214 | btn.textContent = formatDialogueText(trig.text || trig.trigger || "…"); |
| 215 | btn.addEventListener("click", () => |
| 216 | { |
| 217 | socket.send({ |
| 218 | type: MessageType.dialogueChoose, |
| 219 | trigger: trig.trigger |
| 220 | }); |
| 221 | }); |
| 222 | triggersEl?.append(btn); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | document.getElementById("dialogue-close")?.addEventListener("click", () => |
| 227 | { |
| 228 | socket.send({ type: MessageType.closeDialogue }); |
| 229 | show(false); |
| 230 | }); |
| 231 | |
| 232 | return { |
| 233 | isBlocking () |
| 234 | { |
| 235 | return open; |
| 236 | }, |
| 237 | isOpen () |
| 238 | { |
| 239 | return open; |
| 240 | }, |
| 241 | close () |
| 242 | { |
| 243 | if (!open) |
| 244 | { return; } |
| 245 | socket.send({ type: MessageType.closeDialogue }); |
| 246 | show(false); |
| 247 | }, |
| 248 | npcEntityId () |
| 249 | { |
| 250 | return current?.npcEntityId ?? null; |
| 251 | }, |
| 252 | handleStep (message) |
| 253 | { |
| 254 | current = { |
| 255 | label: message.label, |
| 256 | npcEntityId: message.npcEntityId ?? null, |
| 257 | lines: message.lines?.length ? message.lines : [{ speaker: "npc", text: "…" }], |
| 258 | lineIndex: 0, |
| 259 | rewardMessages: null, |
| 260 | rewardIndex: 0, |
| 261 | triggers: null |
| 262 | }; |
| 263 | show(true); |
| 264 | renderLine(); |
| 265 | }, |
| 266 | handleItemRewards (message) |
| 267 | { |
| 268 | const messages = (message.messages ?? []).filter(Boolean); |
| 269 | if (!messages.length) |
| 270 | { |
| 271 | socket.send({ type: MessageType.dialogueAdvance }); |
| 272 | return; |
| 273 | } |
| 274 | if (!current) |
| 275 | { |
| 276 | current = { |
| 277 | label: "Reward", |
| 278 | npcEntityId: message.npcEntityId ?? null, |
| 279 | lines: [], |
| 280 | lineIndex: 0, |
| 281 | rewardMessages: messages, |
| 282 | rewardIndex: 0, |
| 283 | triggers: null |
| 284 | }; |
| 285 | show(true); |
| 286 | } |
| 287 | else |
| 288 | { |
| 289 | current.rewardMessages = messages; |
| 290 | current.rewardIndex = 0; |
| 291 | current.triggers = null; |
| 292 | } |
| 293 | renderLine(); |
| 294 | }, |
| 295 | handleChoices (message) |
| 296 | { |
| 297 | if (!current) |
| 298 | { |
| 299 | current = { |
| 300 | label: "Dialogue", |
| 301 | npcEntityId: message.npcEntityId ?? null, |
| 302 | lines: [], |
| 303 | lineIndex: 0, |
| 304 | rewardMessages: null, |
| 305 | rewardIndex: 0, |
| 306 | triggers: message.triggers ?? [] |
| 307 | }; |
| 308 | show(true); |
| 309 | } |
| 310 | else |
| 311 | { |
| 312 | current.triggers = message.triggers ?? []; |
| 313 | current.rewardMessages = null; |
| 314 | } |
| 315 | current.lineIndex = Math.max(0, (current.lines?.length ?? 1) - 1); |
| 316 | renderLine(); |
| 317 | }, |
| 318 | handleClose () |
| 319 | { |
| 320 | show(false); |
| 321 | }, |
| 322 | hide () |
| 323 | { |
| 324 | show(false); |
| 325 | }, |
| 326 | reset () |
| 327 | { |
| 328 | show(false); |
| 329 | } |
| 330 | }; |
| 331 | } |