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