| 1 | /** |
| 2 | * Play dialogue panel: page lines one-by-one, 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 | { label: string, npcEntityId: number|null, lines: object[], lineIndex: number, triggers: object[]|null }} */ |
| 16 | let current = null; |
| 17 | |
| 18 | function show (visible) |
| 19 | { |
| 20 | open = visible; |
| 21 | if (panel) |
| 22 | { panel.hidden = !visible; } |
| 23 | if (!visible) |
| 24 | { current = null; } |
| 25 | } |
| 26 | |
| 27 | function speakerEntity (speaker) |
| 28 | { |
| 29 | if (speaker === "player") |
| 30 | { return getEntity?.("local") ?? null; } |
| 31 | const id = current?.npcEntityId; |
| 32 | if (id == null) |
| 33 | { return null; } |
| 34 | return getEntity?.(id) ?? getEntity?.(Number(id)) ?? null; |
| 35 | } |
| 36 | |
| 37 | function speakerIconCanvas (speaker) |
| 38 | { |
| 39 | const entity = speakerEntity(speaker); |
| 40 | const captured = entity?.entityId != null |
| 41 | ? captureSpeakerIcon?.(entity.entityId, SPEAKER_ICON_SIZE) |
| 42 | : null; |
| 43 | if (captured) |
| 44 | { |
| 45 | captured.className = "dialogue-speaker-icon"; |
| 46 | return captured; |
| 47 | } |
| 48 | const canvas = document.createElement("canvas"); |
| 49 | canvas.className = "dialogue-speaker-icon"; |
| 50 | canvas.width = SPEAKER_ICON_SIZE; |
| 51 | canvas.height = SPEAKER_ICON_SIZE; |
| 52 | const ctx = canvas.getContext("2d"); |
| 53 | ctx.fillStyle = speaker === "player" ? "#6a8caf" : "#8a6a4a"; |
| 54 | ctx.fillRect(0, 0, SPEAKER_ICON_SIZE, SPEAKER_ICON_SIZE); |
| 55 | return canvas; |
| 56 | } |
| 57 | |
| 58 | function entityDisplayName (speaker) |
| 59 | { |
| 60 | const entity = speakerEntity(speaker); |
| 61 | const first = (entity?.firstName ?? "").trim(); |
| 62 | const last = (entity?.lastName ?? "").trim(); |
| 63 | if (first && last) |
| 64 | { return `${first} ${last}`; } |
| 65 | if (first) |
| 66 | { return first; } |
| 67 | return speaker === "player" ? "You" : "…"; |
| 68 | } |
| 69 | |
| 70 | function renderLine () |
| 71 | { |
| 72 | if (!current) |
| 73 | { return; } |
| 74 | const line = current.lines[current.lineIndex]; |
| 75 | const speaker = line?.speaker === "player" ? "player" : "npc"; |
| 76 | if (titleEl) |
| 77 | { titleEl.textContent = entityDisplayName(speaker); } |
| 78 | linesEl?.replaceChildren(); |
| 79 | triggersEl?.replaceChildren(); |
| 80 | |
| 81 | if (line) |
| 82 | { |
| 83 | const row = document.createElement("div"); |
| 84 | row.className = `dialogue-line dialogue-line-${speaker}`; |
| 85 | row.append(speakerIconCanvas(speaker)); |
| 86 | const text = document.createElement("span"); |
| 87 | text.className = "dialogue-line-text"; |
| 88 | text.textContent = line.text ?? ""; |
| 89 | row.append(text); |
| 90 | linesEl?.append(row); |
| 91 | } |
| 92 | |
| 93 | const atLast = current.lineIndex >= (current.lines?.length ?? 1) - 1; |
| 94 | if (!atLast) |
| 95 | { |
| 96 | const nextBtn = document.createElement("button"); |
| 97 | nextBtn.type = "button"; |
| 98 | nextBtn.className = "dialogue-next"; |
| 99 | nextBtn.textContent = "Next"; |
| 100 | nextBtn.addEventListener("click", () => |
| 101 | { |
| 102 | current.lineIndex += 1; |
| 103 | renderLine(); |
| 104 | }); |
| 105 | triggersEl?.append(nextBtn); |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | if (Array.isArray(current.triggers)) |
| 110 | { |
| 111 | renderTriggers(current.triggers); |
| 112 | return; |
| 113 | } |
| 114 | const cont = document.createElement("button"); |
| 115 | cont.type = "button"; |
| 116 | cont.className = "dialogue-next"; |
| 117 | cont.textContent = "Continue"; |
| 118 | cont.addEventListener("click", () => |
| 119 | { |
| 120 | socket.send({ type: MessageType.dialogueAdvance }); |
| 121 | }); |
| 122 | triggersEl?.append(cont); |
| 123 | } |
| 124 | |
| 125 | function renderTriggers (triggers) |
| 126 | { |
| 127 | triggersEl?.replaceChildren(); |
| 128 | if (!triggers.length) |
| 129 | { |
| 130 | const closeBtn = document.createElement("button"); |
| 131 | closeBtn.type = "button"; |
| 132 | closeBtn.textContent = "Close"; |
| 133 | closeBtn.addEventListener("click", () => |
| 134 | { |
| 135 | socket.send({ type: MessageType.closeDialogue }); |
| 136 | show(false); |
| 137 | }); |
| 138 | triggersEl?.append(closeBtn); |
| 139 | return; |
| 140 | } |
| 141 | for (const trig of triggers) |
| 142 | { |
| 143 | const btn = document.createElement("button"); |
| 144 | btn.type = "button"; |
| 145 | btn.textContent = trig.text || trig.trigger || "…"; |
| 146 | btn.addEventListener("click", () => |
| 147 | { |
| 148 | socket.send({ |
| 149 | type: MessageType.dialogueChoose, |
| 150 | trigger: trig.trigger |
| 151 | }); |
| 152 | }); |
| 153 | triggersEl?.append(btn); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | document.getElementById("dialogue-close")?.addEventListener("click", () => |
| 158 | { |
| 159 | socket.send({ type: MessageType.closeDialogue }); |
| 160 | show(false); |
| 161 | }); |
| 162 | |
| 163 | return { |
| 164 | isBlocking () |
| 165 | { |
| 166 | return open; |
| 167 | }, |
| 168 | isOpen () |
| 169 | { |
| 170 | return open; |
| 171 | }, |
| 172 | close () |
| 173 | { |
| 174 | if (!open) |
| 175 | { return; } |
| 176 | socket.send({ type: MessageType.closeDialogue }); |
| 177 | show(false); |
| 178 | }, |
| 179 | npcEntityId () |
| 180 | { |
| 181 | return current?.npcEntityId ?? null; |
| 182 | }, |
| 183 | handleStep (message) |
| 184 | { |
| 185 | current = { |
| 186 | label: message.label, |
| 187 | npcEntityId: message.npcEntityId ?? null, |
| 188 | lines: message.lines?.length ? message.lines : [{ speaker: "npc", text: "…" }], |
| 189 | lineIndex: 0, |
| 190 | triggers: null |
| 191 | }; |
| 192 | show(true); |
| 193 | renderLine(); |
| 194 | }, |
| 195 | handleChoices (message) |
| 196 | { |
| 197 | if (!current) |
| 198 | { |
| 199 | current = { |
| 200 | label: "Dialogue", |
| 201 | npcEntityId: message.npcEntityId ?? null, |
| 202 | lines: [], |
| 203 | lineIndex: 0, |
| 204 | triggers: message.triggers ?? [] |
| 205 | }; |
| 206 | show(true); |
| 207 | } |
| 208 | else |
| 209 | { current.triggers = message.triggers ?? []; } |
| 210 | current.lineIndex = Math.max(0, (current.lines?.length ?? 1) - 1); |
| 211 | renderLine(); |
| 212 | }, |
| 213 | handleClose () |
| 214 | { |
| 215 | show(false); |
| 216 | }, |
| 217 | hide () |
| 218 | { |
| 219 | show(false); |
| 220 | }, |
| 221 | reset () |
| 222 | { |
| 223 | show(false); |
| 224 | } |
| 225 | }; |
| 226 | } |